Skip to content

Commit

Permalink
add slab test
Browse files Browse the repository at this point in the history
  • Loading branch information
w273732573 committed May 29, 2024
1 parent af30887 commit 5a919fa
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions examples/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ use std::{mem, ptr, time::Instant};

use algorithm::{Reinit, Slab};

const ARRAY_SIZE: usize = 10240;
const NUM: usize = usize::MAX;
const ARRAY_SIZE: usize = 102400;
const NUM: usize = usize::MAX - 99999;
const ZERO_ARRAY: [usize; ARRAY_SIZE] = [NUM; ARRAY_SIZE];
struct TestStruct {
array: [usize; ARRAY_SIZE],
size: usize,
val: String,
}

impl Default for TestStruct {
fn default() -> Self {
Self { array: [NUM; ARRAY_SIZE], size: 0 }
Self { array: [NUM; ARRAY_SIZE], size: 0, val: "slab".to_string(), }
}
}

impl Reinit for TestStruct {
#[inline(always)]
fn reinit(&mut self) {
// self.array.fill(0);
self.size = 0;
self.val.clear();
self.val.push_str("slab");
unsafe {
// ptr::write_bytes(&mut self.array[0], u8::MAX, ARRAY_SIZE);
ptr::copy_nonoverlapping(&ZERO_ARRAY[0], &mut self.array[0], ARRAY_SIZE);
// libc::memset(&mut self.array[0] as *mut usize as *mut libc::c_void, 0, ARRAY_SIZE * mem::size_of::<usize>());
}
}
}
Expand All @@ -38,7 +39,7 @@ fn test_speed() {
for i in 0..times {
let (next, test) = slab.get_reinit_next_val();
test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024);
sum = sum.wrapping_add(test.array[10] + test.size);
sum = sum.wrapping_add(test.array[10] + test.size + test.val.len());
slab.remove(next);
}
println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum);
Expand All @@ -51,7 +52,7 @@ fn test_speed() {
let next = slab.insert(TestStruct::default());
let test = &mut slab[next];
test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024);
sum = sum.wrapping_add(test.array[10] + test.size);
sum = sum.wrapping_add(test.array[10] + test.size + test.val.len());
slab.remove(next);
}
println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum);
Expand All @@ -61,7 +62,7 @@ fn test_speed() {
for i in 0..times {
let mut test = TestStruct::default();
test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024);
sum = sum.wrapping_add(test.array[10] + test.size);
sum = sum.wrapping_add(test.array[10] + test.size + test.val.len());
drop(test);
}
println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum);
Expand Down

0 comments on commit 5a919fa

Please sign in to comment.