Skip to content

Commit 61ed8cd

Browse files
committed
fix(benches): defer benchmark initialization properly and refine loop allocations
1 parent d0ad0f8 commit 61ed8cd

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ boa_runtime.workspace = true
1111
[dev-dependencies]
1212
criterion.workspace = true
1313
walkdir = "2"
14+
regex.workspace = true
1415

1516
[target.x86_64-unknown-linux-gnu.dev-dependencies]
1617
jemallocator.workspace = true

benches/benches/scripts.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ fn prepare_script_bench(path: &Path) -> PreparedScriptBench {
4444
}
4545

4646
fn bench_scripts(c: &mut Criterion) {
47+
let args: Vec<String> = std::env::args().collect();
48+
let is_list = args.iter().any(|arg| arg == "--list");
49+
let filter = args.iter().skip(1).find(|arg| !arg.starts_with('-'));
50+
let filter_re = filter.and_then(|f| regex::Regex::new(f).ok());
51+
4752
let scripts_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts");
4853

4954
let scripts: Vec<_> = walkdir::WalkDir::new(&scripts_dir)
@@ -70,15 +75,25 @@ fn bench_scripts(c: &mut Criterion) {
7075
group.measurement_time(Duration::from_secs(5));
7176
}
7277

73-
let path = path.to_path_buf();
74-
let mut prepared: Option<PreparedScriptBench> = None;
78+
let should_run = !is_list
79+
&& filter.is_none_or(|f| {
80+
let full_name = format!("{name}/Execution");
81+
if let Some(re) = &filter_re {
82+
re.is_match(&full_name)
83+
} else {
84+
full_name.contains(f)
85+
}
86+
});
87+
88+
let mut prepared = should_run.then(|| prepare_script_bench(path));
7589

7690
group.bench_function("Execution", move |b| {
77-
let prepared = prepared.get_or_insert_with(|| prepare_script_bench(&path));
78-
let function = prepared.function.clone();
79-
let context = &mut prepared.context;
91+
if let Some(prepared) = prepared.as_mut() {
92+
let function = prepared.function.clone();
93+
let context = &mut prepared.context;
8094

81-
b.iter(|| function.call(&JsValue::undefined(), &[], context));
95+
b.iter(|| function.call(&JsValue::undefined(), &[], context));
96+
}
8297
});
8398
group.finish();
8499
}

0 commit comments

Comments
 (0)