|
| 1 | +use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main}; |
| 2 | +use oscars::{Gc, MarkSweepGarbageCollector, cell::GcRefCell}; |
| 3 | + |
| 4 | +fn bench_alloc(c: &mut Criterion) { |
| 5 | + let mut group = c.benchmark_group("mark_sweep_gc/alloc"); |
| 6 | + |
| 7 | + for num_objects in [100, 500, 1000].iter() { |
| 8 | + group.bench_with_input( |
| 9 | + BenchmarkId::from_parameter(num_objects), |
| 10 | + num_objects, |
| 11 | + |b, &num_objects| { |
| 12 | + b.iter_batched( |
| 13 | + || { |
| 14 | + MarkSweepGarbageCollector::default() |
| 15 | + .with_arena_size(65536) |
| 16 | + .with_heap_threshold(262144) |
| 17 | + }, |
| 18 | + |collector| { |
| 19 | + let mut roots = Vec::new(); |
| 20 | + for i in 0..num_objects { |
| 21 | + let root = Gc::new_in(GcRefCell::new(i), &collector); |
| 22 | + roots.push(root); |
| 23 | + } |
| 24 | + black_box(roots.len()) |
| 25 | + }, |
| 26 | + criterion::BatchSize::SmallInput, |
| 27 | + ); |
| 28 | + }, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + group.finish(); |
| 33 | +} |
| 34 | + |
| 35 | +fn bench_collect(c: &mut Criterion) { |
| 36 | + let mut group = c.benchmark_group("mark_sweep_gc/collect"); |
| 37 | + |
| 38 | + for num_objects in [100, 500, 1000].iter() { |
| 39 | + group.bench_with_input( |
| 40 | + BenchmarkId::from_parameter(num_objects), |
| 41 | + num_objects, |
| 42 | + |b, &num_objects| { |
| 43 | + b.iter_batched( |
| 44 | + || { |
| 45 | + let collector = MarkSweepGarbageCollector::default() |
| 46 | + .with_arena_size(65536) |
| 47 | + .with_heap_threshold(262144); |
| 48 | + let mut roots = Vec::new(); |
| 49 | + for i in 0..num_objects { |
| 50 | + let root = Gc::new_in(GcRefCell::new(i), &collector); |
| 51 | + roots.push(root); |
| 52 | + } |
| 53 | + (collector, roots) |
| 54 | + }, |
| 55 | + |(collector, roots)| { |
| 56 | + collector.collect(); |
| 57 | + black_box(roots.len()) |
| 58 | + }, |
| 59 | + criterion::BatchSize::SmallInput, |
| 60 | + ); |
| 61 | + }, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + group.finish(); |
| 66 | +} |
| 67 | + |
| 68 | +criterion_group!(benches, bench_alloc, bench_collect); |
| 69 | +criterion_main!(benches); |
0 commit comments