optimize roaring codec

This commit is contained in:
Marin Postma 2021-04-27 14:22:33 +02:00
parent 2f5e61bacb
commit 1373637da1
No known key found for this signature in database
GPG Key ID: D5241F0C0C865F30

View File

@ -1,4 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::mem::size_of;
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
@ -7,9 +9,14 @@ pub struct BoRoaringBitmapCodec;
impl heed::BytesDecode<'_> for BoRoaringBitmapCodec { impl heed::BytesDecode<'_> for BoRoaringBitmapCodec {
type DItem = RoaringBitmap; type DItem = RoaringBitmap;
fn bytes_decode(mut bytes: &[u8]) -> Option<Self::DItem> { fn bytes_decode(bytes: &[u8]) -> Option<Self::DItem> {
let mut bitmap = RoaringBitmap::new(); let mut bitmap = RoaringBitmap::new();
while let Ok(integer) = bytes.read_u32::<NativeEndian>() { let num_u32 = bytes.len() / size_of::<u32>();
for i in 0..num_u32 {
let start = i * size_of::<u32>();
let end = (i + 1) * size_of::<u32>();
let mut bytes = bytes.get(start..end)?;
let integer = bytes.read_u32::<NativeEndian>().ok()?;
bitmap.insert(integer); bitmap.insert(integer);
} }
Some(bitmap) Some(bitmap)