diff --git a/src/typing.rs b/src/typing.rs index d6d559e1..1ea03fab 100644 --- a/src/typing.rs +++ b/src/typing.rs @@ -1,6 +1,5 @@ use std::collections::BTreeMap; use crate::env::Env; -use crate::value::Value; #[derive(Debug, Eq, PartialEq)] pub enum PrimitiveType { diff --git a/src/value.rs b/src/value.rs index ac42c9eb..8d64b043 100644 --- a/src/value.rs +++ b/src/value.rs @@ -67,6 +67,7 @@ impl<'a> ByteArrayParser<'a> { Self { bytes, current: 0 } } + #[inline] fn advance(&mut self, n: usize) -> Option<&'a [u8]> { let cur = self.current; if n + cur > self.bytes.len() { @@ -76,9 +77,13 @@ impl<'a> ByteArrayParser<'a> { Some(&self.bytes[cur..cur + n]) } } + + #[inline] fn at_end(&self) -> bool { self.current == self.bytes.len() } + + #[inline] pub fn parse_varint(&mut self) -> Option { let mut u: u64 = 0; let mut shift = 0; @@ -93,6 +98,7 @@ impl<'a> ByteArrayParser<'a> { Some(u) } + #[inline] pub fn parse_value_tag(&mut self) -> Option { use ValueTag::*; @@ -114,9 +120,12 @@ impl<'a> ByteArrayParser<'a> { } } + #[inline] pub fn compare_varint(&mut self, other: &mut Self) -> Ordering { self.parse_varint().unwrap().cmp(&other.parse_varint().unwrap()) } + + #[inline] pub fn parse_zigzag(&mut self) -> Option { let u = self.parse_varint()?; Some(if u & 1 == 0 { @@ -125,20 +134,29 @@ impl<'a> ByteArrayParser<'a> { -((u >> 1) as i64) - 1 }) } + + #[inline] pub fn compare_zigzag(&mut self, other: &mut Self) -> Ordering { self.parse_zigzag().unwrap().cmp(&other.parse_zigzag().unwrap()) } + + #[inline] pub fn parse_float(&mut self) -> Option { let buf = self.advance(8)?.try_into().ok()?; Some(f64::from_be_bytes(buf)) } + + #[inline] pub fn parse_uuid(&mut self) -> Option { Uuid::from_slice(self.advance(16)?).ok() } + + #[inline] pub fn compare_float(&mut self, other: &mut Self) -> Ordering { OrderedFloat(self.parse_float().unwrap()).cmp(&OrderedFloat(other.parse_float().unwrap())) } // This should first compare UUID version, then for V1, compare the timestamps + #[inline] pub fn compare_uuid(&mut self, other: &mut Self) -> Ordering { let ua = self.parse_uuid().unwrap(); let ub = other.parse_uuid().unwrap(); @@ -158,6 +176,8 @@ impl<'a> ByteArrayParser<'a> { } a4.cmp(b4) } + + #[inline] pub fn parse_string(&mut self) -> Option<&'a str> { let l = self.parse_varint()?; let bytes = self.advance(l as usize)?; @@ -166,6 +186,8 @@ impl<'a> ByteArrayParser<'a> { // } std::str::from_utf8(bytes).ok() } + + #[inline] pub fn compare_string(&mut self, other: &mut Self) -> Ordering { let len_a = self.parse_varint().unwrap(); let len_b = self.parse_varint().unwrap(); @@ -280,6 +302,8 @@ impl ByteArrayBuilder { pub fn new(byte_writer: T) -> Self { Self { byte_writer } } + + #[inline] pub fn build_varint(&mut self, u: u64) { let mut u = u; while u > 0b01111111 { @@ -288,6 +312,8 @@ impl ByteArrayBuilder { } self.byte_writer.write_all(&[u as u8]).unwrap(); } + + #[inline] pub fn build_zigzag(&mut self, i: i64) { let u: u64 = if i >= 0 { (i as u64) << 1 @@ -297,19 +323,28 @@ impl ByteArrayBuilder { }; self.build_varint(u); } + + #[inline] pub fn build_float(&mut self, f: f64) { self.byte_writer.write_all(&f.to_be_bytes()).unwrap(); } + + #[inline] pub fn build_uuid(&mut self, u: Uuid) { self.byte_writer.write_all(u.as_bytes()).unwrap(); } + + #[inline] pub fn build_string(&mut self, s: &str) { self.build_varint(s.len() as u64); self.byte_writer.write_all(s.as_bytes()).unwrap(); } + + #[inline] pub fn build_tag(&mut self, t: ValueTag) { self.byte_writer.write_all(&[t as u8]).unwrap(); } + pub fn build_value(&mut self, v: &Value) { use ValueTag::*; @@ -354,12 +389,14 @@ impl ByteArrayBuilder { } } } + pub fn build_list(&mut self, l: &[Value]) { self.build_varint(l.len() as u64); for el in l { self.build_value(el); } } + pub fn build_dict(&mut self, d: &BTreeMap, Value>) { self.build_varint(d.len() as u64); for (k, v) in d {