Skip to content

Commit

Permalink
cache_dir: further optimise for the common case
Browse files Browse the repository at this point in the history
Optimistically publish the file operation in `put` and `set`; only
when that fails do we ensure the parent directory exists and retry
the file operation.

TESTED=existing tests.
  • Loading branch information
pkhuong committed Sep 23, 2021
1 parent 294bed6 commit 1a12b6e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/cache_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ pub(crate) trait CacheDir {
let mut dst = self.base_dir().into_owned();

let ret = self.maybe_cleanup(&dst)?;
ensure_directory(&dst)?;
// Optimistically try to publish the file, without
// making sure the directory exists: if this works,
// the directory must have been found.
dst.push(name);
if raw_cache::insert_or_update(value, &dst).is_ok() {
return Ok(ret);
}

// We just pushed `name`, we must have a parent.
std::fs::create_dir_all(dst.parent().expect("must have parent"))?;
raw_cache::insert_or_update(value, &dst)?;
Ok(ret)
}
Expand All @@ -204,8 +212,13 @@ pub(crate) trait CacheDir {
let mut dst = self.base_dir().into_owned();

let ret = self.maybe_cleanup(&dst)?;
ensure_directory(&dst)?;

dst.push(name);
if raw_cache::insert_or_touch(value, &dst).is_ok() {
return Ok(ret);
}

std::fs::create_dir_all(dst.parent().expect("must have parent"))?;
raw_cache::insert_or_touch(value, &dst)?;
Ok(ret)
}
Expand Down

0 comments on commit 1a12b6e

Please sign in to comment.