From 46bedb3282aa3386cbf7b5ba81820041d86dabd1 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Sat, 30 Oct 2021 21:27:17 +0300 Subject: [PATCH] feat: only compile changed files (#544) --- ethers-solc/src/cache.rs | 14 +++++++++++--- ethers-solc/src/lib.rs | 12 ++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ethers-solc/src/cache.rs b/ethers-solc/src/cache.rs index 0a1ecaa3..ae12591f 100644 --- a/ethers-solc/src/cache.rs +++ b/ethers-solc/src/cache.rs @@ -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 diff --git a/ethers-solc/src/lib.rs b/ethers-solc/src/lib.rs index bfcf9cb5..5b873449 100644 --- a/ethers-solc/src/lib.rs +++ b/ethers-solc/src/lib.rs @@ -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);