Optimize file table creation, fixes #484 as a side-effect

This commit is contained in:
FliegendeWurst 2019-10-12 13:27:59 +02:00
parent 89c861f24d
commit bbd6db3687
No known key found for this signature in database
GPG Key ID: CA38E82B54B32A88

View File

@ -61,6 +61,7 @@
use std::io::{Write, Error as IOError, Result as IOResult}; use std::io::{Write, Error as IOError, Result as IOResult};
use std::mem::MaybeUninit;
use std::path::PathBuf; use std::path::PathBuf;
use std::vec::IntoIter as VecIntoIter; use std::vec::IntoIter as VecIntoIter;
@ -192,13 +193,13 @@ impl<'a> Render<'a> {
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use fs::feature::xattr; use fs::feature::xattr;
let mut file_eggs = Vec::new(); let mut file_eggs = (0..src.len()).map(|_| MaybeUninit::uninit()).collect::<Vec<_>>();
pool.scoped(|scoped| { pool.scoped(|scoped| {
let file_eggs = Arc::new(Mutex::new(&mut file_eggs)); let file_eggs = Arc::new(Mutex::new(&mut file_eggs));
let table = table.as_ref(); let table = table.as_ref();
for file in src { for (idx, file) in src.iter().enumerate() {
let file_eggs = Arc::clone(&file_eggs); let file_eggs = Arc::clone(&file_eggs);
scoped.execute(move || { scoped.execute(move || {
@ -265,11 +266,13 @@ impl<'a> Render<'a> {
} else { None }; } else { None };
let egg = Egg { table_row, xattrs, errors, dir, file, icon }; 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<Egg>>(file_eggs) };
self.filter.sort_files(&mut file_eggs); self.filter.sort_files(&mut file_eggs);
for (tree_params, egg) in depth.iterate_over(file_eggs.into_iter()) { for (tree_params, egg) in depth.iterate_over(file_eggs.into_iter()) {