From c9a759b4e89b85ed3b412873efec2d2c3bd2888e Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 21 Aug 2020 09:00:21 +0100 Subject: [PATCH] [thin_check (rust)] Use vec::with_capacity() to avoid reallocations. --- src/io_engine.rs | 2 +- src/pdata/btree.rs | 2 +- src/pdata/space_map.rs | 2 +- src/thin/check.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/io_engine.rs b/src/io_engine.rs index 61a65ec..8f03671 100644 --- a/src/io_engine.rs +++ b/src/io_engine.rs @@ -80,7 +80,7 @@ impl SyncIoEngine { } pub fn new(path: &Path, nr_files: usize, writeable: bool) -> Result { - let mut files = Vec::new(); + let mut files = Vec::with_capacity(nr_files); for _n in 0..nr_files { files.push(SyncIoEngine::open_file(path, writeable)?); } diff --git a/src/pdata/btree.rs b/src/pdata/btree.rs index a928927..081db0d 100644 --- a/src/pdata/btree.rs +++ b/src/pdata/btree.rs @@ -199,7 +199,7 @@ impl BTreeWalker { NV: NodeVisitor, V: Unpack, { - let mut blocks = Vec::new(); + let mut blocks = Vec::with_capacity(bs.len()); for b in bs { if self.sm_inc(*b)? == 0 { blocks.push(Block::new(*b)); diff --git a/src/pdata/space_map.rs b/src/pdata/space_map.rs index a2978ad..40c60ef 100644 --- a/src/pdata/space_map.rs +++ b/src/pdata/space_map.rs @@ -159,8 +159,8 @@ impl Unpack for Bitmap { fn unpack(data: &[u8]) -> IResult<&[u8], Self> { let (mut i, header) = BitmapHeader::unpack(data)?; - let mut entries = Vec::new(); let nr_words = (BLOCK_SIZE - BitmapHeader::disk_size() as usize) / 8; + let mut entries = Vec::with_capacity(nr_words * 32); for _w in 0..nr_words { let (tmp, mut word) = le_u64(i)?; diff --git a/src/thin/check.rs b/src/thin/check.rs index c38fddc..b9d52a3 100644 --- a/src/thin/check.rs +++ b/src/thin/check.rs @@ -172,7 +172,7 @@ fn check_space_map( w.walk(&mut v, root.ref_count_root)?; } - let mut blocks = Vec::new(); + let mut blocks = Vec::with_capacity(entries.len()); for i in &entries { blocks.push(Block::new(i.blocknr)); } @@ -243,7 +243,7 @@ fn repair_space_map(ctx: &Context, entries: Vec, sm: ASpaceMap) -> R let sm = sm.lock().unwrap(); - let mut blocks = Vec::new(); + let mut blocks = Vec::with_capacity(entries.len()); for i in &entries { blocks.push(Block::new(i.loc)); }