feat: only compile changed files (#544)

This commit is contained in:
Georgios Konstantopoulos 2021-10-30 21:27:17 +03:00 committed by GitHub
parent 5c6ce6b0a1
commit 46bedb3282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View File

@ -58,9 +58,17 @@ impl SolFilesCache {
self.files.retain(|file, _| Path::new(file).exists())
}
/// Returns if true if a source has changed and false if no source has changed
pub fn is_changed(&self, sources: &Sources, config: Option<&SolcConfig>) -> bool {
sources.iter().any(|(file, source)| self.has_changed(file, source.content_hash(), config))
/// Returns only the files that were changed from the provided sources, to save time
/// when compiling.
pub fn get_changed_files<'a>(
&'a self,
sources: Sources,
config: Option<&'a SolcConfig>,
) -> Sources {
sources
.into_iter()
.filter(move |(file, source)| self.has_changed(file, source.content_hash(), config))
.collect()
}
/// Returns true if the given content hash or config differs from the file's

View File

@ -105,13 +105,17 @@ impl Project {
source_name_path.insert(import, path);
}
if self.cached && self.paths.cache.exists() {
// check anything changed
// If there's a cache set, filter to only re-compile the files which were changed
let sources = if self.cached && self.paths.cache.exists() {
let cache = SolFilesCache::read(&self.paths.cache)?;
if !cache.is_changed(&sources, Some(&self.solc_config)) {
let changed_files = cache.get_changed_files(sources, Some(&self.solc_config));
if changed_files.is_empty() {
return Ok(ProjectCompileOutput::Unchanged)
}
}
changed_files
} else {
sources
};
// replace absolute path with source name to make solc happy
let sources = apply_mappings(sources, path_source_name);