about summary refs log tree commit diff
path: root/tvix/eval/src/vm.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-26T16·22+0300
committertazjin <tazjin@tvl.su>2022-09-03T13·19+0000
commit3a2fcc8bc2eb3895a5dfdb268869d550e359ffe7 (patch)
tree7da39a05125156dd7d7236480cf168a15b4633e8 /tvix/eval/src/vm.rs
parent3270817b9053cc0d656bf9f8522e81a2917b3036 (diff)
refactor(tvix/eval): avoid cloning in NixAttrs::update if possible r/4616
Refactors the update function to take the attribute sets by value
instead.

To facilitate this, we use an equivalent of the currently unstable
`Rc::clone_or_unwrap` in the VM when encountering attribute sets, so
that in cases where the only references to the attrs being updated are
the ones on the stack those clones are avoided completely.

This does make update() a little bit more tricky internally, as some
optimised branches can directly return the moved value, and others
need to destructure with ownership. For this reason there are now two
different match statements handling the different ownership cases.

Change-Id: Ia77d3ba5c86afb75b9f1f51758bda61729ba5aab
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6279
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/vm.rs')
-rw-r--r--tvix/eval/src/vm.rs12
1 files changed, 9 insertions, 3 deletions
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs
index 65534006d0e1..6de9cd03c49c 100644
--- a/tvix/eval/src/vm.rs
+++ b/tvix/eval/src/vm.rs
@@ -203,10 +203,10 @@ impl VM {
                 OpCode::OpAttrPath(count) => self.run_attr_path(count)?,
 
                 OpCode::OpAttrsUpdate => {
-                    let rhs = self.pop().to_attrs()?;
-                    let lhs = self.pop().to_attrs()?;
+                    let rhs = unwrap_or_clone_rc(self.pop().to_attrs()?);
+                    let lhs = unwrap_or_clone_rc(self.pop().to_attrs()?);
 
-                    self.push(Value::Attrs(Rc::new(lhs.update(&rhs))))
+                    self.push(Value::Attrs(Rc::new(lhs.update(rhs))))
                 }
 
                 OpCode::OpAttrsSelect => {
@@ -414,6 +414,12 @@ impl VM {
     }
 }
 
+// TODO: use Rc::unwrap_or_clone once it is stabilised.
+// https://doc.rust-lang.org/std/rc/struct.Rc.html#method.unwrap_or_clone
+fn unwrap_or_clone_rc<T: Clone>(rc: Rc<T>) -> T {
+    Rc::try_unwrap(rc).unwrap_or_else(|rc| (*rc).clone())
+}
+
 pub fn run_lambda(lambda: Lambda) -> EvalResult<Value> {
     let mut vm = VM {
         frames: vec![],