Skip to content

Commit 1a052ae

Browse files
committed
Auto merge of #10507 - Eh2406:finer_grained_cashe, r=weihanglo
File Cache is valid if checkout or contents hasn't changed #10482 allow the registry to have cashe keys at a per file level. On master if the currently checked out commit changes all cached files are regenerated. After this commit we also check if GITs hash of the particular file has changed. To avoid thrashing cached files this PR only checks for the presence of both hashes, but does not write them both. This means that nightly after this branch will still be able to share file caches with older versions, specifically current stable. When sufficient versions of stable know how to read the new format, a one line change can be made to start writing the new format.
2 parents 01c06b0 + 8058c3d commit 1a052ae

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/cargo/sources/registry/remote.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
159159
self.config.assert_package_cache_locked(path)
160160
}
161161

162+
// `index_version` Is a string representing the version of the file used to construct the cached copy.
163+
// Older versions of Cargo used the single value of the hash of the HEAD commit as a `index_version`.
164+
// This is technically correct but a little too conservative. If a new commit is fetched all cached
165+
// files need to be regenerated even if a particular file was not changed.
166+
// Cargo now reads the `index_version` in two parts the cache file is considered valid if `index_version`
167+
// ends with the hash of the HEAD commit OR if it starts with the hash of the file's contents.
168+
// In the future cargo can write cached files with `index_version` = `git_file_hash + ":" + `git_commit_hash`,
169+
// but for now it still uses `git_commit_hash` to be compatible with older Cargoes.
162170
fn load(
163171
&mut self,
164172
_root: &Path,
@@ -169,22 +177,33 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
169177
return Poll::Pending;
170178
}
171179
// Check if the cache is valid.
172-
let current_version = self.current_version();
173-
if current_version.is_some() && current_version.as_deref() == index_version {
174-
return Poll::Ready(Ok(LoadResponse::CacheValid));
180+
let git_commit_hash = self.current_version();
181+
if let (Some(c), Some(i)) = (git_commit_hash, index_version) {
182+
if i.ends_with(c.as_str()) {
183+
return Poll::Ready(Ok(LoadResponse::CacheValid));
184+
}
175185
}
176186
// Note that the index calls this method and the filesystem is locked
177187
// in the index, so we don't need to worry about an `update_index`
178188
// happening in a different process.
179189
fn load_helper(
180190
registry: &RemoteRegistry<'_>,
181191
path: &Path,
182-
current_version: Option<&str>,
192+
index_version: Option<&str>,
193+
git_commit_hash: Option<&str>,
183194
) -> CargoResult<LoadResponse> {
184195
let repo = registry.repo()?;
185196
let tree = registry.tree()?;
186197
let entry = tree.get_path(path);
187198
let entry = entry?;
199+
let git_file_hash = entry.id().to_string();
200+
201+
if let Some(i) = index_version {
202+
if i.starts_with(git_file_hash.as_str()) {
203+
return Ok(LoadResponse::CacheValid);
204+
}
205+
}
206+
188207
let object = entry.to_object(repo)?;
189208
let blob = match object.as_blob() {
190209
Some(blob) => blob,
@@ -193,11 +212,13 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
193212

194213
Ok(LoadResponse::Data {
195214
raw_data: blob.content().to_vec(),
196-
index_version: current_version.map(String::from),
215+
index_version: git_commit_hash.map(String::from),
216+
// TODO: When the reading code has been stable for long enough (Say 8/2022)
217+
// change to `git_file_hash + ":" + git_commit_hash`
197218
})
198219
}
199220

200-
match load_helper(&self, path, current_version.as_deref()) {
221+
match load_helper(&self, path, index_version, git_commit_hash.as_deref()) {
201222
Ok(result) => Poll::Ready(Ok(result)),
202223
Err(_) if !self.updated => {
203224
// If git returns an error and we haven't updated the repo, return

0 commit comments

Comments
 (0)