|
| 1 | +use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main}; |
| 2 | +use oscars::collectors::mark_sweep_arena2::{ |
| 3 | + Finalize, Gc as OscarsGc, MarkSweepGarbageCollector, Trace, TraceColor, |
| 4 | + cell::GcRefCell as OscarsGcRefCell, |
| 5 | +}; |
| 6 | + |
| 7 | +use boa_gc::{Gc as BoaGc, GcRefCell as BoaGcRefCell, force_collect as boa_force_collect}; |
| 8 | + |
| 9 | +fn bench_alloc(c: &mut Criterion) { |
| 10 | + let mut group = c.benchmark_group("gc_node_allocation"); |
| 11 | + |
| 12 | + for size in [10, 100, 1000].iter() { |
| 13 | + group.bench_with_input(BenchmarkId::new("arena2", size), size, |b, &size| { |
| 14 | + let collector = MarkSweepGarbageCollector::default() |
| 15 | + .with_arena_size(65536) |
| 16 | + .with_heap_threshold(262144); |
| 17 | + |
| 18 | + b.iter(|| { |
| 19 | + let mut roots = Vec::new(); |
| 20 | + for i in 0..size { |
| 21 | + let root = OscarsGc::new_in(OscarsGcRefCell::new(i), &collector); |
| 22 | + roots.push(root); |
| 23 | + } |
| 24 | + black_box(roots.len()) |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + group.bench_with_input(BenchmarkId::new("boa_gc", size), size, |b, &size| { |
| 29 | + b.iter_batched( |
| 30 | + || { |
| 31 | + boa_force_collect(); |
| 32 | + }, |
| 33 | + |()| { |
| 34 | + let mut gcs = Vec::new(); |
| 35 | + for i in 0..size { |
| 36 | + let gc = BoaGc::new(BoaGcRefCell::new(i)); |
| 37 | + gcs.push(gc); |
| 38 | + } |
| 39 | + black_box(gcs.len()) |
| 40 | + }, |
| 41 | + criterion::BatchSize::SmallInput, |
| 42 | + ); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + group.finish(); |
| 47 | +} |
| 48 | + |
| 49 | +fn bench_collection(c: &mut Criterion) { |
| 50 | + let mut group = c.benchmark_group("gc_collection_pause"); |
| 51 | + |
| 52 | + for size in [100, 500, 1000].iter() { |
| 53 | + group.bench_with_input(BenchmarkId::new("arena2", size), size, |b, &size| { |
| 54 | + let collector = MarkSweepGarbageCollector::default() |
| 55 | + .with_arena_size(65536) |
| 56 | + .with_heap_threshold(262144); |
| 57 | + |
| 58 | + b.iter(|| { |
| 59 | + let mut roots = Vec::new(); |
| 60 | + for i in 0..size { |
| 61 | + let root = OscarsGc::new_in(OscarsGcRefCell::new(i), &collector); |
| 62 | + roots.push(root); |
| 63 | + } |
| 64 | + // let half be garbage |
| 65 | + roots.truncate(size / 2); |
| 66 | + collector.collect(); |
| 67 | + black_box(roots.len()) |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + group.bench_with_input(BenchmarkId::new("boa_gc", size), size, |b, &size| { |
| 72 | + b.iter(|| { |
| 73 | + let mut gcs = Vec::new(); |
| 74 | + for i in 0..size { |
| 75 | + let gc = BoaGc::new(BoaGcRefCell::new(i)); |
| 76 | + gcs.push(gc); |
| 77 | + } |
| 78 | + gcs.truncate(size / 2); |
| 79 | + boa_force_collect(); |
| 80 | + black_box(gcs.len()) |
| 81 | + }); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + group.finish(); |
| 86 | +} |
| 87 | + |
| 88 | +fn bench_mixed(c: &mut Criterion) { |
| 89 | + let mut group = c.benchmark_group("mixed_workload"); |
| 90 | + |
| 91 | + group.bench_function("arena2", |b| { |
| 92 | + let collector = MarkSweepGarbageCollector::default() |
| 93 | + .with_arena_size(65536) |
| 94 | + .with_heap_threshold(131072); |
| 95 | + |
| 96 | + b.iter(|| { |
| 97 | + let mut roots = Vec::new(); |
| 98 | + |
| 99 | + for i in 0..100 { |
| 100 | + let root = OscarsGc::new_in(OscarsGcRefCell::new(i), &collector); |
| 101 | + roots.push(root); |
| 102 | + } |
| 103 | + collector.collect(); |
| 104 | + |
| 105 | + for i in 100..200 { |
| 106 | + let root = OscarsGc::new_in(OscarsGcRefCell::new(i), &collector); |
| 107 | + roots.push(root); |
| 108 | + } |
| 109 | + collector.collect(); |
| 110 | + |
| 111 | + black_box(roots.len()) |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + group.bench_function("boa_gc", |b| { |
| 116 | + b.iter(|| { |
| 117 | + let mut gcs = Vec::new(); |
| 118 | + |
| 119 | + for i in 0..100 { |
| 120 | + let gc = BoaGc::new(BoaGcRefCell::new(i)); |
| 121 | + gcs.push(gc); |
| 122 | + } |
| 123 | + boa_force_collect(); |
| 124 | + |
| 125 | + for i in 100..200 { |
| 126 | + let gc = BoaGc::new(BoaGcRefCell::new(i)); |
| 127 | + gcs.push(gc); |
| 128 | + } |
| 129 | + boa_force_collect(); |
| 130 | + |
| 131 | + black_box(gcs.len()) |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + group.finish(); |
| 136 | +} |
| 137 | + |
| 138 | +fn bench_pressure(c: &mut Criterion) { |
| 139 | + let mut group = c.benchmark_group("memory_pressure"); |
| 140 | + |
| 141 | + group.bench_function("arena2", |b| { |
| 142 | + let collector = MarkSweepGarbageCollector::default() |
| 143 | + .with_arena_size(32768) |
| 144 | + .with_heap_threshold(65536); |
| 145 | + |
| 146 | + b.iter(|| { |
| 147 | + let mut live = Vec::new(); |
| 148 | + |
| 149 | + for round in 0..10 { |
| 150 | + for i in 0..50 { |
| 151 | + let obj = OscarsGc::new_in(OscarsGcRefCell::new(round * 100 + i), &collector); |
| 152 | + if i % 10 == 0 { |
| 153 | + live.push(obj); |
| 154 | + } |
| 155 | + } |
| 156 | + collector.collect(); |
| 157 | + } |
| 158 | + |
| 159 | + black_box(live.len()) |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + group.bench_function("boa_gc", |b| { |
| 164 | + b.iter(|| { |
| 165 | + let mut live = Vec::new(); |
| 166 | + |
| 167 | + for round in 0..10 { |
| 168 | + for i in 0..50 { |
| 169 | + let obj = BoaGc::new(BoaGcRefCell::new(round * 100 + i)); |
| 170 | + if i % 10 == 0 { |
| 171 | + live.push(obj); |
| 172 | + } |
| 173 | + } |
| 174 | + boa_force_collect(); |
| 175 | + } |
| 176 | + |
| 177 | + black_box(live.len()) |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + group.finish(); |
| 182 | +} |
| 183 | + |
| 184 | +criterion_group!( |
| 185 | + benches, |
| 186 | + bench_alloc, |
| 187 | + bench_collection, |
| 188 | + bench_mixed, |
| 189 | + bench_pressure, |
| 190 | +); |
| 191 | + |
| 192 | +criterion_main!(benches); |
0 commit comments