mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +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.
52 lines
843 B
Rust
52 lines
843 B
Rust
use std::{
|
|
convert::From,
|
|
ops::{Deref, DerefMut},
|
|
};
|
|
|
|
mod arena;
|
|
|
|
pub use arena::{Box, String, Vec};
|
|
use bumpalo::Bump;
|
|
|
|
#[derive(Default)]
|
|
pub struct Allocator {
|
|
bump: Bump,
|
|
}
|
|
|
|
impl From<Bump> for Allocator {
|
|
fn from(bump: Bump) -> Self {
|
|
Self { bump }
|
|
}
|
|
}
|
|
|
|
impl Deref for Allocator {
|
|
type Target = Bump;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.bump
|
|
}
|
|
}
|
|
|
|
impl DerefMut for Allocator {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.bump
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use std::ops::Deref;
|
|
|
|
use crate::Allocator;
|
|
use bumpalo::Bump;
|
|
|
|
#[test]
|
|
fn test_api() {
|
|
let bump = Bump::new();
|
|
let allocator: Allocator = bump.into();
|
|
#[allow(clippy::explicit_deref_methods)]
|
|
{
|
|
_ = allocator.deref();
|
|
}
|
|
}
|
|
}
|