Fix massive memory allocation issue
Not exactly sure what was going on, other than cloning of the FileMatch with its results, since it is recursive I believe this blew up the stack/heap with unnecessary allocations. By using an iterator and filter this seemed to greatly reduce memory use and improve speed.
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -19,26 +19,32 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn traverse_dir(config: &config::Config, dir: &Path, operation: fn(&config::Config, &Path) -> io::Result<FileMatch>)
|
||||
-> io::Result<Vec<FileMatch>> {
|
||||
|
||||
fn traverse_dir(
|
||||
config: &config::Config,
|
||||
dir: &Path,
|
||||
operation: fn(&config::Config, &Path) -> io::Result<FileMatch>,
|
||||
) -> io::Result<Vec<FileMatch>> {
|
||||
let mut matches: Vec<FileMatch> = Vec::new();
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let file_type = entry.file_type()?;
|
||||
|
||||
if file_type.is_dir() {
|
||||
let results = traverse_dir(&config, &entry.path(), operation)?;
|
||||
for mat in results.as_slice() {
|
||||
if mat.matches.len() > 0 {
|
||||
matches.append(&mut results.clone());
|
||||
}
|
||||
let mut results = traverse_dir(&config, &entry.path(), operation)?;
|
||||
let has_results = results
|
||||
.iter()
|
||||
.filter({ |mat| mat.matches.len() > 0 })
|
||||
.count()
|
||||
> 0;
|
||||
|
||||
if has_results {
|
||||
matches.append(&mut results);
|
||||
}
|
||||
} else {
|
||||
let result = operation(&config, &entry.path());
|
||||
match result {
|
||||
Result::Ok(result) if result.matches.len() > 0 => matches.push(result),
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +52,7 @@ fn traverse_dir(config: &config::Config, dir: &Path, operation: fn(&config::Conf
|
||||
Ok(matches)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug)]
|
||||
struct FileMatch {
|
||||
file_name: String,
|
||||
matches: Vec<Match>
|
||||
|
||||
Reference in New Issue
Block a user