Skip to content

Commit bfa8ed6

Browse files
authored
Move file i/o out of benchmarked code (#43)
Replaces the file read within each test with a simple memory copy. No reason to include the time it takes to read the file in the benchmark. This doesn't have a major impact, but it is just enough to be measurable on my machine (difference of about 2 standard deviations on some of the lex/parse/nu_old tests).
1 parent 4b92442 commit bfa8ed6

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

benches/benchmarks.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,14 @@ fn compiler_benchmarks() -> impl IntoBenchmarks {
224224
for bench_name in BENCHMARKS {
225225
for stage in STAGES {
226226
let bench_file = format!("benches/nu/{bench_name}.nu");
227+
let bench_contents =
228+
std::fs::read(&bench_file).expect(&format!("Cannot find file {bench_file}"));
227229

228230
let bench = match stage {
229231
Stage::Lex => {
230232
let name = format!("{bench_name}_lex");
231233
benchmark_fn(name, move |b| {
232-
let contents = std::fs::read(&bench_file)
233-
.expect(&format!("Cannot find file {bench_file}"));
234+
let contents = bench_contents.clone();
234235
b.iter(move || {
235236
let (tokens, err) = lex(&contents, 0);
236237
if let Err(e) = err {
@@ -247,8 +248,7 @@ fn compiler_benchmarks() -> impl IntoBenchmarks {
247248
let (compiler_def_init, span_offset) =
248249
setup_compiler(&bench_file, false, false, false)
249250
.expect("Error setting up compiler");
250-
let contents = std::fs::read(&bench_file)
251-
.expect(&format!("Cannot find file {bench_file}"));
251+
let contents = bench_contents.clone();
252252
let (tokens, err) = lex(&contents, span_offset);
253253
if let Err(e) = err {
254254
tokens.eprint(&contents);
@@ -307,8 +307,7 @@ fn compiler_benchmarks() -> impl IntoBenchmarks {
307307
let name = format!("{bench_name}_nu_old");
308308
benchmark_fn(name, move |b| {
309309
let engine_state = make_engine_state();
310-
let contents = std::fs::read(bench_file.clone())
311-
.expect(&format!("Cannot find file {bench_file}"));
310+
let contents = bench_contents.clone();
312311
b.iter(move || parse_nu_old(&engine_state, &contents))
313312
})
314313
}

0 commit comments

Comments
 (0)