diff --git a/crates/oxc_allocator/src/lib.rs b/crates/oxc_allocator/src/lib.rs index da6f1c14b..33e738d02 100644 --- a/crates/oxc_allocator/src/lib.rs +++ b/crates/oxc_allocator/src/lib.rs @@ -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; diff --git a/tasks/benchmark/benches/lexer.rs b/tasks/benchmark/benches/lexer.rs index 6d2284767..7a95125f6 100644 --- a/tasks/benchmark/benches/lexer.rs +++ b/tasks/benchmark/benches/lexer.rs @@ -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(); }); }, ); diff --git a/tasks/benchmark/benches/parser.rs b/tasks/benchmark/benches/parser.rs index db0aaa1f1..05418ee2f 100644 --- a/tasks/benchmark/benches/parser.rs +++ b/tasks/benchmark/benches/parser.rs @@ -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(); }); }, );