From 3f645198087af037ee922e3f80c11ee7ce53d870 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Sun, 27 Oct 2024 17:46:32 +0100 Subject: [PATCH] fix(alloc): Right(ish)-sized buffer allocations Where possible, and where a safe improvement --- src/main.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index de11f1c..67c9398 100644 --- a/src/main.rs +++ b/src/main.rs @@ -274,7 +274,7 @@ fn handle_actions_on_stdin( info!("Will use stdin to stdout."); let mut source = String::new(); io::stdin().lock().read_to_string(&mut source)?; - let mut destination = String::new(); + let mut destination = String::with_capacity(source.len()); apply( global_options, @@ -565,11 +565,12 @@ fn process_path( let mut file = File::open(&path)?; let filesize = file.metadata().map_or(0, |m| m.len()); - let mut destination = + let mut source = String::with_capacity(filesize.try_into().unwrap_or(/* no perf gains for you */ 0)); - let mut source = String::new(); file.read_to_string(&mut source)?; + let mut destination = String::with_capacity(source.len()); + let changed = apply( global_options, standalone_action,