feat(tvix/eval): Don't emit OpForce for non-thunk constants
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
This commit is contained in:
parent
25f0920025
commit
8caa097ba8
3 changed files with 26 additions and 0 deletions
|
@ -70,6 +70,11 @@ impl Chunk {
|
||||||
self.spans[0].span
|
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
|
/// Pop the last operation from the chunk and clean up its tracked
|
||||||
/// span. Used when the compiler backtracks.
|
/// span. Used when the compiler backtracks.
|
||||||
pub fn pop_op(&mut self) {
|
pub fn pop_op(&mut self) {
|
||||||
|
@ -90,6 +95,11 @@ impl Chunk {
|
||||||
ConstantIdx(idx)
|
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
|
// Span tracking implementation
|
||||||
|
|
||||||
fn push_span(&mut self, span: codemap::Span, start: usize) {
|
fn push_span(&mut self, span: codemap::Span, start: usize) {
|
||||||
|
|
|
@ -1457,6 +1457,15 @@ impl Compiler<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_force<N: ToSpan>(&mut self, node: &N) {
|
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);
|
self.push_op(OpCode::OpForce, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -724,6 +724,13 @@ impl Value {
|
||||||
gen_is!(is_attrs, Value::Attrs(_));
|
gen_is!(is_attrs, Value::Attrs(_));
|
||||||
gen_is!(is_catchable, Value::Catchable(_));
|
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.
|
/// Compare `self` against other using (fallible) Nix ordering semantics.
|
||||||
///
|
///
|
||||||
/// The function is intended to be used from within other generator
|
/// The function is intended to be used from within other generator
|
||||||
|
|
Loading…
Reference in a new issue