about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/parser.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-11-05T17·36+0300
committerclbot <clbot@tvl.fyi>2023-11-05T20·28+0000
commit327548d2fdefddc211d15302a8108999f352e685 (patch)
tree60be3a08fe4f1f54cf760a259ef75adf0196e5ae /tvix/nix-compat/src/derivation/parser.rs
parentb3b1f649d613c97a196528b1210dd5b914995c14 (diff)
refactor(tvix/nix-compat): check presence with btree_map's entry API r/6956
Walking a btree_map twice is more expensive than copying a string,
especially because the cloning only happens in the (non-hot) error
path.

This fixes a clippy lint, so it's related to b/321.

Change-Id: I2ccfd0bc46792a45d277f47564e595b87107d8be
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9962
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/derivation/parser.rs')
-rw-r--r--tvix/nix-compat/src/derivation/parser.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/tvix/nix-compat/src/derivation/parser.rs b/tvix/nix-compat/src/derivation/parser.rs
index 3b79f7bea3..36e512514f 100644
--- a/tvix/nix-compat/src/derivation/parser.rs
+++ b/tvix/nix-compat/src/derivation/parser.rs
@@ -9,7 +9,7 @@ use nom::character::complete::char as nomchar;
 use nom::combinator::{all_consuming, map_res};
 use nom::multi::{separated_list0, separated_list1};
 use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
-use std::collections::{BTreeMap, BTreeSet};
+use std::collections::{btree_map, BTreeMap, BTreeSet};
 use thiserror;
 
 use crate::derivation::parse_error::{into_nomerror, ErrorKind, NomError, NomResult};
@@ -274,13 +274,14 @@ where
                     for (k, v) in pairs.into_iter() {
                         // collect the 2-tuple to a BTreeMap,
                         // and fail if the key was already seen before.
-                        if kvs.contains_key(&k) {
-                            return Err(nom::Err::Failure(NomError {
-                                input: i,
-                                code: ErrorKind::DuplicateMapKey(k),
-                            }));
-                        } else {
-                            kvs.insert(k, v);
+                        match kvs.entry(k) {
+                            btree_map::Entry::Vacant(e) => { e.insert(v); },
+                            btree_map::Entry::Occupied(e) => {
+                                return Err(nom::Err::Failure(NomError {
+                                    input: i,
+                                    code: ErrorKind::DuplicateMapKey(e.key().clone()),
+                                }));
+                            }
                         }
                     }
                     Ok((rest, kvs))