about summary refs log tree commit diff
path: root/tvix/nix-compat/src/narinfo.rs
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2023-10-28T16·19+0000
committerclbot <clbot@tvl.fyi>2023-10-28T17·11+0000
commit12fb5004d9033ca50579deddcdb5ee0160a296fd (patch)
treeb747c6ffa434e160d058db3f5a2f17a8a1fcc827 /tvix/nix-compat/src/narinfo.rs
parentae839983818d69852d733c23794d74933bcfe74e (diff)
fix(nix-compat/narinfo): don't allocate errors early r/6902
We also switch the MissingField error to &'static str, since we only 
parse a fixed set of fields.

Together, this makes the performance impact of error handling 
negligible in batch happy-path parsing.

Change-Id: I2bd0ef2f5b35fcaced56b32d238eca75ac199ef1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9867
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: edef <edef@edef.eu>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/narinfo.rs')
-rw-r--r--tvix/nix-compat/src/narinfo.rs23
1 files changed, 12 insertions, 11 deletions
diff --git a/tvix/nix-compat/src/narinfo.rs b/tvix/nix-compat/src/narinfo.rs
index b22415218592..1cd89d7c5840 100644
--- a/tvix/nix-compat/src/narinfo.rs
+++ b/tvix/nix-compat/src/narinfo.rs
@@ -80,11 +80,11 @@ impl<'a> NarInfo<'a> {
         for line in input.lines() {
             let (tag, val) = line
                 .split_once(':')
-                .ok_or(Error::InvalidLine(line.to_string()))?;
+                .ok_or_else(|| Error::InvalidLine(line.to_string()))?;
 
             let val = val
                 .strip_prefix(' ')
-                .ok_or(Error::InvalidLine(line.to_string()))?;
+                .ok_or_else(|| Error::InvalidLine(line.to_string()))?;
 
             match tag {
                 "StorePath" => {
@@ -121,7 +121,7 @@ impl<'a> NarInfo<'a> {
                 "FileHash" => {
                     let val = val
                         .strip_prefix("sha256:")
-                        .ok_or(Error::MissingPrefixForHash(tag.to_string()))?;
+                        .ok_or_else(|| Error::MissingPrefixForHash(tag.to_string()))?;
                     let val = nixbase32::decode_fixed::<32>(val)
                         .map_err(|e| Error::UnableToDecodeHash(tag.to_string(), e))?;
 
@@ -141,7 +141,7 @@ impl<'a> NarInfo<'a> {
                 "NarHash" => {
                     let val = val
                         .strip_prefix("sha256:")
-                        .ok_or(Error::MissingPrefixForHash(tag.to_string()))?;
+                        .ok_or_else(|| Error::MissingPrefixForHash(tag.to_string()))?;
 
                     let val = nixbase32::decode_fixed::<32>(val)
                         .map_err(|e| Error::UnableToDecodeHash(tag.to_string(), e))?;
@@ -210,7 +210,8 @@ impl<'a> NarInfo<'a> {
                     signatures.push(val);
                 }
                 "CA" => {
-                    let val = parse_ca(val).ok_or(Error::UnableToParseCA(val.to_string()))?;
+                    let val =
+                        parse_ca(val).ok_or_else(|| Error::UnableToParseCA(val.to_string()))?;
 
                     if ca.replace(val).is_some() {
                         return Err(Error::DuplicateField(tag.to_string()));
@@ -223,15 +224,15 @@ impl<'a> NarInfo<'a> {
         }
 
         Ok(NarInfo {
-            store_path: store_path.ok_or(Error::MissingField("StorePath".to_string()))?,
-            nar_hash: nar_hash.ok_or(Error::MissingField("NarHash".to_string()))?,
-            nar_size: nar_size.ok_or(Error::MissingField("NarSize".to_string()))?,
-            references: references.ok_or(Error::MissingField("References".to_string()))?,
+            store_path: store_path.ok_or(Error::MissingField("StorePath"))?,
+            nar_hash: nar_hash.ok_or(Error::MissingField("NarHash"))?,
+            nar_size: nar_size.ok_or(Error::MissingField("NarSize"))?,
+            references: references.ok_or(Error::MissingField("References"))?,
             signatures,
             ca,
             system,
             deriver,
-            url: url.ok_or(Error::MissingField("URL".to_string()))?,
+            url: url.ok_or(Error::MissingField("URL"))?,
             compression,
             file_hash,
             file_size,
@@ -417,7 +418,7 @@ pub enum Error {
     DuplicateField(String),
 
     #[error("missing field: {0}")]
-    MissingField(String),
+    MissingField(&'static str),
 
     #[error("invalid line: {0}")]
     InvalidLine(String),