Skip to content

Commit

Permalink
Add benchmark for from_bitmap_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
lemolatoon committed Aug 21, 2024
1 parent b485e51 commit 83af352
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions benchmarks/benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ fn creation(c: &mut Criterion) {

group.throughput(Throughput::Elements(dataset.bitmaps.iter().map(|rb| rb.len()).sum()));

group.bench_function(BenchmarkId::new("from_bitmap_bytes", &dataset.name), |b| {
let bitmap_bytes = dataset_numbers
.iter()
.map(|bitmap_numbers| {
let max_number = *bitmap_numbers.iter().max().unwrap() as usize;
let mut buf = vec![0u8; max_number.next_multiple_of(8) / 8 + 1];
for n in bitmap_numbers {
let byte = (n / 8) as usize;
let bit = n % 8;
buf[byte] |= 1 << bit;
}
buf
})
.collect::<Vec<_>>();
b.iter(|| {
for bitmap_bytes in &bitmap_bytes {
black_box(RoaringBitmap::from_bitmap_bytes(0, bitmap_bytes));
}
})
});

group.bench_function(BenchmarkId::new("from_sorted_iter", &dataset.name), |b| {
b.iter(|| {
for bitmap_numbers in &dataset_numbers {
Expand Down
2 changes: 1 addition & 1 deletion roaring/src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl RoaringBitmap {
/// assert!(rb.contains(31));
/// ```
pub fn from_bitmap_bytes(offset: u32, bytes: &[u8]) -> RoaringBitmap {
const BITS_IN_ONE_CONTAINER: usize = 8 * size_of::<u64>() * BITMAP_LENGTH;
const BITS_IN_ONE_CONTAINER: usize = u64::BITS as usize * BITMAP_LENGTH;
const BYTE_IN_ONE_CONTAINER: usize = BITS_IN_ONE_CONTAINER / 8;
assert_eq!(offset % BITS_IN_ONE_CONTAINER as u32, 0);
let mut containers = Vec::with_capacity(bytes.len() / BYTE_IN_ONE_CONTAINER);
Expand Down

0 comments on commit 83af352

Please sign in to comment.