From 984dc7c1ed157a3691b77a0982d06390c13ea2ce Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Tue, 27 Apr 2021 18:42:08 +0200 Subject: [PATCH] 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)) } }