|
| 1 | +use std::hint::black_box; |
| 2 | + |
| 3 | +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; |
| 4 | +use hyperlight_common::virtq::{BufferPool, BufferProvider}; |
| 5 | + |
| 6 | +// Helper to create a pool for benchmarking |
| 7 | +fn make_pool<const L: usize, const U: usize>(size: usize) -> BufferPool<L, U> { |
| 8 | + let base = 0x10000; |
| 9 | + BufferPool::<L, U>::new(base, size).unwrap() |
| 10 | +} |
| 11 | + |
| 12 | +// Single allocation performance |
| 13 | +fn bench_alloc_single(c: &mut Criterion) { |
| 14 | + let mut group = c.benchmark_group("alloc_single"); |
| 15 | + |
| 16 | + for size in [64, 128, 256, 512, 1024, 1500, 4096].iter() { |
| 17 | + group.throughput(Throughput::Elements(1)); |
| 18 | + group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { |
| 19 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 20 | + b.iter(|| { |
| 21 | + let alloc = pool.alloc(black_box(size)).unwrap(); |
| 22 | + pool.dealloc(alloc).unwrap(); |
| 23 | + }); |
| 24 | + }); |
| 25 | + } |
| 26 | + group.finish(); |
| 27 | +} |
| 28 | + |
| 29 | +// LIFO recycling |
| 30 | +fn bench_alloc_lifo(c: &mut Criterion) { |
| 31 | + let mut group = c.benchmark_group("alloc_lifo"); |
| 32 | + |
| 33 | + for size in [256, 1500, 4096].iter() { |
| 34 | + group.throughput(Throughput::Elements(100)); |
| 35 | + group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { |
| 36 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 37 | + b.iter(|| { |
| 38 | + for _ in 0..100 { |
| 39 | + let alloc = pool.alloc(black_box(size)).unwrap(); |
| 40 | + pool.dealloc(alloc).unwrap(); |
| 41 | + } |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + group.finish(); |
| 46 | +} |
| 47 | + |
| 48 | +// Fragmented allocation worst case |
| 49 | +fn bench_alloc_fragmented(c: &mut Criterion) { |
| 50 | + let mut group = c.benchmark_group("alloc_fragmented"); |
| 51 | + |
| 52 | + group.bench_function("fragmented_256", |b| { |
| 53 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 54 | + |
| 55 | + // Create fragmentation pattern: allocate many, free every other |
| 56 | + let mut allocations = Vec::new(); |
| 57 | + for _ in 0..100 { |
| 58 | + allocations.push(pool.alloc(128).unwrap()); |
| 59 | + } |
| 60 | + for i in (0..100).step_by(2) { |
| 61 | + pool.dealloc(allocations[i]).unwrap(); |
| 62 | + } |
| 63 | + |
| 64 | + b.iter(|| { |
| 65 | + let alloc = pool.alloc(black_box(256)).unwrap(); |
| 66 | + pool.dealloc(alloc).unwrap(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + group.finish(); |
| 71 | +} |
| 72 | + |
| 73 | +// Realloc operations |
| 74 | +fn bench_realloc(c: &mut Criterion) { |
| 75 | + let mut group = c.benchmark_group("realloc"); |
| 76 | + |
| 77 | + // In-place grow (same tier) |
| 78 | + group.bench_function("grow_inplace", |b| { |
| 79 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 80 | + b.iter(|| { |
| 81 | + let alloc = pool.alloc(256).unwrap(); |
| 82 | + let grown = pool.resize(alloc, black_box(512)).unwrap(); |
| 83 | + pool.dealloc(grown).unwrap(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + // Relocate grow (cross tier) |
| 88 | + group.bench_function("grow_relocate", |b| { |
| 89 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 90 | + b.iter(|| { |
| 91 | + let alloc = pool.alloc(128).unwrap(); |
| 92 | + // Block in-place growth |
| 93 | + let blocker = pool.alloc(256).unwrap(); |
| 94 | + let grown = pool.resize(alloc, black_box(1500)).unwrap(); |
| 95 | + pool.dealloc(grown).unwrap(); |
| 96 | + pool.dealloc(blocker).unwrap(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + // Shrink |
| 101 | + group.bench_function("shrink", |b| { |
| 102 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 103 | + b.iter(|| { |
| 104 | + let alloc = pool.alloc(1500).unwrap(); |
| 105 | + let shrunk = pool.resize(alloc, black_box(256)).unwrap(); |
| 106 | + pool.dealloc(shrunk).unwrap(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + group.finish(); |
| 111 | +} |
| 112 | + |
| 113 | +// Free performance |
| 114 | +fn bench_free(c: &mut Criterion) { |
| 115 | + let mut group = c.benchmark_group("free"); |
| 116 | + |
| 117 | + for size in [256, 1500, 4096].iter() { |
| 118 | + group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { |
| 119 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 120 | + b.iter(|| { |
| 121 | + let alloc = pool.alloc(size).unwrap(); |
| 122 | + pool.dealloc(black_box(alloc)).unwrap(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + group.finish(); |
| 128 | +} |
| 129 | + |
| 130 | +// Cursor optimization |
| 131 | +fn bench_last_free_run(c: &mut Criterion) { |
| 132 | + let mut group = c.benchmark_group("last_free_run"); |
| 133 | + |
| 134 | + // With cursor optimization (LIFO) |
| 135 | + group.bench_function("lifo_pattern", |b| { |
| 136 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 137 | + b.iter(|| { |
| 138 | + let alloc = pool.alloc(256).unwrap(); |
| 139 | + pool.dealloc(alloc).unwrap(); |
| 140 | + let alloc2 = pool.alloc(black_box(256)).unwrap(); |
| 141 | + pool.dealloc(alloc2).unwrap(); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + // Without cursor benefit (FIFO-like) |
| 146 | + group.bench_function("fifo_pattern", |b| { |
| 147 | + let pool = make_pool::<256, 4096>(4 * 1024 * 1024); |
| 148 | + let mut queue = Vec::new(); |
| 149 | + |
| 150 | + // Pre-fill queue |
| 151 | + for _ in 0..10 { |
| 152 | + queue.push(pool.alloc(256).unwrap()); |
| 153 | + } |
| 154 | + |
| 155 | + b.iter(|| { |
| 156 | + // FIFO: free oldest, allocate new |
| 157 | + let old = queue.remove(0); |
| 158 | + pool.dealloc(old).unwrap(); |
| 159 | + queue.push(pool.alloc(black_box(256)).unwrap()); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + group.finish(); |
| 164 | +} |
| 165 | + |
| 166 | +criterion_group!( |
| 167 | + benches, |
| 168 | + bench_alloc_single, |
| 169 | + bench_alloc_lifo, |
| 170 | + bench_alloc_fragmented, |
| 171 | + bench_realloc, |
| 172 | + bench_free, |
| 173 | + bench_last_free_run, |
| 174 | +); |
| 175 | + |
| 176 | +criterion_main!(benches); |
0 commit comments