mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 21:58:36 +00:00
Re-use allocator in parser + lexer benchmarks. I believe this is the recommended usage when parsing a bunch of files - to re-use one allocator rather than create a fresh one for each run, so it makes sense to me that this is what the benchmark should measure. Doesn't show much difference on CodSpeed because it only runs the benchmark once, and it treats allocations as free anyway. But I imagine the difference may show up a bit more in a standard criterion benchmark.
30 lines
1.1 KiB
Rust
30 lines
1.1 KiB
Rust
use oxc_allocator::Allocator;
|
|
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
|
use oxc_parser::Parser;
|
|
use oxc_span::SourceType;
|
|
use oxc_tasks_common::TestFiles;
|
|
|
|
fn bench_parser(criterion: &mut Criterion) {
|
|
let mut group = criterion.benchmark_group("parser");
|
|
for file in TestFiles::complicated().files() {
|
|
let source_type = SourceType::from_path(&file.file_name).unwrap();
|
|
group.bench_with_input(
|
|
BenchmarkId::from_parameter(&file.file_name),
|
|
&file.source_text,
|
|
|b, source_text| {
|
|
// Do not include initializing allocator in benchmark.
|
|
// User code would likely reuse the same allocator over and over to parse multiple files,
|
|
// so we do the same here.
|
|
let mut allocator = Allocator::default();
|
|
b.iter(|| {
|
|
Parser::new(&allocator, source_text, source_type).parse();
|
|
allocator.reset();
|
|
});
|
|
},
|
|
);
|
|
}
|
|
group.finish();
|
|
}
|
|
|
|
criterion_group!(parser, bench_parser);
|
|
criterion_main!(parser);
|