From b01a0a46d1e4e1dea247dc57cf6a4c2bf5e379ab Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 21 Aug 2020 09:14:54 +0100 Subject: [PATCH] [thin_metadata_pack/unpack] Use Vec::with_capacity() to avoid reallocs. Gives a small speed boost to both pack and unpack. --- src/pack/delta_list.rs | 2 +- src/pack/vm.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pack/delta_list.rs b/src/pack/delta_list.rs index 6a8a7e1..bd14330 100644 --- a/src/pack/delta_list.rs +++ b/src/pack/delta_list.rs @@ -13,7 +13,7 @@ use Delta::*; pub fn to_delta(ns: &[u64]) -> Vec { use std::cmp::Ordering::*; - let mut ds = Vec::new(); + let mut ds = Vec::with_capacity(ns.len()); if !ns.is_empty() { let mut base = ns[0]; diff --git a/src/pack/vm.rs b/src/pack/vm.rs index 4086640..8e3565a 100644 --- a/src/pack/vm.rs +++ b/src/pack/vm.rs @@ -146,8 +146,8 @@ pub fn pack_u64s(w: &mut W, ns: &[u64]) -> io::Result<()> { } fn unshift_nrs(shift: usize, ns: &[u64]) -> (Vec, Vec) { - let mut values = Vec::new(); - let mut shifts = Vec::new(); + let mut values = Vec::with_capacity(ns.len()); + let mut shifts = Vec::with_capacity(ns.len()); let mask = (1 << shift) - 1; for n in ns { @@ -207,7 +207,7 @@ fn unpack_with_width(r: &mut R, nibble: u8) -> io::Result { } fn unpack_u64s(r: &mut R, count: usize) -> io::Result> { - let mut v = Vec::new(); + let mut v = Vec::with_capacity(count); for _ in 0..count { let n = r.read_u64::()?; v.push(n);