Skip to content

Commit

Permalink
More bench
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jul 14, 2024
1 parent e4f57ea commit 0be7c20
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
44 changes: 40 additions & 4 deletions crates/swc_allocator/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate swc_malloc;

use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_allocator::Allocator;
use swc_allocator::{FastAlloc, MemorySpace};

fn bench_alloc(c: &mut Criterion) {
fn direct_alloc_std(b: &mut Bencher, times: usize) {
Expand All @@ -25,9 +25,22 @@ fn bench_alloc(c: &mut Criterion) {
})
}

fn direct_alloc_in_scope(b: &mut Bencher, times: usize) {
fn fast_alloc_no_scope(b: &mut Bencher, times: usize) {
b.iter(|| {
let allocator = Allocator::default();
let allocator = FastAlloc::default();

let mut vec = allocator.vec();
for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(allocator.alloc(black_box(i)));
vec.push(item);
}
})
}

fn direct_alloc_scoped(b: &mut Bencher, times: usize) {
b.iter(|| {
let allocator = MemorySpace::default();

allocator.scope(|| {
let mut vec = swc_allocator::vec::Vec::new();
Expand All @@ -41,14 +54,37 @@ fn bench_alloc(c: &mut Criterion) {
})
}

fn fast_alloc_scoped(b: &mut Bencher, times: usize) {
b.iter(|| {
MemorySpace::default().scope(|| {
let allocator = FastAlloc::default();

let mut vec = allocator.vec();

for i in 0..times {
let item: swc_allocator::boxed::Box<usize> =
black_box(allocator.alloc(black_box(i)));
vec.push(item);
}
});
})
}

c.bench_function("common/allocator/alloc/std/100000", |b| {
direct_alloc_std(b, 100000)
});
c.bench_function("common/allocator/alloc/no-scope/100000", |b| {
direct_alloc_no_scope(b, 100000)
});
c.bench_function("common/allocator/alloc/scoped/100000", |b| {
direct_alloc_in_scope(b, 100000)
direct_alloc_scoped(b, 100000)
});

c.bench_function("common/allocator/alloc/cached-no-scope/100000", |b| {
fast_alloc_no_scope(b, 100000)
});
c.bench_function("common/allocator/alloc/cached-scoped/100000", |b| {
fast_alloc_scoped(b, 100000)
});
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_allocator/tests/apis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::black_box;
use swc_allocator::Allocator;
use swc_allocator::MemorySpace;

#[test]
fn direct_alloc_std() {
Expand All @@ -22,7 +22,7 @@ fn direct_alloc_no_scope() {

#[test]
fn direct_alloc_in_scope() {
let allocator = Allocator::default();
let allocator = MemorySpace::default();

allocator.scope(|| {
let mut vec = swc_allocator::vec::Vec::new();
Expand Down

0 comments on commit 0be7c20

Please sign in to comment.