bomalloc 0.3.1

A fragmentation-aware experimental allocator built around address-span ownership
Documentation
#![cfg(any(windows, target_os = "linux"))]

use std::alloc::{GlobalAlloc, Layout, System};
use std::hint::black_box;
use std::ptr::NonNull;
use std::time::Instant;

use bomalloc::{BoAllocator, BoGlobalAllocator, Config};

const SAMPLE_BATCH: usize = 100;

fn percentile(samples: &mut [u128], percentile: usize) -> u128 {
    samples.sort_unstable();
    samples[(samples.len() - 1) * percentile / 100]
}

fn run_bomalloc(layout: Layout, iterations: usize) -> (f64, u128, u128) {
    let allocator = BoAllocator::new(Config::default()).unwrap();
    let mut samples = Vec::with_capacity(iterations / SAMPLE_BATCH);
    let start = Instant::now();
    for batch in 0..(iterations / SAMPLE_BATCH) {
        let sample_start = Instant::now();
        for _ in 0..SAMPLE_BATCH {
            let allocation = allocator.allocate(layout).unwrap();
            let pointer = NonNull::new(allocation.as_ptr() as *mut u8).unwrap();
            black_box(pointer);
            unsafe { allocator.deallocate(pointer, layout) };
        }
        black_box(batch);
        samples.push(sample_start.elapsed().as_nanos() / SAMPLE_BATCH as u128);
    }
    let elapsed = start.elapsed().as_secs_f64();
    let p50 = percentile(&mut samples, 50);
    let p99 = percentile(&mut samples, 99);
    (iterations as f64 / elapsed, p50, p99)
}

fn run_system(layout: Layout, iterations: usize) -> (f64, u128, u128) {
    let mut samples = Vec::with_capacity(iterations / SAMPLE_BATCH);
    let start = Instant::now();
    for batch in 0..(iterations / SAMPLE_BATCH) {
        let sample_start = Instant::now();
        for _ in 0..SAMPLE_BATCH {
            unsafe {
                let pointer = System.alloc(layout);
                assert!(!pointer.is_null());
                black_box(pointer);
                System.dealloc(pointer, layout);
            }
        }
        black_box(batch);
        samples.push(sample_start.elapsed().as_nanos() / SAMPLE_BATCH as u128);
    }
    let elapsed = start.elapsed().as_secs_f64();
    let p50 = percentile(&mut samples, 50);
    let p99 = percentile(&mut samples, 99);
    (iterations as f64 / elapsed, p50, p99)
}

fn run_global_adapter(layout: Layout, iterations: usize) -> (f64, u128, u128) {
    let allocator = BoGlobalAllocator::new();
    let mut samples = Vec::with_capacity(iterations / SAMPLE_BATCH);
    let start = Instant::now();
    for batch in 0..(iterations / SAMPLE_BATCH) {
        let sample_start = Instant::now();
        for _ in 0..SAMPLE_BATCH {
            unsafe {
                let pointer = allocator.alloc(layout);
                assert!(!pointer.is_null());
                black_box(pointer);
                allocator.dealloc(pointer, layout);
            }
        }
        black_box(batch);
        samples.push(sample_start.elapsed().as_nanos() / SAMPLE_BATCH as u128);
    }
    let elapsed = start.elapsed().as_secs_f64();
    let p50 = percentile(&mut samples, 50);
    let p99 = percentile(&mut samples, 99);
    (iterations as f64 / elapsed, p50, p99)
}

fn main() {
    for size in [16, 96, 1024, 16 * 1024, 64 * 1024, 2 * 1024 * 1024] {
        let iterations = if size >= 1024 * 1024 { 2_000 } else { 100_000 };
        let layout = Layout::from_size_align(size, size.next_power_of_two().min(4096)).unwrap();
        let (bo_rate, bo_p50, bo_p99) = run_bomalloc(layout, iterations);
        let (global_rate, global_p50, global_p99) = run_global_adapter(layout, iterations);
        let (sys_rate, sys_p50, sys_p99) = run_system(layout, iterations);
        println!(
            "size={size:>8} bomalloc={bo_rate:>12.0}/s p50={bo_p50:>5}ns p99={bo_p99:>5}ns \
             global={global_rate:>12.0}/s p50={global_p50:>5}ns p99={global_p99:>5}ns \
             system={sys_rate:>12.0}/s p50={sys_p50:>5}ns p99={sys_p99:>5}ns"
        );
    }
}