diff options
-rw-r--r-- | tvix/eval/src/chunk.rs | 10 | ||||
-rw-r--r-- | tvix/eval/src/compiler/mod.rs | 9 | ||||
-rw-r--r-- | tvix/eval/src/value/mod.rs | 7 |
3 files changed, 26 insertions, 0 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index b6fc6be5b99c..f1a35a6ce162 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -70,6 +70,11 @@ impl Chunk { self.spans[0].span } + /// Return a reference to the last op in the chunk, if any + pub fn last_op(&self) -> Option<&OpCode> { + self.code.last() + } + /// Pop the last operation from the chunk and clean up its tracked /// span. Used when the compiler backtracks. pub fn pop_op(&mut self) { @@ -90,6 +95,11 @@ impl Chunk { ConstantIdx(idx) } + /// Return a reference to the constant at the given [`ConstantIdx`] + pub fn get_constant(&self, constant: ConstantIdx) -> Option<&Value> { + self.constants.get(constant.0) + } + // Span tracking implementation fn push_span(&mut self, span: codemap::Span, start: usize) { diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 60f5666ea9ce..0fa3d4139308 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -1457,6 +1457,15 @@ impl Compiler<'_> { } fn emit_force<N: ToSpan>(&mut self, node: &N) { + if let Some(&OpCode::OpConstant(c)) = self.chunk().last_op() { + if !self.chunk().get_constant(c).unwrap().is_thunk() { + // Optimization: Don't emit a force op for non-thunk constants, since they don't + // need one! + // TODO: this is probably doable for more ops (?) + return; + } + } + self.push_op(OpCode::OpForce, node); } diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index df663dd34c06..e9b509834273 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -724,6 +724,13 @@ impl Value { gen_is!(is_attrs, Value::Attrs(_)); gen_is!(is_catchable, Value::Catchable(_)); + /// Returns `true` if the value is a [`Thunk`]. + /// + /// [`Thunk`]: Value::Thunk + pub fn is_thunk(&self) -> bool { + matches!(self, Self::Thunk(..)) + } + /// Compare `self` against other using (fallible) Nix ordering semantics. /// /// The function is intended to be used from within other generator |