Skip to content

Commit

Permalink
fix(alloc): Right(ish)-sized buffer allocations
Browse files Browse the repository at this point in the history
Where possible, and where a safe improvement
  • Loading branch information
alexpovel committed Oct 27, 2024
1 parent 9b9010f commit 3f64519
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3f64519

Please sign in to comment.