From 1373637da18b3bc58eaddae2ad5614cac94137c1 Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Tue, 27 Apr 2021 14:22:33 +0200 Subject: [PATCH 1/2] 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) From 984dc7c1ed157a3691b77a0982d06390c13ea2ce Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Tue, 27 Apr 2021 18:42:08 +0200 Subject: [PATCH 2/2] rewrite roaring codec without byteorder. --- .../roaring_bitmap/bo_roaring_bitmap_codec.rs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 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 25c8afe2f..8d1eb79dd 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,7 +1,7 @@ use std::borrow::Cow; +use std::convert::TryInto; use std::mem::size_of; -use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt}; use roaring::RoaringBitmap; pub struct BoRoaringBitmapCodec; @@ -11,14 +11,12 @@ impl heed::BytesDecode<'_> for BoRoaringBitmapCodec { fn bytes_decode(bytes: &[u8]) -> Option { let mut bitmap = RoaringBitmap::new(); - 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); + + for chunk in bytes.chunks(size_of::()) { + let bytes = chunk.try_into().ok()?; + bitmap.push(u32::from_ne_bytes(bytes)); } + Some(bitmap) } } @@ -27,10 +25,12 @@ impl heed::BytesEncode<'_> for BoRoaringBitmapCodec { type EItem = RoaringBitmap; fn bytes_encode(item: &Self::EItem) -> Option> { - let mut bytes = Vec::with_capacity(item.len() as usize * 4); - for integer in item.iter() { - bytes.write_u32::(integer).ok()?; - } - Some(Cow::Owned(bytes)) + let mut out = Vec::with_capacity(item.len() as usize * size_of::()); + + item.iter() + .map(|i| i.to_ne_bytes()) + .for_each(|bytes| out.extend_from_slice(&bytes)); + + Some(Cow::Owned(out)) } }