Skip to content

Instantly share code, notes, and snippets.

@Lisoph
Created April 6, 2018 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lisoph/08d9111fd09302e651363cc5f0d585e1 to your computer and use it in GitHub Desktop.
Save Lisoph/08d9111fd09302e651363cc5f0d585e1 to your computer and use it in GitHub Desktop.
Rust allocator locality tester. Analyses whether the current allocator allocates successive allocations sequentially in memory.
const PAYLOAD_SIZE: usize = 128;
const TESTS: usize = 20;
fn main() {
let mut items: [Box<[u8; PAYLOAD_SIZE]>; TESTS] = unsafe { std::mem::uninitialized() };
for item in items.iter_mut() {
unsafe {
std::ptr::write(item as *mut _, Box::new([0u8; PAYLOAD_SIZE]));
}
}
println!("Distances (in bytes) between {} sequentially allocated objects with {} bytes in size:", items.len(), PAYLOAD_SIZE);
for (i, item) in items.iter().enumerate() {
let item = &**item as *const _ as isize;
print!("{:02}: {:#X}", i + 1, item);
if i > 0 {
let prev_item = &*items[i - 1] as *const _ as isize;
let diff = item - prev_item;
let sign = if diff < 0 { "-" } else { " " };
print!(": {}{:#X} ({})", sign, diff.abs(), diff);
}
println!();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment