mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(bench): reuse allocator in parser + lexer benchmarks (#3053)
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.
This commit is contained in:
parent
84c43c8282
commit
6bc18e15e0
3 changed files with 23 additions and 14 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
use std::{convert::From, ops::Deref};
|
use std::{
|
||||||
|
convert::From,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
};
|
||||||
|
|
||||||
mod arena;
|
mod arena;
|
||||||
|
|
||||||
|
|
@ -24,6 +27,12 @@ impl Deref for Allocator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DerefMut for Allocator {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.bump
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,14 @@ fn bench_lexer(criterion: &mut Criterion) {
|
||||||
BenchmarkId::from_parameter(&file.file_name),
|
BenchmarkId::from_parameter(&file.file_name),
|
||||||
&file.source_text,
|
&file.source_text,
|
||||||
|b, source_text| {
|
|b, source_text| {
|
||||||
b.iter_with_large_drop(|| {
|
// Do not include initializing allocator in benchmark.
|
||||||
// Include the allocator drop time to make time measurement consistent.
|
// User code would likely reuse the same allocator over and over to parse multiple files,
|
||||||
// Otherwise the allocator will allocate huge memory chunks (by power of two) from the
|
// so we do the same here.
|
||||||
// system allocator, which makes time measurement unequal during long runs.
|
let mut allocator = Allocator::default();
|
||||||
let allocator = Allocator::default();
|
b.iter(|| {
|
||||||
let mut lexer = Lexer::new_for_benchmarks(&allocator, source_text, source_type);
|
let mut lexer = Lexer::new_for_benchmarks(&allocator, source_text, source_type);
|
||||||
while lexer.next_token().kind != Kind::Eof {}
|
while lexer.next_token().kind != Kind::Eof {}
|
||||||
allocator
|
allocator.reset();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,13 @@ fn bench_parser(criterion: &mut Criterion) {
|
||||||
BenchmarkId::from_parameter(&file.file_name),
|
BenchmarkId::from_parameter(&file.file_name),
|
||||||
&file.source_text,
|
&file.source_text,
|
||||||
|b, source_text| {
|
|b, source_text| {
|
||||||
b.iter_with_large_drop(|| {
|
// Do not include initializing allocator in benchmark.
|
||||||
// Include the allocator drop time to make time measurement consistent.
|
// User code would likely reuse the same allocator over and over to parse multiple files,
|
||||||
// Otherwise the allocator will allocate huge memory chunks (by power of two) from the
|
// so we do the same here.
|
||||||
// system allocator, which makes time measurement unequal during long runs.
|
let mut allocator = Allocator::default();
|
||||||
let allocator = Allocator::default();
|
b.iter(|| {
|
||||||
_ = Parser::new(&allocator, source_text, source_type).parse();
|
Parser::new(&allocator, source_text, source_type).parse();
|
||||||
allocator
|
allocator.reset();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue