#![feature(allocator_api)]
#![feature(slice_ptr_get)]
#![feature(nonnull_slice_from_raw_parts)]
use std::{
alloc::{Allocator, Layout},
time::Instant,
};
use good_memory_allocator::DEFAULT_SMALLBINS_AMOUNT;
use rand::Rng;
use simple_chunk_allocator::{GlobalChunkAllocator, DEFAULT_CHUNK_SIZE};
const BENCH_DURATION: f64 = 10.0;
const HEAP_SIZE: usize = 0xa000000;
static mut HEAP_MEMORY: PageAlignedBytes<HEAP_SIZE> = PageAlignedBytes([0; HEAP_SIZE]);
const CHUNK_COUNT: usize = HEAP_SIZE / DEFAULT_CHUNK_SIZE;
const BITMAP_SIZE: usize = CHUNK_COUNT / 8;
static mut HEAP_BITMAP_MEMORY: PageAlignedBytes<BITMAP_SIZE> = PageAlignedBytes([0; BITMAP_SIZE]);
fn main() {
let chunk_allocator = unsafe {
GlobalChunkAllocator::<DEFAULT_CHUNK_SIZE>::new(
HEAP_MEMORY.0.as_mut_slice(),
HEAP_BITMAP_MEMORY.0.as_mut_slice(),
)
};
let bench_res_1 = benchmark_allocator(&mut chunk_allocator.allocator_api_glue());
let mut linked_list_allocator = unsafe {
linked_list_allocator::LockedHeap::new(HEAP_MEMORY.0.as_mut_ptr() as _, HEAP_SIZE)
};
let bench_res_2 = benchmark_allocator(&mut linked_list_allocator);
let mut galloc_allocator =
good_memory_allocator::SpinLockedAllocator::<DEFAULT_SMALLBINS_AMOUNT>::empty();
unsafe {
galloc_allocator.init(HEAP_MEMORY.0.as_ptr() as usize, HEAP_SIZE);
}
let bench_res_3 = benchmark_allocator(&mut galloc_allocator);
print_bench_results("Chunk Allocator", &bench_res_1);
println!();
print_bench_results("Linked List Allocator", &bench_res_2);
println!();
print_bench_results("Galloc", &bench_res_3);
}
fn benchmark_allocator(alloc: &mut dyn Allocator) -> BenchRunResults {
let now_fn = || unsafe { x86::time::rdtscp().0 };
let mut all_allocations = Vec::new();
let mut all_deallocations = Vec::new();
let mut all_alloc_measurements = Vec::new();
let powers_of_two = [1, 2, 4, 8, 16, 32, 64];
let mut rng = rand::thread_rng();
let bench_begin_time = Instant::now();
while bench_begin_time.elapsed().as_secs_f64() <= BENCH_DURATION {
let alignment_i = rng.gen_range(0..powers_of_two.len());
let size = rng.gen_range(64..16384);
let layout = Layout::from_size_align(size, powers_of_two[alignment_i]).unwrap();
let alloc_begin = now_fn();
let alloc_res = alloc.allocate(layout);
let alloc_ticks = now_fn() - alloc_begin;
all_alloc_measurements.push(alloc_ticks);
all_allocations.push(Some((layout, alloc_res)));
let count_all_allocations_not_freed_yet =
all_allocations.iter().filter(|x| x.is_some()).count();
let count_allocations_to_free =
if count_all_allocations_not_freed_yet > 10 && rng.gen_range(0..10) == 0 {
7
} else {
0
};
all_allocations
.iter_mut()
.filter(|x| x.is_some())
.map(|x| x.take().unwrap())
.filter(|(_, res)| res.is_ok())
.map(|(layout, res)| (layout, res.unwrap()))
.take(count_allocations_to_free)
.for_each(|(layout, allocation)| unsafe {
all_deallocations.push((layout, allocation));
alloc.deallocate(allocation.as_non_null_ptr(), layout);
});
}
all_alloc_measurements.sort_by(|x1, x2| x1.cmp(x2));
BenchRunResults {
allocation_attempts: all_allocations.len() as _,
successful_allocations: all_allocations
.iter()
.filter(|x| x.is_some())
.map(|x| x.as_ref().unwrap())
.map(|(_layout, res)| res.is_ok())
.count() as _,
deallocations: all_deallocations.len() as _,
allocation_measurements: all_alloc_measurements,
}
}
fn print_bench_results(bench_name: &str, res: &BenchRunResults) {
println!("RESULTS OF BENCHMARK: {bench_name}");
println!(
" {:6} allocations, {:6} successful_allocations, {:6} deallocations",
res.allocation_attempts, res.successful_allocations, res.deallocations
);
println!(
" median={:6} ticks, average={:6} ticks, min={:6} ticks, max={:6} ticks",
res.allocation_measurements[res.allocation_measurements.len() / 2],
res.allocation_measurements.iter().sum::<u64>()
/ (res.allocation_measurements.len() as u64),
res.allocation_measurements.iter().min().unwrap(),
res.allocation_measurements.iter().max().unwrap(),
);
}
struct BenchRunResults {
allocation_attempts: u64,
successful_allocations: u64,
deallocations: u64,
allocation_measurements: Vec<u64>,
}
#[repr(align(4096))]
struct PageAlignedBytes<const N: usize>([u8; N]);