Skip to content

Commit 39a85a6

Browse files
committed
Performance tuning, When getting the include files
1 parent f657c90 commit 39a85a6

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

src/builder.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -649,23 +649,19 @@ impl<'a> Target<'a> {
649649
log(LogLevel::Error, &format!("Could not read directory: {}", root_path));
650650
std::process::exit(1);
651651
});
652-
653-
// Convert src_only and src_exclude to a Vec<&str> for easier comparison
654652
let src_only: Vec<&str> = self.target_config.src_only.iter().map(AsRef::as_ref).collect();
655653
let src_exclude: Vec<&str> = self.target_config.src_exclude.iter().map(AsRef::as_ref).collect();
656654

657655
// Iterate over all entrys
658656
for entry in root_entries {
659657
let entry = entry.unwrap();
660658
let path = entry.path().to_str().unwrap().to_string().replace("\\", "/"); // if windows's path
661-
662659
// Exclusion logic: Check if the path is in src_exclude
663660
let exclude = src_exclude.iter().any(|&excluded| path.contains(excluded));
664661
if exclude {
665662
log(LogLevel::Debug, &format!("Excluding (in src_exclude): {}", path));
666663
continue;
667664
}
668-
669665
if entry.path().is_dir() {
670666
srcs.append(&mut self.get_srcs(&path));
671667
} else {
@@ -675,12 +671,10 @@ impl<'a> Target<'a> {
675671
} else {
676672
true // If src_only is empty, include all
677673
};
678-
679674
if !include {
680675
log(LogLevel::Debug, &format!("Excluding (not in src_only): {}", path));
681676
continue;
682677
}
683-
684678
if path.ends_with(".cpp") || path.ends_with(".c") {
685679
self.add_src(path);
686680
}
@@ -726,31 +720,35 @@ impl<'a> Target<'a> {
726720
if include_substrings.is_empty() {
727721
return result;
728722
}
729-
for include_substring in include_substrings {
730-
let mut dep_paths = Vec::new();
731-
self.target_config.include_dir.iter().for_each(|include| {
732-
let dep_path = format!("{}/{}", include, &include_substring);
733-
dep_paths.push(dep_path.clone());
734-
});
735-
if self.dependant_includes.contains_key(&include_substring) {
736-
continue;
723+
let dep_paths: Vec<String> = include_substrings.par_iter().flat_map(|include_substring| {
724+
self.target_config.include_dir.par_iter().filter_map(move |include| {
725+
let dep_path = format!("{}/{}", include, include_substring);
726+
if Path::new(&dep_path).is_file() {
727+
Some(dep_path)
728+
} else {
729+
None
730+
}
731+
})
732+
}).collect();
733+
734+
for dep_path in dep_paths {
735+
if !self.dependant_includes.contains_key(dep_path.as_str()) {
736+
result.push(dep_path.clone());
737+
self.dependant_includes.insert(dep_path.clone(), result.clone());
738+
let mut recursive_includes = self.get_dependant_includes(&dep_path);
739+
result.append(&mut recursive_includes);
737740
}
738-
// append current includes
739-
result.extend(dep_paths.clone());
740-
self.dependant_includes.insert(include_substring, result.clone());
741-
// append recursive includes
742-
dep_paths.iter().for_each(|dep_path| result.append(&mut self.get_dependant_includes(dep_path)))
743741
}
744-
//log(LogLevel::Debug, &format!("dependant_includes: {:#?}", self.dependant_includes));
745-
};
742+
}
746743
result.into_iter().unique().collect()
747744
}
748745

749746
/// Returns a list of substrings that contain "#include \"" in the source file
750747
fn get_include_substrings(&self, path: &str) -> Option<Vec<String>> {
751748
let file = std::fs::File::open(path);
752749
if file.is_err() {
753-
log(LogLevel::Warn, &format!("Failed to get include substrings for file: {}", path));
750+
// If the software is self-developed, enable this debug option
751+
//log(LogLevel::Debug, &format!("Failed to get include substrings for file: {}", path));
754752
return None;
755753
}
756754
let mut file = file.unwrap();

src/hasher.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Hasher {
6767
if path_hash.contains_key(path) {
6868
return Some(path_hash.get(path).unwrap().to_string());
6969
}
70-
return None;
70+
None
7171
}
7272

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

9797
/// Saves the hashes to a file.
@@ -173,7 +173,6 @@ impl Hasher {
173173
if hash != new_hash {
174174
log(LogLevel::Info, &format!("File changed, updating hash for file: {}", path));
175175
path_hash.insert(path.to_string(), new_hash);
176-
return;
177176
}
178177
}
179178
}

0 commit comments

Comments
 (0)