From 83af352926e8cee331028525103bddc727a75bd4 Mon Sep 17 00:00:00 2001 From: lemolatoon <63438515+lemolatoon@users.noreply.github.com> Date: Tue, 20 Aug 2024 19:27:20 -0500 Subject: [PATCH] Add benchmark for from_bitmap_bytes --- benchmarks/benches/lib.rs | 21 +++++++++++++++++++++ roaring/src/bitmap/inherent.rs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/benchmarks/benches/lib.rs b/benchmarks/benches/lib.rs index b5c92882..01d17b13 100644 --- a/benchmarks/benches/lib.rs +++ b/benchmarks/benches/lib.rs @@ -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::>(); + 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 { diff --git a/roaring/src/bitmap/inherent.rs b/roaring/src/bitmap/inherent.rs index f954d0ef..a7bc303b 100644 --- a/roaring/src/bitmap/inherent.rs +++ b/roaring/src/bitmap/inherent.rs @@ -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::() * 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);