From cf5b90b4445468e457795be38c21aa40aa367ea7 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Tue, 30 Jan 2018 22:14:03 -0500 Subject: [PATCH] Preallocate vector on reading in file Provides 1.5% overall speedup on -o4 --- CHANGELOG.md | 1 + src/png/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ca8212d..7bc90791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### Version 0.19.1 (unreleased) - Refactor of internal code. - Implement unix-specific permissions copying for `-p` option + - Performance optimizations ### Version 0.19.0 - [SEMVER_MAJOR] Default to overwriting the input file if `out_file` is not set. diff --git a/src/png/mod.rs b/src/png/mod.rs index 16698665..84604994 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -73,7 +73,8 @@ impl PngData { return Err(PngError::new("Failed to read from file")); } // Read raw png data into memory - let mut byte_data: Vec = Vec::new(); + let mut byte_data: Vec = + Vec::with_capacity(file.metadata().map(|m| m.len() as usize).unwrap_or(0)); match file.read_to_end(&mut byte_data) { Ok(_) => (), Err(_) => return Err(PngError::new("Failed to read from file")),