[thin_metadata_pack/unpack] Use Vec::with_capacity() to avoid reallocs.

Gives a small speed boost to both pack and unpack.
This commit is contained in:
Joe Thornber 2020-08-21 09:14:54 +01:00
parent c9a759b4e8
commit b01a0a46d1
2 changed files with 4 additions and 4 deletions

View File

@ -13,7 +13,7 @@ use Delta::*;
pub fn to_delta(ns: &[u64]) -> Vec<Delta> {
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];

View File

@ -146,8 +146,8 @@ pub fn pack_u64s<W: Write>(w: &mut W, ns: &[u64]) -> io::Result<()> {
}
fn unshift_nrs(shift: usize, ns: &[u64]) -> (Vec<u64>, Vec<u64>) {
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: Read>(r: &mut R, nibble: u8) -> io::Result<u64> {
}
fn unpack_u64s<R: Read>(r: &mut R, count: usize) -> io::Result<Vec<u64>> {
let mut v = Vec::new();
let mut v = Vec::with_capacity(count);
for _ in 0..count {
let n = r.read_u64::<LittleEndian>()?;
v.push(n);