Created
August 19, 2022 21:38
-
-
Save alessandrod/a80788429873a4b9caa6aa53a82e0b2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(bench_black_box)] | |
use std::hint::black_box; | |
use jemallocator::Jemalloc; | |
#[global_allocator] | |
static GLOBAL: Jemalloc = Jemalloc; | |
const PAGE_SIZE: usize = 4096; | |
const N_BUFS: usize = 64; | |
const STRIDE: usize = PAGE_SIZE * N_BUFS; | |
const ALLOCS: usize = 100; | |
const TEST_ITERATIONS: usize = 1000; | |
#[inline(never)] | |
fn run_malloc_memset() { | |
for i in 0..ALLOCS { | |
let mut v = black_box(Vec::new()); | |
v.resize(STRIDE, 0); | |
use_mem(i, &mut v); | |
} | |
} | |
#[inline(never)] | |
fn run_calloc() { | |
for i in 0..ALLOCS { | |
let mut v = black_box(vec![0u8; STRIDE]); | |
use_mem(i, &mut v); | |
} | |
} | |
#[inline(never)] | |
fn run_calloc_slab() { | |
let mut v = black_box(vec![0u8; STRIDE * ALLOCS]); | |
for i in 0..ALLOCS { | |
let mut v = &mut v[i * STRIDE..(i + 1) * STRIDE]; | |
use_mem(i, &mut v); | |
} | |
} | |
#[inline(never)] | |
fn run_calloc_free_list() { | |
let mut bufs = Vec::new(); | |
for i in 0..ALLOCS { | |
let mut v = black_box(vec![0u8; STRIDE]); | |
use_mem(i, &mut v); | |
bufs.push(v); | |
} | |
} | |
#[inline(never)] | |
fn use_mem(i: usize, buf: &mut [u8]) { | |
if i <= ALLOCS / 2 { | |
let _ = black_box(buf[0]); | |
} | |
} | |
fn main() { | |
use rayon::prelude::*; | |
rayon::ThreadPoolBuilder::new() | |
.num_threads(64) | |
.build_global() | |
.unwrap(); | |
match std::env::args().nth(1).expect("missing alloc").as_ref() { | |
"malloc_memset" => (0..TEST_ITERATIONS) | |
.into_par_iter() | |
.for_each(|_| run_malloc_memset()), | |
"calloc" => (0..TEST_ITERATIONS) | |
.into_par_iter() | |
.for_each(|_| run_calloc()), | |
"calloc_slab" => (0..TEST_ITERATIONS) | |
.into_par_iter() | |
.for_each(|_| run_calloc_slab()), | |
"calloc_free_list" => (0..TEST_ITERATIONS) | |
.into_par_iter() | |
.for_each(|_| run_calloc_free_list()), | |
_ => panic!(), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment