about summary refs log tree commit diff
path: root/tvix/eval/src/builtins/mod.rs
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2022-11-23T08·34-0800
committerclbot <clbot@tvl.fyi>2022-11-23T13·02+0000
commita740653c83436e3adc14343efa82422eb0a44933 (patch)
tree685fd9550b9ac0caf5330f95a801b93de92a3d96 /tvix/eval/src/builtins/mod.rs
parentc537cc6fcee5f5cde4b0e6f8c5d6dcd5d8e3690f (diff)
feat(tvix/eval): make NixList::clone() cheap r/5301
When we start unrecursivifying (sp?) things, Rust's borrow checker
is going to be a headache; its magic only works when you use the CPU
stack as your call stack.

Fixing the borrow checker issues usually involves adding lots of
`clone()`s.  Right now `NixList` is the only variant of `Value` that
isn't cheap to clone() -- all the others are either a wrapper around
Rc or else are of bounded size.

Note that this requires dropping the `DerefMut for NixList` instance
and using `Vec<Value>` instead in those situations.

Change-Id: I5a47df66855342aa2064f8f3cb7934ff422d26bd
Signed-off-by: Adam Joseph <adam@westernsemico.com>
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7359
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins/mod.rs')
-rw-r--r--tvix/eval/src/builtins/mod.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs
index 89e7c37d3f20..7c4f40479bc2 100644
--- a/tvix/eval/src/builtins/mod.rs
+++ b/tvix/eval/src/builtins/mod.rs
@@ -360,7 +360,7 @@ mod pure_builtins {
 
         let operator = attrs.select_required("operator")?;
 
-        let mut res = NixList::new();
+        let mut res: Vec<Value> = vec![];
         let mut done_keys: Vec<Value> = vec![];
 
         let mut insert_key = |k: Value, vm: &mut VM| -> Result<bool, ErrorKind> {
@@ -387,7 +387,7 @@ mod pure_builtins {
             work_set.extend(op_result.into_iter());
         }
 
-        Ok(Value::List(res))
+        Ok(Value::List(NixList::from(res)))
     }
 
     #[builtin("genList")]
@@ -415,15 +415,16 @@ mod pure_builtins {
 
     #[builtin("groupBy")]
     fn builtin_group_by(vm: &mut VM, f: Value, list: Value) -> Result<Value, ErrorKind> {
-        let mut res: BTreeMap<NixString, Value> = BTreeMap::new();
+        let mut res: BTreeMap<NixString, Vec<Value>> = BTreeMap::new();
         for val in list.to_list()? {
             let key = vm.call_with(&f, [val.clone()])?.force(vm)?.to_str()?;
-            res.entry(key)
-                .or_insert_with(|| Value::List(NixList::new()))
-                .as_list_mut()?
-                .push(val);
+            res.entry(key).or_insert_with(|| vec![]).push(val);
         }
-        Ok(Value::attrs(NixAttrs::from_map(res)))
+        Ok(Value::attrs(NixAttrs::from_map(
+            res.into_iter()
+                .map(|(k, v)| (k, Value::List(NixList::from(v))))
+                .collect(),
+        )))
     }
 
     #[builtin("hasAttr")]
@@ -745,7 +746,7 @@ mod pure_builtins {
         let re: Regex = Regex::new(re.as_str()).unwrap();
         let mut capture_locations = re.capture_locations();
         let num_captures = capture_locations.len();
-        let mut ret = NixList::new();
+        let mut ret: Vec<Value> = vec![];
         let mut pos = 0;
 
         while let Some(thematch) = re.captures_read_at(&mut capture_locations, text, pos) {
@@ -770,12 +771,12 @@ mod pure_builtins {
         // push the unmatched characters following the last match
         ret.push(Value::from(&text[pos..]));
 
-        Ok(Value::List(ret))
+        Ok(Value::List(NixList::from(ret)))
     }
 
     #[builtin("sort")]
     fn builtin_sort(vm: &mut VM, comparator: Value, list: Value) -> Result<Value, ErrorKind> {
-        let mut list = list.to_list()?;
+        let mut list = list.to_list()?.into_vec();
 
         // Used to let errors "escape" from the sorting closure. If anything
         // ends up setting an error, it is returned from this function.
@@ -806,7 +807,7 @@ mod pure_builtins {
         });
 
         match error {
-            None => Ok(Value::List(list)),
+            None => Ok(Value::List(NixList::from(list))),
             Some(e) => Err(e),
         }
     }