diff --git a/tree-buf-macros/src/decode.rs b/tree-buf-macros/src/decode.rs index 9ba0909..8b78e3a 100644 --- a/tree-buf-macros/src/decode.rs +++ b/tree-buf-macros/src/decode.rs @@ -171,10 +171,9 @@ fn fill_decode_skeleton( fn impl_enum_decode(ast: &DeriveInput, data_enum: &DataEnum) -> TokenStream { let ident = &ast.ident; - let mut array_fields = Vec::new(); - array_fields.push(quote! { + let mut array_fields = vec![quote! { tree_buf_discriminant: ::DecoderArray - }); + }]; let mut new_matches = Vec::new(); let mut new_inits = Vec::new(); diff --git a/tree-buf-macros/src/encode.rs b/tree-buf-macros/src/encode.rs index 4f040b5..45ce100 100644 --- a/tree-buf-macros/src/encode.rs +++ b/tree-buf-macros/src/encode.rs @@ -121,12 +121,11 @@ fn impl_enum_encode(ast: &DeriveInput, data_enum: &DataEnum) -> TokenStream { // For each variant, possibly it's own encoder struct if it's a tuple or struct sort of DataEnum // A discriminant - let mut array_fields = Vec::new(); - array_fields.push(quote! { + let mut array_fields = vec![quote! { // TODO: (Performance) have the size scale to the number of variants tree_buf_discriminant: ::EncoderArray, tree_buf_next_discriminant: u64 - }); + }]; let mut array_matches = Vec::new(); let mut root_matches = Vec::new(); diff --git a/tree-buf-macros/src/lib.rs b/tree-buf-macros/src/lib.rs index 5b18bc7..d50a662 100644 --- a/tree-buf-macros/src/lib.rs +++ b/tree-buf-macros/src/lib.rs @@ -3,9 +3,9 @@ extern crate syn; #[macro_use] extern crate quote; -mod utils; mod decode; mod encode; +mod utils; use { decode::impl_decode_macro, @@ -13,8 +13,6 @@ use { syn::{parse_macro_input, DeriveInput}, }; - - #[proc_macro_derive(Encode)] pub fn encode_macro_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = parse_macro_input!(input as DeriveInput); @@ -27,4 +25,4 @@ pub fn decode_macro_derive(input: proc_macro::TokenStream) -> proc_macro::TokenS let ast = parse_macro_input!(input as DeriveInput); let output = impl_decode_macro(&ast); proc_macro::TokenStream::from(output) -} \ No newline at end of file +} diff --git a/tree-buf/benches/float_list.rs b/tree-buf/benches/float_list.rs index 6c58fbd..bf015ec 100644 --- a/tree-buf/benches/float_list.rs +++ b/tree-buf/benches/float_list.rs @@ -1,5 +1,4 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use tree_buf::prelude::*; /* diff --git a/tree-buf/src/experimental/mod.rs b/tree-buf/src/experimental/mod.rs index 0a877ab..4e2b8ab 100644 --- a/tree-buf/src/experimental/mod.rs +++ b/tree-buf/src/experimental/mod.rs @@ -3,7 +3,7 @@ //! or resulting file may change unexpectedly with any version. Even so, it is worth //! poking around. +pub mod options; #[doc(hidden)] pub mod scratch; pub mod stats; -pub mod options; \ No newline at end of file diff --git a/tree-buf/src/experimental/options.rs b/tree-buf/src/experimental/options.rs index 8075b77..09602c8 100644 --- a/tree-buf/src/experimental/options.rs +++ b/tree-buf/src/experimental/options.rs @@ -5,16 +5,9 @@ use crate::prelude::*; -pub use crate::internal::options::{ - DisableParallel, - EnableParallel, - LosslessFloat, - LossyFloatTolerance, - EncodeOptions, DecodeOptions -}; - -pub use crate::{encode_options, decode_options}; +pub use crate::internal::options::{DecodeOptions, DisableParallel, EnableParallel, EncodeOptions, LosslessFloat, LossyFloatTolerance}; +pub use crate::{decode_options, encode_options}; #[cfg(feature = "encode")] pub fn encode_with_options(value: &T, options: &impl EncodeOptions) -> Vec { @@ -38,4 +31,4 @@ pub fn decode_with_options(bytes: &[u8], options: &impl DecodeOpti profile_fn!(T, decode_with_options); let sticks = decode_root(bytes)?; T::decode(sticks, options) -} \ No newline at end of file +} diff --git a/tree-buf/src/experimental/scratch.rs b/tree-buf/src/experimental/scratch.rs index 1159094..0c64e48 100644 --- a/tree-buf/src/experimental/scratch.rs +++ b/tree-buf/src/experimental/scratch.rs @@ -20,8 +20,9 @@ impl Scratch { /// A re-usable object which may increase performance when encoding over and over again /// in a loop. Avoids allocations +#[must_use] pub fn scratch() -> Scratch { - Scratch { buffers: Default::default() } + Scratch { buffers: Rc::default() } } pub fn encode_into_with_scratch(_value: &T, _scratch: &mut Scratch, _into: &mut Vec) { diff --git a/tree-buf/src/experimental/stats.rs b/tree-buf/src/experimental/stats.rs index d0248e0..4bcc3de 100644 --- a/tree-buf/src/experimental/stats.rs +++ b/tree-buf/src/experimental/stats.rs @@ -12,16 +12,14 @@ struct Path { } impl Path { - fn c(s: &String, x: &impl fmt::Display) -> String { + fn c(s: &str, x: &impl fmt::Display) -> String { let x = format!("{}", x); if s.is_empty() { x + } else if x.is_empty() { + s.to_string() } else { - if x.is_empty() { - s.clone() - } else { - format!("{}.{}", s, x) - } + format!("{}.{}", s, x) } } @@ -59,13 +57,13 @@ impl fmt::Display for SizeBreakdown { by_type.sort_by_key(|i| usize::MAX - i.1.size); writeln!(f, "Largest by path:")?; - for (path, agg) in by_path.iter() { + for (path, agg) in &by_path { writeln!(f, "\t{}\n\t {}\n\t {}", agg.size, path, agg.types)?; } writeln!(f)?; writeln!(f, "Largest by type:")?; - for (t, agg) in by_type.iter() { + for (t, agg) in &by_type { writeln!(f, "\t {}x {} @ {}", agg.count, agg.size, t)?; } @@ -98,22 +96,22 @@ impl SizeBreakdown { } } -fn visit_array(path: Path, branch: &DynArrayBranch, breakdown: &mut SizeBreakdown) { +fn visit_array(path: &Path, branch: &DynArrayBranch, breakdown: &mut SizeBreakdown) { match branch { - DynArrayBranch::ArrayFixed { values, len } => visit_array(path.a(&format!("[{}]", len), &"Array Fixed"), values, breakdown), + DynArrayBranch::ArrayFixed { values, len } => visit_array(&path.a(&format!("[{}]", len), &"Array Fixed"), values, breakdown), DynArrayBranch::Array { len, values } => { - visit_array(path.a(&"len", &"Array"), len, breakdown); - visit_array(path.a(&"values", &"Array"), values, breakdown); + visit_array(&path.a(&"len", &"Array"), len, breakdown); + visit_array(&path.a(&"values", &"Array"), values, breakdown); } DynArrayBranch::Enum { discriminants, variants } => { - visit_array(path.a(&"discriminants", &"Enum"), discriminants, breakdown); + visit_array(&path.a(&"discriminants", &"Enum"), discriminants, breakdown); for variant in variants.iter() { - visit_array(path.a(&variant.ident, &"Enum"), &variant.data, breakdown); + visit_array(&path.a(&variant.ident, &"Enum"), &variant.data, breakdown); } } DynArrayBranch::Boolean(enc) => match enc { ArrayBool::Packed(b) => breakdown.add(&path, "Packed Boolean", b), - ArrayBool::RLE(_first, runs) => visit_array(path.a(&"runs", &"Bool RLE"), runs, breakdown), + ArrayBool::RLE(_first, runs) => visit_array(&path.a(&"runs", &"Bool RLE"), runs, breakdown), }, DynArrayBranch::Float(f) => match f { ArrayFloat::DoubleGorilla(b) => breakdown.add(&path, "Gorilla", b), @@ -129,60 +127,60 @@ fn visit_array(path: Path, branch: &DynArrayBranch, breakdown: &mut SizeBreakdow ArrayIntegerEncoding::DeltaZig => breakdown.add(&path, "DeltaZig", bytes), }, DynArrayBranch::Map { len, keys, values } => { - visit_array(path.a(&"len", &"Map"), len, breakdown); - visit_array(path.a(&"keys", &"Map"), keys, breakdown); - visit_array(path.a(&"values", &"Map"), values, breakdown); + visit_array(&path.a(&"len", &"Map"), len, breakdown); + visit_array(&path.a(&"keys", &"Map"), keys, breakdown); + visit_array(&path.a(&"values", &"Map"), values, breakdown); } DynArrayBranch::Object { fields } => { for (name, field) in fields { - visit_array(path.a(name, &"Object"), field, breakdown); + visit_array(&path.a(name, &"Object"), field, breakdown); } } DynArrayBranch::RLE { runs, values } => { - visit_array(path.a(&"runs", &"RLE"), runs, breakdown); - visit_array(path.a(&"values", &"RLE"), values, breakdown); + visit_array(&path.a(&"runs", &"RLE"), runs, breakdown); + visit_array(&path.a(&"values", &"RLE"), values, breakdown); } DynArrayBranch::Dictionary { indices, values } => { - visit_array(path.a(&"indices", &"Dictionary"), indices, breakdown); - visit_array(path.a(&"values", &"Dictionary"), values, breakdown); + visit_array(&path.a(&"indices", &"Dictionary"), indices, breakdown); + visit_array(&path.a(&"values", &"Dictionary"), values, breakdown); } DynArrayBranch::String(b) => breakdown.add(&path, "UTF-8", b), DynArrayBranch::Tuple { fields } => { for (i, field) in fields.iter().enumerate() { - visit_array(path.a(&i, &"Tuple"), field, breakdown); + visit_array(&path.a(&i, &"Tuple"), field, breakdown); } } DynArrayBranch::Nullable { opt, values } => { - visit_array(path.a(&"opt", &"Nullable"), opt, breakdown); - visit_array(path.a(&"values", &"Nullable"), values, breakdown); + visit_array(&path.a(&"opt", &"Nullable"), opt, breakdown); + visit_array(&path.a(&"values", &"Nullable"), values, breakdown); } DynArrayBranch::Void | DynArrayBranch::Map0 | DynArrayBranch::Array0 => {} } } -fn visit(path: Path, branch: &DynRootBranch<'_>, breakdown: &mut SizeBreakdown) { +fn visit(path: &Path, branch: &DynRootBranch<'_>, breakdown: &mut SizeBreakdown) { match branch { DynRootBranch::Object { fields } => { for (name, value) in fields.iter() { - visit(path.a(name, &"Object"), value, breakdown); + visit(&path.a(name, &"Object"), value, breakdown); } } - DynRootBranch::Enum { discriminant, value } => visit(path.a(discriminant, &"Enum"), value, breakdown), + DynRootBranch::Enum { discriminant, value } => visit(&path.a(discriminant, &"Enum"), value, breakdown), DynRootBranch::Map { len: _, keys, values } => { - visit_array(path.a(&"keys", &"Map"), keys, breakdown); - visit_array(path.a(&"values", &"Values"), values, breakdown); + visit_array(&path.a(&"keys", &"Map"), keys, breakdown); + visit_array(&path.a(&"values", &"Values"), values, breakdown); } DynRootBranch::Tuple { fields } => { for (i, field) in fields.iter().enumerate() { - visit(path.a(&i, &"Tuple"), field, breakdown); + visit(&path.a(&i, &"Tuple"), field, breakdown); } } DynRootBranch::Map1 { key, value } => { - visit(path.a(&"key", &"Map1"), key, breakdown); - visit(path.a(&"value", &"Map1"), value, breakdown); + visit(&path.a(&"key", &"Map1"), key, breakdown); + visit(&path.a(&"value", &"Map1"), value, breakdown); } - DynRootBranch::Array { len, values } => visit_array(path.a(&format!("[{}]", len), &"Array"), values, breakdown), - DynRootBranch::Array1(item) => visit(path.a(&"1", &"Array1"), item, breakdown), + DynRootBranch::Array { len, values } => visit_array(&path.a(&format!("[{}]", len), &"Array"), values, breakdown), + DynRootBranch::Array1(item) => visit(&path.a(&"1", &"Array1"), item, breakdown), DynRootBranch::Boolean(_) | DynRootBranch::Array0 | DynRootBranch::Map0 @@ -269,7 +267,7 @@ pub fn size_breakdown(data: &[u8]) -> DecodeResult { by_type: HashMap::new(), total: data.len(), }; - visit(Path::default(), &root, &mut breakdown); + visit(&Path::default(), &root, &mut breakdown); Ok(format!("{}", breakdown)) } diff --git a/tree-buf/src/internal/branch/array_branch.rs b/tree-buf/src/internal/branch/array_branch.rs index 9998c9f..cb117d5 100644 --- a/tree-buf/src/internal/branch/array_branch.rs +++ b/tree-buf/src/internal/branch/array_branch.rs @@ -1,4 +1,4 @@ -use crate::internal::encodings::varint::*; +use crate::internal::encodings::varint::{decode_prefix_varint, decode_suffix_varint}; use crate::prelude::*; use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; @@ -105,10 +105,13 @@ pub enum DynArrayBranch<'a> { pub fn decode_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize) -> DecodeResult> { let id = ArrayTypeId::decode_next(bytes, offset)?; - use ArrayTypeId::*; + use ArrayTypeId::{ + ArrayFixed, ArrayVar, DeltaZig, Dictionary, DoubleGorilla, Enum, IntPrefixVar, IntSimple16, Map, Nullable, Obj0, Obj1, Obj2, Obj3, Obj4, Obj5, Obj6, Obj7, Obj8, ObjN, + PackedBool, RLEBoolFalse, RLEBoolTrue, Tuple2, Tuple3, Tuple4, Tuple5, Tuple6, Tuple7, Tuple8, TupleN, Utf8, Void, Zfp32, Zfp64, F32, F64, RLE, U8, + }; fn decode_ints<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize, encoding: ArrayIntegerEncoding) -> DecodeResult> { - let bytes = decode_bytes_from_len(bytes, offset, lens)?.into(); + let bytes = decode_bytes_from_len(bytes, offset, lens)?; Ok(DynArrayBranch::Integer(ArrayInteger { bytes, encoding })) } @@ -155,14 +158,13 @@ pub fn decode_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ m TupleN => decode_tuple(decode_prefix_varint(bytes, offset)? as usize + 9, bytes, offset, lens)?, ArrayVar => { let len = decode_next_array(bytes, offset, lens)?; - match len { - DynArrayBranch::Void => DynArrayBranch::Array0, - _ => { - let len = Box::new(len); - let values = decode_next_array(bytes, offset, lens)?; - let values = Box::new(values); - DynArrayBranch::Array { len, values } - } + if let DynArrayBranch::Void = len { + DynArrayBranch::Array0 + } else { + let len = Box::new(len); + let values = decode_next_array(bytes, offset, lens)?; + let values = Box::new(values); + DynArrayBranch::Array { len, values } } } ArrayFixed => { @@ -173,16 +175,15 @@ pub fn decode_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ m } Map => { let len = decode_next_array(bytes, offset, lens)?; - match len { - DynArrayBranch::Void => DynArrayBranch::Map0, - _ => { - let len = Box::new(len); - let keys = decode_next_array(bytes, offset, lens)?; - let keys = Box::new(keys); - let values = decode_next_array(bytes, offset, lens)?; - let values = Box::new(values); - DynArrayBranch::Map { len, keys, values } - } + if let DynArrayBranch::Void = len { + DynArrayBranch::Map0 + } else { + let len = Box::new(len); + let keys = decode_next_array(bytes, offset, lens)?; + let keys = Box::new(keys); + let values = decode_next_array(bytes, offset, lens)?; + let values = Box::new(values); + DynArrayBranch::Map { len, keys, values } } } diff --git a/tree-buf/src/internal/branch/mod.rs b/tree-buf/src/internal/branch/mod.rs index 325e7c4..bdecc0d 100644 --- a/tree-buf/src/internal/branch/mod.rs +++ b/tree-buf/src/internal/branch/mod.rs @@ -118,7 +118,7 @@ pub trait TypeId: Copy + Into + PartialEq + std::fmt::Debug { #[cfg(feature = "decode")] pub fn decode_root(bytes: &[u8]) -> DecodeResult> { profile_fn!(decode_root); - if bytes.len() == 0 { + if bytes.is_empty() { return Ok(DynRootBranch::Void); } let mut lens = bytes.len() - 1; @@ -131,7 +131,7 @@ mod tests { use super::*; use std::convert::{TryFrom, TryInto}; fn convert_all + Into>() { - for i in 0..=255u8 { + for i in 0..=255_u8 { let v: Result = i.try_into(); if let Ok(v) = v { debug_assert_eq!(i, v.into()); diff --git a/tree-buf/src/internal/branch/root_branch.rs b/tree-buf/src/internal/branch/root_branch.rs index e1f972b..cf5a99f 100644 --- a/tree-buf/src/internal/branch/root_branch.rs +++ b/tree-buf/src/internal/branch/root_branch.rs @@ -86,7 +86,11 @@ pub fn decode_next_root<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mu Ok(DynRootBranch::String(s)) } - use RootTypeId::*; + use RootTypeId::{ + Array0, Array1, ArrayN, Enum, False, IntS16, IntS24, IntS32, IntS40, IntS48, IntS56, IntS64, IntS8, IntU16, IntU24, IntU32, IntU40, IntU48, IntU56, IntU64, IntU8, Map, + NaN, NegOne, Obj0, Obj1, Obj2, Obj3, Obj4, Obj5, Obj6, Obj7, Obj8, ObjN, One, Str, Str0, Str1, Str2, Str3, True, Tuple2, Tuple3, Tuple4, Tuple5, Tuple6, Tuple7, Tuple8, + TupleN, Void, Zero, F32, F64, + }; let branch = match id { Void => DynRootBranch::Void, @@ -264,30 +268,30 @@ impl RootInteger { (4, false) => Self::U(u32::from_le_bytes(bytes.try_into().unwrap()).into()), (5, false) => Self::U({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0]; - u64::from_le_bytes(b).into() + u64::from_le_bytes(b) }), (5, true) => Self::S({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0]; - i64::from_le_bytes(b).into() + i64::from_le_bytes(b) }), (6, false) => Self::U({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0]; - u64::from_le_bytes(b).into() + u64::from_le_bytes(b) }), (6, true) => Self::S({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0]; - i64::from_le_bytes(b).into() + i64::from_le_bytes(b) }), (7, false) => Self::U({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], 0]; - u64::from_le_bytes(b).into() + u64::from_le_bytes(b) }), (7, true) => Self::S({ let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], 0]; - i64::from_le_bytes(b).into() + i64::from_le_bytes(b) }), - (8, true) => Self::S(i64::from_le_bytes(bytes.try_into().unwrap()).into()), - (8, false) => Self::U(u64::from_le_bytes(bytes.try_into().unwrap()).into()), + (8, true) => Self::S(i64::from_le_bytes(bytes.try_into().unwrap())), + (8, false) => Self::U(u64::from_le_bytes(bytes.try_into().unwrap())), _ => unreachable!(), }; Ok(ok) diff --git a/tree-buf/src/internal/buffer.rs b/tree-buf/src/internal/buffer.rs index 08fdeb2..e5f85ea 100644 --- a/tree-buf/src/internal/buffer.rs +++ b/tree-buf/src/internal/buffer.rs @@ -21,7 +21,7 @@ pub(crate) struct Buffer { impl Drop for Buffer { fn drop(&mut self) { - unsafe { dealloc(self.base() as *mut u8, LAYOUT) } + unsafe { dealloc(self.base().cast::(), LAYOUT) } } } @@ -48,7 +48,7 @@ impl Buffer { check_buffer::(); unsafe { let ptr = alloc(LAYOUT); - let ptr = transmute(ptr); + let ptr = ptr.cast::(); let ptr = NonNull::new(ptr).expect("Failed to allocate buffer"); Self { ptr, len: 0 } } @@ -107,16 +107,16 @@ impl Buffer { ptr::copy_nonoverlapping(slice.as_ptr(), self.top(), take); } self.len += take; - if take != slice.len() { - Err(&slice[take..]) - } else { + if take == slice.len() { Ok(()) + } else { + Err(&slice[take..]) } } #[inline] fn top(&self) -> *mut T { - unsafe { self.base().offset(self.len as isize) } + unsafe { self.base().add(self.len) } } #[inline] @@ -132,7 +132,7 @@ pub(crate) struct BufferPool { impl BufferPool { pub fn new() -> Self { - Default::default() + Self::default() } /// Returns an empty Buffer. @@ -164,7 +164,7 @@ mod tests { pub fn can_extend() { // Try just a normal extend let mut buffer = Buffer::new(); - let mut data = vec![100u32, 20, 20, 10, 0]; + let mut data = vec![100_u32, 20, 20, 10, 0]; assert_eq!(Ok(()), buffer.try_extend(&data[..])); assert_eq!(&data[..], &buffer[..]); @@ -187,8 +187,8 @@ mod tests { pub fn deref() { let mut buffer = Buffer::new(); - let data = vec![0u8, 1, 255, 12]; - for elem in data.iter() { + let data = vec![0_u8, 1, 255, 12]; + for elem in &data { buffer.try_push(*elem).unwrap(); } diff --git a/tree-buf/src/internal/encoder_decoder.rs b/tree-buf/src/internal/encoder_decoder.rs index f558e6f..f84e6e1 100644 --- a/tree-buf/src/internal/encoder_decoder.rs +++ b/tree-buf/src/internal/encoder_decoder.rs @@ -95,7 +95,7 @@ pub trait InfallibleDecoderArray: Sized { /// This trait exists to first reduce a little bit of boilerplate for the common /// case of not having fallibility, but also to automatically inline at least the Ok /// wrapping portion of the code to aid the optimizer in knowing that the Error path -/// is impossible. Putting the inline here instead of on a decode_next of a DecoderArray +/// is impossible. Putting the inline here instead of on a `decode_next` of a `DecoderArray` /// implementation allows for not necessarily inlining what may be a larger method. /// It may not be necessary, but why not. impl DecoderArray for T { diff --git a/tree-buf/src/internal/encodings/compress.rs b/tree-buf/src/internal/encodings/compress.rs index f07deda..d850b04 100644 --- a/tree-buf/src/internal/encodings/compress.rs +++ b/tree-buf/src/internal/encodings/compress.rs @@ -25,7 +25,6 @@ pub(crate) fn compress(data: &[T], stream: &mut by_size.push((i, size)); } } - drop(samples); profile_section!(actual_compress); @@ -33,7 +32,7 @@ pub(crate) fn compress(data: &[T], stream: &mut by_size.sort_unstable_by_key(|&(_, size)| size); // Return the first compressor that succeeds - for ranked in by_size.iter() { + for ranked in &by_size { if let Ok(ok) = compressors.compress(ranked.0, data, stream) { return ok; } diff --git a/tree-buf/src/internal/encodings/delta.rs b/tree-buf/src/internal/encodings/delta.rs index 58b7b91..cbb34db 100644 --- a/tree-buf/src/internal/encodings/delta.rs +++ b/tree-buf/src/internal/encodings/delta.rs @@ -4,13 +4,13 @@ use std::ops::{Add, Sub}; // FIXME: This may not be what is needed. Zigzag may be. pub fn delta_encode_in_place + Copy>(data: &mut [T]) { profile_fn!(delta_encode_in_place); - if data.len() == 0 { + if data.is_empty() { return; } let mut current = data[0]; - for i in 1..data.len() { - let next = data[i]; - data[i] = next - current; + for value in data.iter_mut().skip(1) { + let next = *value; + *value = next - current; current = next; } } diff --git a/tree-buf/src/internal/encodings/dictionary.rs b/tree-buf/src/internal/encodings/dictionary.rs index 429fa6c..d09b3ce 100644 --- a/tree-buf/src/internal/encodings/dictionary.rs +++ b/tree-buf/src/internal/encodings/dictionary.rs @@ -77,15 +77,13 @@ fn get_lookup_table(data: &[T let mut values = Vec::new(); let mut lookup = HashMap::new(); - for value in data.iter() { - let index = if let Some(i) = lookup.get(value) { - *i - } else { + for value in data { + let index = lookup.get(value).copied().unwrap_or_else(|| { let i = lookup.len(); lookup.insert(value, i); values.push(*value); i - }; + }); //indices.push(index.try_into().map_err(|_| ())?); indices.push(index as u64); } diff --git a/tree-buf/src/internal/encodings/gorilla_new.rs b/tree-buf/src/internal/encodings/gorilla_new.rs index 4bc57ce..9e7a826 100644 --- a/tree-buf/src/internal/encodings/gorilla_new.rs +++ b/tree-buf/src/internal/encodings/gorilla_new.rs @@ -15,7 +15,7 @@ pub fn size_for(data: impl Iterator) -> Result { let mut data = data.map(f64::to_bits); // Initialized to 72 to account for the first value, // and 1 byte at end of "remaining bits" - let mut bits = 72usize; + let mut bits = 72_usize; let buffer = match data.next() { Some(first) => first, @@ -30,20 +30,19 @@ pub fn size_for(data: impl Iterator) -> Result { for value in data { let xored = previous ^ value; - match xored { - 0 => bits += 1, - _ => { - let lz = xored.leading_zeros().min(31) as usize; - let tz = xored.trailing_zeros() as usize; - let prev_lz = prev_xor.leading_zeros() as usize; - let prev_tz = if prev_lz == 64 { 0 } else { prev_xor.trailing_zeros() as usize }; - if lz >= prev_lz && tz >= prev_tz { - bits += 66 - prev_tz - prev_lz; - } else { - bits += 77 - tz - lz; - } + if let 0 = xored { + bits += 1; + } else { + let lz = xored.leading_zeros().min(31) as usize; + let tz = xored.trailing_zeros() as usize; + let prev_lz = prev_xor.leading_zeros() as usize; + let prev_tz = if prev_lz == 64 { 0 } else { prev_xor.trailing_zeros() as usize }; + if lz >= prev_lz && tz >= prev_tz { + bits += 66 - prev_tz - prev_lz; + } else { + bits += 77 - tz - lz; } - }; + } previous = value; prev_xor = xored; @@ -94,30 +93,29 @@ pub fn compress(data: impl Iterator, bytes: &mut Vec) -> Result< for value in data { let xored = previous ^ value; - match xored { - 0 => encode(0, 1, capacity, buffer, bytes), - _ => { - let lz = xored.leading_zeros().min(31) as u64; - let tz = xored.trailing_zeros() as u64; - let prev_lz = prev_xor.leading_zeros() as u64; - let prev_tz = if prev_lz == 64 { 0 } else { prev_xor.trailing_zeros() as u64 }; - if lz >= prev_lz && tz >= prev_tz { - let meaningful_bits = xored >> prev_tz; - let meaningful_bit_count = 64 - prev_tz - prev_lz; - - encode(0b10, 2, capacity, buffer, bytes); - encode(meaningful_bits, meaningful_bit_count as u8, capacity, buffer, bytes); - } else { - let meaningful_bits = xored >> tz; - let meaningful_bit_count = 64 - tz - lz; - - encode(0b11, 2, capacity, buffer, bytes); - encode(lz, 5, capacity, buffer, bytes); - encode(meaningful_bit_count - 1, 6, capacity, buffer, bytes); - encode(meaningful_bits, meaningful_bit_count as u8, capacity, buffer, bytes); - } + if let 0 = xored { + encode(0, 1, capacity, buffer, bytes) + } else { + let lz = u64::from(xored.leading_zeros().min(31)); + let tz = u64::from(xored.trailing_zeros()); + let prev_lz = u64::from(prev_xor.leading_zeros()); + let prev_tz = if prev_lz == 64 { 0 } else { u64::from(prev_xor.trailing_zeros()) }; + if lz >= prev_lz && tz >= prev_tz { + let meaningful_bits = xored >> prev_tz; + let meaningful_bit_count = 64 - prev_tz - prev_lz; + + encode(0b10, 2, capacity, buffer, bytes); + encode(meaningful_bits, meaningful_bit_count as u8, capacity, buffer, bytes); + } else { + let meaningful_bits = xored >> tz; + let meaningful_bit_count = 64 - tz - lz; + + encode(0b11, 2, capacity, buffer, bytes); + encode(lz, 5, capacity, buffer, bytes); + encode(meaningful_bit_count - 1, 6, capacity, buffer, bytes); + encode(meaningful_bits, meaningful_bit_count as u8, capacity, buffer, bytes); } - }; + } previous = value; prev_xor = xored; diff --git a/tree-buf/src/internal/encodings/gorilla_old.rs b/tree-buf/src/internal/encodings/gorilla_old.rs index 97581a3..2150c56 100644 --- a/tree-buf/src/internal/encodings/gorilla_old.rs +++ b/tree-buf/src/internal/encodings/gorilla_old.rs @@ -9,7 +9,7 @@ where f64: AsPrimitive, { // FIXME: Should do schema mismatch for f32 -> f64 - let num_bits_last_elm = *bytes.last().ok_or_else(|| DecodeError::InvalidFormat)?; + let num_bits_last_elm = *bytes.last().ok_or(DecodeError::InvalidFormat)?; // Remove the byte we just read containing the bit count of the last element. let bytes = &bytes[..bytes.len() - 1]; let mut last_byte_count = num_bits_last_elm / 8; @@ -18,7 +18,7 @@ where } let last = &bytes[bytes.len() - last_byte_count as usize..]; let bytes = &bytes[..bytes.len() - last.len()]; - let mut last_2 = [0u8; 8]; + let mut last_2 = [0_u8; 8]; for (i, value) in last.iter().enumerate() { last_2[i + (8 - last.len())] = *value; } @@ -43,7 +43,7 @@ where drop(construct); // FIXME: It seems like this collect can panic if the data is invalid. profile_section!(collect); - let values: Vec<_> = iterator.map(|v| v.as_()).collect(); + let values: Vec<_> = iterator.map(AsPrimitive::as_).collect(); drop(collect); Ok(values) } diff --git a/tree-buf/src/internal/encodings/packed_bool.rs b/tree-buf/src/internal/encodings/packed_bool.rs index 44263ab..0ca53e7 100644 --- a/tree-buf/src/internal/encodings/packed_bool.rs +++ b/tree-buf/src/internal/encodings/packed_bool.rs @@ -28,6 +28,7 @@ pub fn encode_packed_bool(items: &[bool], bytes: &mut Vec) { } #[cfg(feature = "decode")] +#[must_use] pub fn decode_packed_bool(bytes: &[u8]) -> Vec { profile_fn!(decode_packed_bool); diff --git a/tree-buf/src/internal/encodings/rle.rs b/tree-buf/src/internal/encodings/rle.rs index d36189c..6c81fb8 100644 --- a/tree-buf/src/internal/encodings/rle.rs +++ b/tree-buf/src/internal/encodings/rle.rs @@ -40,16 +40,14 @@ impl Iterator for RleIterator { self.current_value = self.values.next(); self.next() } - Some(run) => match run { - 0 => { - self.current_run = None; - self.current_value.take() - } - _ => { - self.current_run = Some(run - 1); - self.current_value.clone() - } - }, + Some(0) => { + self.current_run = None; + self.current_value.take() + } + Some(run) => { + self.current_run = Some(run - 1); + self.current_value.clone() + } } } } @@ -96,7 +94,7 @@ fn get_runs(data: &[T]) -> Result<(Vec, Vec), ()> { profile_fn!(rle_get_runs); let mut runs = Vec::new(); - let mut current_run = 0u64; + let mut current_run = 0_u64; let mut current_value = data[0]; let mut values = vec![]; for item in data[1..].iter() { diff --git a/tree-buf/src/internal/encodings/rle_bool.rs b/tree-buf/src/internal/encodings/rle_bool.rs index 190eb04..e8ae2c5 100644 --- a/tree-buf/src/internal/encodings/rle_bool.rs +++ b/tree-buf/src/internal/encodings/rle_bool.rs @@ -2,6 +2,7 @@ use crate::prelude::*; use std::vec::IntoIter; #[cfg(feature = "decode")] +#[must_use] pub fn decode_rle_bool(runs: IntoIter, first: bool) -> IntoIter { let mut results = Vec::new(); let mut current = first; diff --git a/tree-buf/src/internal/encodings/varint.rs b/tree-buf/src/internal/encodings/varint.rs index 26e8d3d..dc0f80b 100644 --- a/tree-buf/src/internal/encodings/varint.rs +++ b/tree-buf/src/internal/encodings/varint.rs @@ -1,6 +1,7 @@ use crate::prelude::*; #[cfg(feature = "encode")] +#[must_use] pub fn size_for_varint(value: u64) -> usize { /* // Performance: Tried this lookup table and it was slower @@ -171,7 +172,7 @@ pub fn decode_prefix_varint(bytes: &[u8], offset: &mut usize) -> DecodeResult DecodeResult (first >> 1) as u64, - 1 => (first >> 2) as u64 | ((bytes[*offset + 1] as u64) << 6), - 2 => (first >> 3) as u64 | ((bytes[*offset + 1] as u64) << 5) | ((bytes[*offset + 2] as u64) << 13), - 3 => (first >> 4) as u64 | ((bytes[*offset + 1] as u64) << 4) | ((bytes[*offset + 2] as u64) << 12) | ((bytes[*offset + 3] as u64) << 20), + 0 => u64::from(first >> 1), + 1 => u64::from(first >> 2) | (u64::from(bytes[*offset + 1]) << 6), + 2 => u64::from(first >> 3) | (u64::from(bytes[*offset + 1]) << 5) | (u64::from(bytes[*offset + 2]) << 13), + 3 => u64::from(first >> 4) | (u64::from(bytes[*offset + 1]) << 4) | (u64::from(bytes[*offset + 2]) << 12) | (u64::from(bytes[*offset + 3]) << 20), 4 => { - (first >> 5) as u64 - | ((bytes[*offset + 1] as u64) << 3) - | ((bytes[*offset + 2] as u64) << 11) - | ((bytes[*offset + 3] as u64) << 19) - | ((bytes[*offset + 4] as u64) << 27) + u64::from(first >> 5) + | (u64::from(bytes[*offset + 1]) << 3) + | (u64::from(bytes[*offset + 2]) << 11) + | (u64::from(bytes[*offset + 3]) << 19) + | (u64::from(bytes[*offset + 4]) << 27) } 5 => { - (first >> 6) as u64 - | ((bytes[*offset + 1] as u64) << 2) - | ((bytes[*offset + 2] as u64) << 10) - | ((bytes[*offset + 3] as u64) << 18) - | ((bytes[*offset + 4] as u64) << 26) - | ((bytes[*offset + 5] as u64) << 34) + u64::from(first >> 6) + | (u64::from(bytes[*offset + 1]) << 2) + | (u64::from(bytes[*offset + 2]) << 10) + | (u64::from(bytes[*offset + 3]) << 18) + | (u64::from(bytes[*offset + 4]) << 26) + | (u64::from(bytes[*offset + 5]) << 34) } 6 => { - (first >> 7) as u64 - | ((bytes[*offset + 1] as u64) << 1) - | ((bytes[*offset + 2] as u64) << 9) - | ((bytes[*offset + 3] as u64) << 17) - | ((bytes[*offset + 4] as u64) << 25) - | ((bytes[*offset + 5] as u64) << 33) - | ((bytes[*offset + 6] as u64) << 41) + u64::from(first >> 7) + | (u64::from(bytes[*offset + 1]) << 1) + | (u64::from(bytes[*offset + 2]) << 9) + | (u64::from(bytes[*offset + 3]) << 17) + | (u64::from(bytes[*offset + 4]) << 25) + | (u64::from(bytes[*offset + 5]) << 33) + | (u64::from(bytes[*offset + 6]) << 41) } 7 => { - (bytes[*offset + 1] as u64) - | ((bytes[*offset + 2] as u64) << 8) - | ((bytes[*offset + 3] as u64) << 16) - | ((bytes[*offset + 4] as u64) << 24) - | ((bytes[*offset + 5] as u64) << 32) - | ((bytes[*offset + 6] as u64) << 40) - | ((bytes[*offset + 7] as u64) << 48) + u64::from(bytes[*offset + 1]) + | (u64::from(bytes[*offset + 2]) << 8) + | (u64::from(bytes[*offset + 3]) << 16) + | (u64::from(bytes[*offset + 4]) << 24) + | (u64::from(bytes[*offset + 5]) << 32) + | (u64::from(bytes[*offset + 6]) << 40) + | (u64::from(bytes[*offset + 7]) << 48) } 8 => { - (bytes[*offset + 1] as u64) - | ((bytes[*offset + 2] as u64) << 8) - | ((bytes[*offset + 3] as u64) << 16) - | ((bytes[*offset + 4] as u64) << 24) - | ((bytes[*offset + 5] as u64) << 32) - | ((bytes[*offset + 6] as u64) << 40) - | ((bytes[*offset + 7] as u64) << 48) - | ((bytes[*offset + 8] as u64) << 56) + u64::from(bytes[*offset + 1]) + | (u64::from(bytes[*offset + 2]) << 8) + | (u64::from(bytes[*offset + 3]) << 16) + | (u64::from(bytes[*offset + 4]) << 24) + | (u64::from(bytes[*offset + 5]) << 32) + | (u64::from(bytes[*offset + 6]) << 40) + | (u64::from(bytes[*offset + 7]) << 48) + | (u64::from(bytes[*offset + 8]) << 56) } _ => unreachable!(), }; @@ -233,10 +234,10 @@ pub fn decode_prefix_varint(bytes: &[u8], offset: &mut usize) -> DecodeResult DecodeResult { - let first = bytes.get(*offset).ok_or_else(|| DecodeError::InvalidFormat)?; + let first = bytes.get(*offset).ok_or(DecodeError::InvalidFormat)?; let shift = first.trailing_zeros(); // TODO: Ensure unchecked indexing follows. @@ -245,52 +246,52 @@ pub fn decode_suffix_varint(bytes: &[u8], offset: &mut usize) -> DecodeResult (first >> 1) as u64, - 1 => (first >> 2) as u64 | ((bytes[*offset - 1] as u64) << 6), - 2 => (first >> 3) as u64 | ((bytes[*offset - 2] as u64) << 5) | ((bytes[*offset - 1] as u64) << 13), - 3 => (first >> 4) as u64 | ((bytes[*offset - 3] as u64) << 4) | ((bytes[*offset - 2] as u64) << 12) | ((bytes[*offset - 1] as u64) << 20), + 0 => u64::from(first >> 1), + 1 => u64::from(first >> 2) | (u64::from(bytes[*offset - 1]) << 6), + 2 => u64::from(first >> 3) | (u64::from(bytes[*offset - 2]) << 5) | (u64::from(bytes[*offset - 1]) << 13), + 3 => u64::from(first >> 4) | (u64::from(bytes[*offset - 3]) << 4) | (u64::from(bytes[*offset - 2]) << 12) | (u64::from(bytes[*offset - 1]) << 20), 4 => { - (first >> 5) as u64 - | ((bytes[*offset - 4] as u64) << 3) - | ((bytes[*offset - 3] as u64) << 11) - | ((bytes[*offset - 2] as u64) << 19) - | ((bytes[*offset - 1] as u64) << 27) + u64::from(first >> 5) + | (u64::from(bytes[*offset - 4]) << 3) + | (u64::from(bytes[*offset - 3]) << 11) + | (u64::from(bytes[*offset - 2]) << 19) + | (u64::from(bytes[*offset - 1]) << 27) } 5 => { - (first >> 6) as u64 - | ((bytes[*offset - 5] as u64) << 2) - | ((bytes[*offset - 4] as u64) << 10) - | ((bytes[*offset - 3] as u64) << 18) - | ((bytes[*offset - 2] as u64) << 26) - | ((bytes[*offset - 1] as u64) << 34) + u64::from(first >> 6) + | (u64::from(bytes[*offset - 5]) << 2) + | (u64::from(bytes[*offset - 4]) << 10) + | (u64::from(bytes[*offset - 3]) << 18) + | (u64::from(bytes[*offset - 2]) << 26) + | (u64::from(bytes[*offset - 1]) << 34) } 6 => { - (first >> 7) as u64 - | ((bytes[*offset - 6] as u64) << 1) - | ((bytes[*offset - 5] as u64) << 9) - | ((bytes[*offset - 4] as u64) << 17) - | ((bytes[*offset - 3] as u64) << 25) - | ((bytes[*offset - 2] as u64) << 33) - | ((bytes[*offset - 1] as u64) << 41) + u64::from(first >> 7) + | (u64::from(bytes[*offset - 6]) << 1) + | (u64::from(bytes[*offset - 5]) << 9) + | (u64::from(bytes[*offset - 4]) << 17) + | (u64::from(bytes[*offset - 3]) << 25) + | (u64::from(bytes[*offset - 2]) << 33) + | (u64::from(bytes[*offset - 1]) << 41) } 7 => { - (bytes[*offset - 7] as u64) - | ((bytes[*offset - 6] as u64) << 8) - | ((bytes[*offset - 5] as u64) << 16) - | ((bytes[*offset - 4] as u64) << 24) - | ((bytes[*offset - 3] as u64) << 32) - | ((bytes[*offset - 2] as u64) << 40) - | ((bytes[*offset - 1] as u64) << 48) + u64::from(bytes[*offset - 7]) + | (u64::from(bytes[*offset - 6]) << 8) + | (u64::from(bytes[*offset - 5]) << 16) + | (u64::from(bytes[*offset - 4]) << 24) + | (u64::from(bytes[*offset - 3]) << 32) + | (u64::from(bytes[*offset - 2]) << 40) + | (u64::from(bytes[*offset - 1]) << 48) } 8 => { - (bytes[*offset - 8] as u64) - | ((bytes[*offset - 7] as u64) << 8) - | ((bytes[*offset - 6] as u64) << 16) - | ((bytes[*offset - 5] as u64) << 24) - | ((bytes[*offset - 4] as u64) << 32) - | ((bytes[*offset - 3] as u64) << 40) - | ((bytes[*offset - 2] as u64) << 48) - | ((bytes[*offset - 1] as u64) << 56) + u64::from(bytes[*offset - 8]) + | (u64::from(bytes[*offset - 7]) << 8) + | (u64::from(bytes[*offset - 6]) << 16) + | (u64::from(bytes[*offset - 5]) << 24) + | (u64::from(bytes[*offset - 4]) << 32) + | (u64::from(bytes[*offset - 3]) << 40) + | (u64::from(bytes[*offset - 2]) << 48) + | (u64::from(bytes[*offset - 1]) << 56) } _ => unreachable!(), }; @@ -332,7 +333,7 @@ mod tests { #[test] fn test_prefix() -> DecodeResult<()> { let vecs = vec![vec![99, 127, 128, 0, 1, 2, 3, std::u64::MAX]]; - for vec in vecs.iter() { + for vec in &vecs { round_trip_prefix(vec)?; } @@ -342,7 +343,7 @@ mod tests { for a in 0..64 { for b in 0..64 { for c in 0..64 { - let num = (1u64 << a) | (1u64 << b) | (1u64 << c); + let num = (1_u64 << a) | (1_u64 << b) | (1_u64 << c); vec.push(num); } round_trip_prefix(&vec)?; @@ -356,7 +357,7 @@ mod tests { #[test] fn test_suffix() -> DecodeResult<()> { let vecs = vec![vec![99, 127, 128, 0, 1, 2, 3, std::u64::MAX]]; - for vec in vecs.iter() { + for vec in &vecs { round_trip_suffix(vec)?; } @@ -367,7 +368,7 @@ mod tests { for a in 0..64 { for b in 0..64 { for c in 0..64 { - let num = (1u64 << a) | (1u64 << b) | (1u64 << c); + let num = (1_u64 << a) | (1_u64 << b) | (1_u64 << c); vec.push(num); } round_trip_suffix(&vec)?; diff --git a/tree-buf/src/internal/mod.rs b/tree-buf/src/internal/mod.rs index ba1322f..54accf0 100644 --- a/tree-buf/src/internal/mod.rs +++ b/tree-buf/src/internal/mod.rs @@ -16,8 +16,8 @@ pub mod parallel; pub mod rust_std; pub mod types; -pub use {branch::*, encoder_decoder::*, encodings::*, options::*, parallel::*, rust_std::*, types::*}; pub(crate) use buffer::*; +pub use {branch::*, encoder_decoder::*, encodings::*, options::*, parallel::*, rust_std::*, types::*}; pub(crate) use markers::*; diff --git a/tree-buf/src/internal/parallel.rs b/tree-buf/src/internal/parallel.rs index ea43afc..fefd20a 100644 --- a/tree-buf/src/internal/parallel.rs +++ b/tree-buf/src/internal/parallel.rs @@ -1,5 +1,4 @@ use crate::prelude::*; -use rayon; // TODO: Have a way to not use rayon when the operation is considered 'trivial' #[inline(always)] diff --git a/tree-buf/src/internal/types/array_fixed.rs b/tree-buf/src/internal/types/array_fixed.rs index 9db728e..4db19dd 100644 --- a/tree-buf/src/internal/types/array_fixed.rs +++ b/tree-buf/src/internal/types/array_fixed.rs @@ -45,9 +45,7 @@ macro_rules! impl_fixed { profile_method!(decode); match sticks { DynRootBranch::Array0 => { - if $size != 0 { - return Err(DecodeError::SchemaMismatch); - } else { + if $size == 0 { let data: [MaybeUninit; $size] = unsafe { MaybeUninit::uninit().assume_init() }; @@ -55,6 +53,8 @@ macro_rules! impl_fixed { // it's an empty array of uninit and doesn't need to // be initialized Ok(unsafe { transmute(data) }) + } else { + return Err(DecodeError::SchemaMismatch); } }, DynRootBranch::Array1(inner) => { diff --git a/tree-buf/src/internal/types/boolean.rs b/tree-buf/src/internal/types/boolean.rs index eadcf74..af3c866 100644 --- a/tree-buf/src/internal/types/boolean.rs +++ b/tree-buf/src/internal/types/boolean.rs @@ -1,5 +1,5 @@ -use crate::internal::encodings::packed_bool::*; -use crate::internal::encodings::rle_bool::*; +use crate::internal::encodings::packed_bool::{decode_packed_bool, encode_packed_bool}; +use crate::internal::encodings::rle_bool::{decode_rle_bool, encode_rle_bool, size_of_rle_bool}; use crate::prelude::*; use std::vec::IntoIter; diff --git a/tree-buf/src/internal/types/float.rs b/tree-buf/src/internal/types/float.rs index 772221e..822d58f 100644 --- a/tree-buf/src/internal/types/float.rs +++ b/tree-buf/src/internal/types/float.rs @@ -42,6 +42,7 @@ macro_rules! impl_float { } #[cfg(feature = "encode")] + #[allow(clippy::float_cmp)] impl Encodable for $T { type EncoderArray = Vec<$T>; fn encode_root(&self, stream: &mut EncoderStream<'_, O>) -> RootTypeId { @@ -132,7 +133,7 @@ macro_rules! impl_float { let values = decode_all(&bytes, |bytes, offset| Ok(super::_f32::decode_item(bytes, offset)?.as_()))?; Ok(values.into_iter()) } - ArrayFloat::DoubleGorilla(bytes) => gorilla::decompress::<$T>(&bytes).map(|f| f.into_iter()), + ArrayFloat::DoubleGorilla(bytes) => gorilla::decompress::<$T>(&bytes).map(IntoIterator::into_iter), /* ArrayFloat::Zfp32(bytes) => { // FIXME: This is likely a bug switching between 32 and 64 might just get garbage data out @@ -223,7 +224,7 @@ macro_rules! impl_float { if let Some(tolerance) = options.lossy_float_tolerance() { // TODO: This is a hack (albeit a surprisingly effective one) to get lossy compression // before a real lossy compressor (Eg: fzip) is used. - let multiplier = (2.0 as $T).powi(tolerance * -1); + let multiplier = (2.0 as $T).powi(-tolerance); let data = data.iter().map(|f| ((f * multiplier).floor() / multiplier) as f64); gorilla::size_for(data) } else { @@ -239,7 +240,7 @@ macro_rules! impl_float { if let Some(tolerance) = stream.options.lossy_float_tolerance() { // TODO: This is a hack (albeit a surprisingly effective one) to get lossy compression // before a real lossy compressor (Eg: fzip) is used. - let multiplier = (2.0 as $T).powi(tolerance * -1); + let multiplier = (2.0 as $T).powi(-tolerance); let data = data.iter().map(|f| ((f * multiplier).floor() / multiplier) as f64); gorilla::compress(data, stream.bytes) } else { diff --git a/tree-buf/src/internal/types/hashmap.rs b/tree-buf/src/internal/types/hashmap.rs index a95520b..721fa8e 100644 --- a/tree-buf/src/internal/types/hashmap.rs +++ b/tree-buf/src/internal/types/hashmap.rs @@ -43,15 +43,13 @@ impl Encodable for HashMap impl Decodable for HashMap where // Overly verbose because of `?` requiring `From` See also ec4fa3ba-def5-44eb-9065-e80b59530af6 - DecodeError: From<<::DecoderArray as DecoderArray>::Error>, - // Overly verbose because of `?` requiring `From` See also ec4fa3ba-def5-44eb-9065-e80b59530af6 - DecodeError: From<<::DecoderArray as DecoderArray>::Error>, + DecodeError: From<<::DecoderArray as DecoderArray>::Error> + From<<::DecoderArray as DecoderArray>::Error>, { type DecoderArray = Option>; fn decode(sticks: DynRootBranch<'_>, options: &impl DecodeOptions) -> DecodeResult { profile_method!(decode); - let mut v = Default::default(); // TODO: (Performance) Capacity + let mut v = HashMap::default(); // TODO: (Performance) Capacity match sticks { DynRootBranch::Map0 => Ok(v), DynRootBranch::Map1 { key, value } => { @@ -121,9 +119,7 @@ impl DecoderArray fo where K::Decode: Hash + Eq, // Overly verbose because of `?` requiring `From` See also ec4fa3ba-def5-44eb-9065-e80b59530af6 - DecodeError: From, - // Overly verbose because of `?` requiring `From` See also ec4fa3ba-def5-44eb-9065-e80b59530af6 - DecodeError: From, + DecodeError: From + From, { type Decode = HashMap; type Error = DecodeError; @@ -174,7 +170,7 @@ where } Ok(result) } else { - Ok(Default::default()) + Ok(Self::Decode::default()) } } } diff --git a/tree-buf/src/internal/types/integer.rs b/tree-buf/src/internal/types/integer.rs index a0015e9..d44180f 100644 --- a/tree-buf/src/internal/types/integer.rs +++ b/tree-buf/src/internal/types/integer.rs @@ -1,7 +1,7 @@ // TODO: Try Streaming V-Byte (which has a Rust port) // https://lemire.me/blog/2017/09/27/stream-vbyte-breaking-new-speed-records-for-integer-compression/ use crate::internal::encodings::compress; -use crate::internal::encodings::varint::*; +use crate::internal::encodings::varint::{decode_prefix_varint, encode_prefix_varint, size_for_varint}; use crate::prelude::*; use num_traits::{AsPrimitive, Bounded}; use simple_16::Simple16; @@ -24,7 +24,7 @@ impl Bounded for U0 { } } mod _0 { - use super::*; + use super::{ArrayTypeId, EncodeOptions, EncoderStream, U0}; pub type Type = U0; pub fn encode_array(_data: &[T], _max: T, _stream: &mut EncoderStream<'_, O>) -> ArrayTypeId { @@ -132,12 +132,7 @@ macro_rules! impl_lowerable { fn decode(sticks: DynRootBranch<'_>, _options: &impl DecodeOptions) -> DecodeResult { profile_method!(decode); match sticks { - DynRootBranch::Integer(root_int) => { - match root_int { - RootInteger::U(v) => v.try_into().map_err(|_| DecodeError::SchemaMismatch), - _ => Err(DecodeError::SchemaMismatch), - } - } + DynRootBranch::Integer(RootInteger::U(v)) => v.try_into().map_err(|_| DecodeError::SchemaMismatch), _ => Err(DecodeError::SchemaMismatch), } } @@ -321,23 +316,23 @@ fn encode_root_uint(value: u64, bytes: &mut Vec) -> RootTypeId { bytes.extend_from_slice(&le[..2]); RootTypeId::IntU16 } - 65536..=16777215 => { + 65536..=16_777_215 => { bytes.extend_from_slice(&le[..3]); RootTypeId::IntU24 } - 16777216..=4294967295 => { + 16_777_216..=4_294_967_295 => { bytes.extend_from_slice(&le[..4]); RootTypeId::IntU32 } - 4294967296..=1099511627775 => { + 4_294_967_296..=1_099_511_627_775 => { bytes.extend_from_slice(&le[..5]); RootTypeId::IntU40 } - 1099511627776..=281474976710655 => { + 1_099_511_627_776..=281_474_976_710_655 => { bytes.extend_from_slice(&le[..6]); RootTypeId::IntU48 } - 281474976710656..=72057594037927936 => { + 281_474_976_710_656..=72_057_594_037_927_936 => { bytes.extend_from_slice(&le[..7]); RootTypeId::IntU56 } diff --git a/tree-buf/src/internal/types/nullable.rs b/tree-buf/src/internal/types/nullable.rs index 6cf6421..f7134cf 100644 --- a/tree-buf/src/internal/types/nullable.rs +++ b/tree-buf/src/internal/types/nullable.rs @@ -4,11 +4,7 @@ use crate::prelude::*; impl Encodable for Option { type EncoderArray = NullableEncoder; fn encode_root(&self, stream: &mut EncoderStream<'_, O>) -> RootTypeId { - if let Some(value) = self { - T::encode_root(value, stream) - } else { - RootTypeId::Void - } + self.as_ref().map_or(RootTypeId::Void, |value| T::encode_root(&value, stream)) } } @@ -42,13 +38,11 @@ impl EncoderArray> for NullableEncoder fn flush(self, stream: &mut EncoderStream<'_, O>) -> ArrayTypeId { profile_method!(flush); let Self { opt, value } = self; - if let Some(value) = value { + value.map_or(ArrayTypeId::Void, |v| { stream.encode_with_id(|stream| opt.flush(stream)); - stream.encode_with_id(|stream| value.flush(stream)); + stream.encode_with_id(|stream| v.flush(stream)); ArrayTypeId::Nullable - } else { - ArrayTypeId::Void - } + }) } } diff --git a/tree-buf/src/internal/types/string.rs b/tree-buf/src/internal/types/string.rs index 58002df..e8679b7 100644 --- a/tree-buf/src/internal/types/string.rs +++ b/tree-buf/src/internal/types/string.rs @@ -1,4 +1,4 @@ -use crate::internal::encodings::varint::*; +use crate::internal::encodings::varint::{decode_prefix_varint, encode_prefix_varint, size_for_varint}; use crate::prelude::*; use rle::RLE; use std::borrow::Borrow; @@ -104,7 +104,7 @@ impl InfallibleDecoderArray for IntoIter { DynArrayBranch::String(bytes) => { profile_section!(str_utf8); - let strs = decode_all(&bytes, |b, o| decode_str(b, o).and_then(|v| Ok(v.to_owned())))?; + let strs = decode_all(&bytes, |b, o| decode_str(b, o).map(std::borrow::ToOwned::to_owned))?; Ok(strs.into_iter()) } DynArrayBranch::RLE { runs, values } => { diff --git a/tree-buf/src/lib.rs b/tree-buf/src/lib.rs index 584dc68..bc4b174 100644 --- a/tree-buf/src/lib.rs +++ b/tree-buf/src/lib.rs @@ -44,15 +44,12 @@ pub fn encode(value: &T) -> Vec { crate::experimental::options::encode_with_options(value, &options) } - #[cfg(feature = "decode")] pub fn decode(bytes: &[u8]) -> DecodeResult { let options = DecodeOptionsDefault; crate::experimental::options::decode_with_options(bytes, &options) } - - // TODO: Figure out recursion, at least enough to handle this: https://docs.rs/serde_json/1.0.44/serde_json/value/enum.Value.html // TODO: Nullable should be able to handle recursion as well, even if Option doesn't. (Option> could though) diff --git a/tree-buf/tests/common/mod.rs b/tree-buf/tests/common/mod.rs index b28591a..5f1ecc9 100644 --- a/tree-buf/tests/common/mod.rs +++ b/tree-buf/tests/common/mod.rs @@ -29,7 +29,7 @@ where //dbg!(tree_buf::internal::decode_root(&bytes)); match result { Ok(parsed) => assert_eq!(o, &parsed), - Err(e) => assert!(false, "{}", e), + Err(e) => panic!("{}", e), } if let Some(size) = size.into() { assert_eq!(bytes.len() as i32, size, "Size Before: {}\nSize After: {}", size, bytes.len()); diff --git a/tree-buf/tests/round_trip.rs b/tree-buf/tests/round_trip.rs index 0513511..8efdecc 100644 --- a/tree-buf/tests/round_trip.rs +++ b/tree-buf/tests/round_trip.rs @@ -440,9 +440,7 @@ fn nested_strings_using_rle() { fn long_bool_runs() { let mut data = Vec::new(); for i in 560..570 { - for _ in 0..i { - data.push(true); - } + data.extend(vec![true; i]); data.push(false); } round_trip(&data, 36, 68); @@ -467,7 +465,7 @@ fn delta_prefix_var() { 1_000_000_002, 1_000_000_010, 1_000_000_100, - 1_000_000_50, + 100_000_050, 1_000_000_125, 1_000_000_122, 999_000_000, diff --git a/tree-buf/tests/schema_mismatches.rs b/tree-buf/tests/schema_mismatches.rs index 042d888..ef833e4 100644 --- a/tree-buf/tests/schema_mismatches.rs +++ b/tree-buf/tests/schema_mismatches.rs @@ -9,7 +9,7 @@ fn expect_schema_mismatch() { let result = decode::(&bytes); match result.unwrap_err() { DecodeError::SchemaMismatch => (), - _ => assert!(false), + _ => panic!(), } }