From 7f062c4809306a05396b920ac2b49b5ed866f910 Mon Sep 17 00:00:00 2001 From: Florian Hartwig Date: Thu, 13 Oct 2016 22:53:51 +0200 Subject: [PATCH] Use vec! macro instead of iterator to initialise buffer The vec! macro is much faster than using `collect` to initialise a vector. This change decreases the run time of `load_png_from_memory` by 20%-30% on my machine. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 45faca9..04efe80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,7 +156,7 @@ pub fn load_png_from_memory(image: &[u8]) -> Result { _ => panic!("color type not supported"), }; - let mut image_data: Vec = repeat(0u8).take(width * height * pixel_width).collect(); + let mut image_data: Vec = vec![0u8;width * height * pixel_width]; let image_buf = image_data.as_mut_ptr(); let mut row_pointers: Vec<*mut u8> = (0..height).map(|idx| { image_buf.offset((width * pixel_width * idx) as isize)