Skip to content

Commit

Permalink
Performance tuning, When getting the include files
Browse files Browse the repository at this point in the history
  • Loading branch information
BeichenY1 committed Mar 3, 2024
1 parent f657c90 commit 39a85a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
42 changes: 20 additions & 22 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,23 +649,19 @@ impl<'a> Target<'a> {
log(LogLevel::Error, &format!("Could not read directory: {}", root_path));
std::process::exit(1);
});

// Convert src_only and src_exclude to a Vec<&str> for easier comparison
let src_only: Vec<&str> = self.target_config.src_only.iter().map(AsRef::as_ref).collect();
let src_exclude: Vec<&str> = self.target_config.src_exclude.iter().map(AsRef::as_ref).collect();

// Iterate over all entrys
for entry in root_entries {
let entry = entry.unwrap();
let path = entry.path().to_str().unwrap().to_string().replace("\\", "/"); // if windows's path

// Exclusion logic: Check if the path is in src_exclude
let exclude = src_exclude.iter().any(|&excluded| path.contains(excluded));
if exclude {
log(LogLevel::Debug, &format!("Excluding (in src_exclude): {}", path));
continue;
}

if entry.path().is_dir() {
srcs.append(&mut self.get_srcs(&path));
} else {
Expand All @@ -675,12 +671,10 @@ impl<'a> Target<'a> {
} else {
true // If src_only is empty, include all
};

if !include {
log(LogLevel::Debug, &format!("Excluding (not in src_only): {}", path));
continue;
}

if path.ends_with(".cpp") || path.ends_with(".c") {
self.add_src(path);
}
Expand Down Expand Up @@ -726,31 +720,35 @@ impl<'a> Target<'a> {
if include_substrings.is_empty() {
return result;
}
for include_substring in include_substrings {
let mut dep_paths = Vec::new();
self.target_config.include_dir.iter().for_each(|include| {
let dep_path = format!("{}/{}", include, &include_substring);
dep_paths.push(dep_path.clone());
});
if self.dependant_includes.contains_key(&include_substring) {
continue;
let dep_paths: Vec<String> = include_substrings.par_iter().flat_map(|include_substring| {
self.target_config.include_dir.par_iter().filter_map(move |include| {
let dep_path = format!("{}/{}", include, include_substring);
if Path::new(&dep_path).is_file() {
Some(dep_path)
} else {
None
}
})
}).collect();

for dep_path in dep_paths {
if !self.dependant_includes.contains_key(dep_path.as_str()) {
result.push(dep_path.clone());
self.dependant_includes.insert(dep_path.clone(), result.clone());
let mut recursive_includes = self.get_dependant_includes(&dep_path);
result.append(&mut recursive_includes);
}
// append current includes
result.extend(dep_paths.clone());
self.dependant_includes.insert(include_substring, result.clone());
// append recursive includes
dep_paths.iter().for_each(|dep_path| result.append(&mut self.get_dependant_includes(dep_path)))
}
//log(LogLevel::Debug, &format!("dependant_includes: {:#?}", self.dependant_includes));
};
}
result.into_iter().unique().collect()
}

/// Returns a list of substrings that contain "#include \"" in the source file
fn get_include_substrings(&self, path: &str) -> Option<Vec<String>> {
let file = std::fs::File::open(path);
if file.is_err() {
log(LogLevel::Warn, &format!("Failed to get include substrings for file: {}", path));
// If the software is self-developed, enable this debug option
//log(LogLevel::Debug, &format!("Failed to get include substrings for file: {}", path));
return None;
}
let mut file = file.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Hasher {
if path_hash.contains_key(path) {
return Some(path_hash.get(path).unwrap().to_string());
}
return None;
None
}

/// Loads the hashes from a file and returns them as a hashmap.
Expand All @@ -91,7 +91,7 @@ impl Hasher {
let hash = split.next().unwrap();
path_hash.insert(path.to_string(), hash.to_string());
}
return path_hash;
path_hash
}

/// Saves the hashes to a file.
Expand Down Expand Up @@ -173,7 +173,6 @@ impl Hasher {
if hash != new_hash {
log(LogLevel::Info, &format!("File changed, updating hash for file: {}", path));
path_hash.insert(path.to_string(), new_hash);
return;
}
}
}

0 comments on commit 39a85a6

Please sign in to comment.