Skip to content

Instantly share code, notes, and snippets.

@arthurprs
Created October 30, 2017 16:57
Show Gist options
  • Save arthurprs/0c6e7d1f561ac8ec7ff942d399620dcc to your computer and use it in GitHub Desktop.
Save arthurprs/0c6e7d1f561ac8ec7ff942d399620dcc to your computer and use it in GitHub Desktop.
jemalloc bench
#![feature(global_allocator, test)]
extern crate jemallocator;
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
extern crate jemalloc_sys;
fn main() { unsafe {
let ptr = jemalloc_sys::mallocx(100, 0);
let rsz1 = sallocx(ptr as usize, 0);
let rsz2 = jemalloc_sys::nallocx(100, 0);
println!("{:?}, {}, {}", ptr, rsz1, rsz2);
}
}
extern crate libc;
use libc::{c_int, c_void, size_t, c_char};
extern "C" {
#[link_name = "_rjem_sallocx"]
pub fn sallocx(size: usize, flags: c_int) -> size_t;
#[link_name = "_rjem_malloc"]
pub fn malloc(size: size_t) -> *mut c_void;
}
// MALLOCX_ALIGN(a) macro
fn mallocx_align(a: usize) -> c_int {
a.trailing_zeros() as c_int
}
const MIN_ALIGN: usize = 16;
fn align_to_flags(align: usize) -> c_int {
if align <= MIN_ALIGN {
0
} else {
mallocx_align(align)
}
}
extern crate test;
use test::Bencher;
#[bench]
fn roundtrip_w_nallocx(b: &mut Bencher) {
b.iter(|| unsafe {
let ptr = jemalloc_sys::mallocx(100, 0);
let rsz = jemalloc_sys::nallocx(100, 0);
jemalloc_sys::sdallocx(ptr, rsz, 0);
// rsz
});
}
#[bench]
fn roundtrip_w_sallocx(b: &mut Bencher) {
b.iter(|| unsafe {
let ptr = jemalloc_sys::mallocx(100, 0);
let rsz = sallocx(ptr as usize, 0);
jemalloc_sys::sdallocx(ptr, rsz, 0);
// rsz
});
}
#[bench]
fn roundtripx(b: &mut Bencher) {
b.iter(|| unsafe {
let ptr = jemalloc_sys::mallocx(100, 0);
// let rsz = sallocx(ptr as usize, 0);
jemalloc_sys::sdallocx(ptr, 100, 0);
});
}
#[bench]
fn roundtrip(b: &mut Bencher) {
b.iter(|| unsafe {
let ptr = malloc(100);
// let rsz = sallocx(ptr as usize, 0);
jemalloc_sys::sdallocx(ptr, 100, 0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment