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:
overlookmotel 2024-04-22 02:03:26 +01:00 committed by GitHub
parent 84c43c8282
commit 6bc18e15e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 14 deletions

View file

@ -1,4 +1,7 @@
use std::{convert::From, ops::Deref};
use std::{
convert::From,
ops::{Deref, DerefMut},
};
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)]
mod test {
use std::ops::Deref;

View file

@ -27,14 +27,14 @@ fn bench_lexer(criterion: &mut Criterion) {
BenchmarkId::from_parameter(&file.file_name),
&file.source_text,
|b, source_text| {
b.iter_with_large_drop(|| {
// Include the allocator drop time to make time measurement consistent.
// Otherwise the allocator will allocate huge memory chunks (by power of two) from the
// system allocator, which makes time measurement unequal during long runs.
let allocator = Allocator::default();
// 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(|| {
let mut lexer = Lexer::new_for_benchmarks(&allocator, source_text, source_type);
while lexer.next_token().kind != Kind::Eof {}
allocator
allocator.reset();
});
},
);

View file

@ -12,13 +12,13 @@ fn bench_parser(criterion: &mut Criterion) {
BenchmarkId::from_parameter(&file.file_name),
&file.source_text,
|b, source_text| {
b.iter_with_large_drop(|| {
// Include the allocator drop time to make time measurement consistent.
// Otherwise the allocator will allocate huge memory chunks (by power of two) from the
// system allocator, which makes time measurement unequal during long runs.
let allocator = Allocator::default();
_ = Parser::new(&allocator, source_text, source_type).parse();
allocator
// 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();
});
},
);