about summary refs log tree commit diff
path: root/tvix/eval/src/compiler
diff options
context:
space:
mode:
authorAspen Smith <root@gws.fyi>2024-01-30T19·21-0500
committeraspen <root@gws.fyi>2024-02-01T21·08+0000
commit8caa097ba84f57515513d86621826556f2374fad (patch)
tree1eb7ecac8755b63c9907c870eaa6a16a6f87c71e /tvix/eval/src/compiler
parent25f092002554fa53497fc7d844ae257832ad655a (diff)
feat(tvix/eval): Don't emit OpForce for non-thunk constants r/7463
In the compiler, skip emitting an OpForce if the last op was an
OpConstant for a non-thunk constant. This gives a small (~1% on my
machine) perf boost, eg when evaluating hello.outPath:

    ❯ hyperfine \
        "./before --no-warnings -E '(import <nixpkgs> {}).hello.outPath'" \
        "./after --no-warnings -E '(import <nixpkgs> {}).hello.outPath'"
    Benchmark 1: ./before --no-warnings -E '(import <nixpkgs> {}).hello.outPath'
      Time (mean ± σ):      1.151 s ±  0.022 s    [User: 1.003 s, System: 0.151 s]
      Range (min … max):    1.123 s …  1.184 s    10 runs

    Benchmark 2: ./after --no-warnings -E '(import <nixpkgs> {}).hello.outPath'
      Time (mean ± σ):      1.140 s ±  0.022 s    [User: 0.989 s, System: 0.152 s]
      Range (min … max):    1.115 s …  1.175 s    10 runs

    Summary
      ./after --no-warnings -E '(import <nixpkgs> {}).hello.outPath' ran
        1.01 ± 0.03 times faster than ./before --no-warnings -E '(import <nixpkgs> {}).hello.outPath'

Change-Id: I2105fd431d4bad699087907e16c789418e9a4062
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10714
Reviewed-by: sterni <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/compiler')
-rw-r--r--tvix/eval/src/compiler/mod.rs9
1 files changed, 9 insertions, 0 deletions
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);
     }