From 1373637da18b3bc58eaddae2ad5614cac94137c1 Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Tue, 27 Apr 2021 14:22:33 +0200 Subject: [PATCH] optimize roaring codec --- .../roaring_bitmap/bo_roaring_bitmap_codec.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/milli/src/heed_codec/roaring_bitmap/bo_roaring_bitmap_codec.rs b/milli/src/heed_codec/roaring_bitmap/bo_roaring_bitmap_codec.rs index 7ceb69f9a..25c8afe2f 100644 --- a/milli/src/heed_codec/roaring_bitmap/bo_roaring_bitmap_codec.rs +++ b/milli/src/heed_codec/roaring_bitmap/bo_roaring_bitmap_codec.rs @@ -1,4 +1,6 @@ use std::borrow::Cow; +use std::mem::size_of; + use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt}; use roaring::RoaringBitmap; @@ -7,9 +9,14 @@ pub struct BoRoaringBitmapCodec; impl heed::BytesDecode<'_> for BoRoaringBitmapCodec { type DItem = RoaringBitmap; - fn bytes_decode(mut bytes: &[u8]) -> Option { + fn bytes_decode(bytes: &[u8]) -> Option { let mut bitmap = RoaringBitmap::new(); - while let Ok(integer) = bytes.read_u32::() { + let num_u32 = bytes.len() / size_of::(); + for i in 0..num_u32 { + let start = i * size_of::(); + let end = (i + 1) * size_of::(); + let mut bytes = bytes.get(start..end)?; + let integer = bytes.read_u32::().ok()?; bitmap.insert(integer); } Some(bitmap)