Skip to content

Commit

Permalink
Use separate cache for watchman-enabled scans
Browse files Browse the repository at this point in the history
Writing list of all files takes non-trivial amount of time
  • Loading branch information
Dmitry Bogatov committed Mar 22, 2024
1 parent bf4ef26 commit 34ddc73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub async fn run_treefmt(
assert!(work_dir.is_absolute());
assert!(cache_dir.is_absolute());
assert!(treefmt_toml.is_absolute());
let flavor = if watchman.is_some() {
"watchman"
} else {
"stat"
};

let mut stats = Statistics::init();

Expand Down Expand Up @@ -91,7 +96,7 @@ pub async fn run_treefmt(
// Start with an empty cache
CacheManifest::default()
} else {
CacheManifest::load(cache_dir, treefmt_toml)
CacheManifest::load(cache_dir, treefmt_toml, flavor)
};
stats.timed_debug("load cache");

Expand Down Expand Up @@ -298,7 +303,7 @@ pub async fn run_treefmt(
cache.add_results(new_matches.clone());
};
// And write to disk
cache.write(cache_dir, treefmt_toml);
cache.write(cache_dir, treefmt_toml, flavor);
stats.timed_debug("write cache");
}

Expand Down
20 changes: 10 additions & 10 deletions src/eval_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl Clone for CacheManifest {

impl CacheManifest {
/// Loads the manifest and returns an error if it failed
pub fn try_load(cache_dir: &Path, treefmt_toml: &Path) -> Result<Self> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml);
pub fn try_load(cache_dir: &Path, treefmt_toml: &Path, flavor: &str) -> Result<Self> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml, flavor);
debug!("cache: loading from {}", manifest_path.display());
let content = read_to_string(&manifest_path)?;
let manifest = toml::from_str(&content)?;
Expand All @@ -68,8 +68,8 @@ impl CacheManifest {

/// Always loads the manifest. If an error occurred, log and return an empty manifest.
#[must_use]
pub fn load(cache_dir: &Path, treefmt_toml: &Path) -> Self {
match Self::try_load(cache_dir, treefmt_toml) {
pub fn load(cache_dir: &Path, treefmt_toml: &Path, flavor: &str) -> Self {
match Self::try_load(cache_dir, treefmt_toml, flavor) {
Ok(manifest) => manifest,
Err(err) => {
warn!("cache: failed to load the manifest due to: {}", err);
Expand All @@ -79,8 +79,8 @@ impl CacheManifest {
}

/// Serializes back the manifest into place.
pub fn try_write(self, cache_dir: &Path, treefmt_toml: &Path) -> Result<()> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml);
pub fn try_write(self, cache_dir: &Path, treefmt_toml: &Path, flavor: &str) -> Result<()> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml, flavor);
debug!("cache: writing to {}", manifest_path.display());
// Make sure the cache directory exists.
create_dir_all(manifest_path.parent().unwrap())?;
Expand All @@ -97,8 +97,8 @@ impl CacheManifest {
}

/// Serializes back the manifest into place.
pub fn write(self, cache_dir: &Path, treefmt_toml: &Path) {
if let Err(err) = self.try_write(cache_dir, treefmt_toml) {
pub fn write(self, cache_dir: &Path, treefmt_toml: &Path, flavor: &str) {
if let Err(err) = self.try_write(cache_dir, treefmt_toml, flavor) {
warn!("cache: failed to write to disk: {}", err);
};
}
Expand Down Expand Up @@ -198,14 +198,14 @@ fn load_formatter_info(fmt: Formatter) -> Result<FormatterInfo> {
}

/// Derive the manifest filename from the treefmt_toml path.
fn get_manifest_path(cache_dir: &Path, treefmt_toml: &Path) -> PathBuf {
fn get_manifest_path(cache_dir: &Path, treefmt_toml: &Path, flavor: &str) -> PathBuf {
assert!(cache_dir.is_absolute());
assert!(treefmt_toml.is_absolute());
// FIXME: it's a shame that we can't access the underlying OsStr bytes
let path_bytes = treefmt_toml.to_string_lossy();
// Hash the config path
let treefmt_hash = Sha1::digest(path_bytes.as_bytes());
// Hexencode
let filename = format!("{:x}.toml", treefmt_hash);
let filename = format!("{:x}.{}.toml", treefmt_hash, flavor);
cache_dir.join(filename)
}

0 comments on commit 34ddc73

Please sign in to comment.