From 2ef75c214c6f525b48fd31b6c7c0da5cafc94053 Mon Sep 17 00:00:00 2001 From: Joshua Koo Date: Thu, 18 Feb 2021 05:15:05 -0800 Subject: [PATCH] Reduce string allocations while parsing During benchmarkings, pemfile parsing can be seen to be spending much time in b64buf.push_str() in flamegraphs. Setting a capacity on the base64 stringbuffer shows about 10%-25% improvements in the microbenchmarks --- src/pemfile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pemfile.rs b/src/pemfile.rs index 456a1b1..c60cea6 100644 --- a/src/pemfile.rs +++ b/src/pemfile.rs @@ -34,7 +34,7 @@ impl Item { /// You can use this function to build an iterator, for example: /// `for item in iter::from_fn(|| read_one(rd).transpose()) { ... }` pub fn read_one(rd: &mut dyn io::BufRead) -> Result, io::Error> { - let mut b64buf = String::new(); + let mut b64buf = String::with_capacity(1024); let mut section_type = None; let mut end_marker = None; let mut line = String::with_capacity(80);