From bbd6db3687c1df052678ba456f2bbb4c346123fb Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu@web.de> Date: Sat, 12 Oct 2019 13:27:59 +0200 Subject: [PATCH] Optimize file table creation, fixes #484 as a side-effect --- src/output/details.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/output/details.rs b/src/output/details.rs index 577ed74..634dc8d 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -61,6 +61,7 @@ use std::io::{Write, Error as IOError, Result as IOResult}; +use std::mem::MaybeUninit; use std::path::PathBuf; use std::vec::IntoIter as VecIntoIter; @@ -192,13 +193,13 @@ impl<'a> Render<'a> { use std::sync::{Arc, Mutex}; use fs::feature::xattr; - let mut file_eggs = Vec::new(); + let mut file_eggs = (0..src.len()).map(|_| MaybeUninit::uninit()).collect::>(); pool.scoped(|scoped| { let file_eggs = Arc::new(Mutex::new(&mut file_eggs)); let table = table.as_ref(); - for file in src { + for (idx, file) in src.iter().enumerate() { let file_eggs = Arc::clone(&file_eggs); scoped.execute(move || { @@ -265,11 +266,13 @@ impl<'a> Render<'a> { } else { None }; let egg = Egg { table_row, xattrs, errors, dir, file, icon }; - file_eggs.lock().unwrap().push(egg); + unsafe { std::ptr::write(file_eggs.lock().unwrap()[idx].as_mut_ptr(), egg) } }); } }); + // this is safe because all entries have been initialized above + let mut file_eggs = unsafe { std::mem::transmute::<_, Vec>(file_eggs) }; self.filter.sort_files(&mut file_eggs); for (tree_params, egg) in depth.iterate_over(file_eggs.into_iter()) {