tvl-depot/tvix/eval/benches/eval.rs
Florian Klink 1571d7195e fix(tvix/eval/benches): use black_box properly
The purpose of black_box is to actually prevent the compiler from being
able to optimize computation of the benchmarked function away.

To accomplish this, we need to actually *use* black_box to blackbox the
input data away, rather than the return type.

Change-Id: I5438982f57509fbf7b85034346a2739d76aef1fa
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9902
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
2023-11-02 20:39:52 +00:00

36 lines
969 B
Rust

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use itertools::Itertools;
fn interpret(code: &str) {
tvix_eval::Evaluation::new(code, None).evaluate();
}
fn eval_literals(c: &mut Criterion) {
c.bench_function("int", |b| {
b.iter(|| {
interpret(black_box("42"));
})
});
}
fn eval_merge_attrs(c: &mut Criterion) {
c.bench_function("merge small attrs", |b| {
b.iter(|| {
interpret(black_box("{ a = 1; b = 2; } // { c = 3; }"));
})
});
c.bench_function("merge large attrs with small attrs", |b| {
let large_attrs = format!(
"{{{}}}",
(0..10000).map(|n| format!("a{n} = {n};")).join(" ")
);
let expr = format!("{large_attrs} // {{ c = 3; }}");
b.iter(move || {
interpret(black_box(&expr));
})
});
}
criterion_group!(benches, eval_literals, eval_merge_attrs);
criterion_main!(benches);