From 17707b84218f5c30d57de0d99a8081016f122f2f Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 13 Jul 2022 13:43:35 +0200 Subject: [PATCH] Improve performance by preallocating a bit --- implementations/rust/preserves/src/value/iolist.rs | 4 ++-- implementations/rust/preserves/src/value/packed/writer.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/implementations/rust/preserves/src/value/iolist.rs b/implementations/rust/preserves/src/value/iolist.rs index 6a7e5da..9061d8e 100644 --- a/implementations/rust/preserves/src/value/iolist.rs +++ b/implementations/rust/preserves/src/value/iolist.rs @@ -11,14 +11,14 @@ pub struct IOList { impl IOList { pub fn new() -> Self { IOList { - chunks: vec![], + chunks: Vec::with_capacity(5), len: 0, } } fn rightmost_chunk(&mut self) -> &mut Vec { if self.chunks.is_empty() { - self.chunks.push(vec![]); + self.chunks.push(Vec::with_capacity(64)) } self.chunks.last_mut().unwrap() } diff --git a/implementations/rust/preserves/src/value/packed/writer.rs b/implementations/rust/preserves/src/value/packed/writer.rs index e71c8e2..7d46659 100644 --- a/implementations/rust/preserves/src/value/packed/writer.rs +++ b/implementations/rust/preserves/src/value/packed/writer.rs @@ -55,7 +55,7 @@ impl PackedWriter { PackedWriter { w: write, buffer: IOList::new(), - items: Vec::new(), + items: Vec::with_capacity(16), streaming: false, } } @@ -117,7 +117,7 @@ impl PackedWriter { } pub fn start_seq(&mut self) -> io::Result<()> { - self.items.push(Vec::new()); + self.items.push(Vec::with_capacity(32)); Ok(()) }