Skip to content

Commit 642cf9c

Browse files
committed
Put hash back in cache.
1 parent df16720 commit 642cf9c

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/cargo/core/compiler/context/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct Context<'a, 'cfg> {
4040
/// Fingerprints used to detect if a unit is out-of-date.
4141
pub fingerprints: HashMap<Unit, Arc<Fingerprint>>,
4242
/// Cache of file mtimes to reduce filesystem hits.
43-
pub mtime_cache: HashMap<PathBuf, (FileTime, FileSize, FileHash)>,
43+
pub mtime_cache: HashMap<PathBuf, (FileTime, FileSize, Option<FileHash>)>,
4444
/// A set used to track which units have been compiled.
4545
/// A unit may appear in the job graph multiple times as a dependency of
4646
/// multiple packages, but it only needs to run once.

src/cargo/core/compiler/fingerprint.rs

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl LocalFingerprint {
722722
fn find_stale_item(
723723
&self,
724724
config: &Config,
725-
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, FileHash)>,
725+
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, Option<FileHash>)>,
726726
pkg_root: &Path,
727727
target_root: &Path,
728728
) -> CargoResult<Option<StaleItem>> {
@@ -991,7 +991,7 @@ impl Fingerprint {
991991
fn check_filesystem(
992992
&mut self,
993993
config: &Config,
994-
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, FileHash)>,
994+
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, Option<FileHash>)>,
995995
pkg_root: &Path,
996996
target_root: &Path,
997997
) -> CargoResult<()> {
@@ -1720,7 +1720,7 @@ fn pkg_fingerprint(bcx: &BuildContext<'_, '_>, pkg: &Package) -> CargoResult<Str
17201720
//type It = ;
17211721
fn find_stale_file(
17221722
config: &Config,
1723-
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, FileHash)>,
1723+
mtime_cache: &mut HashMap<PathBuf, (FileTime, FileSize, Option<FileHash>)>,
17241724
reference: &Path,
17251725
paths: &[(PathBuf, FileSize, FileHash)],
17261726
) -> Option<StaleItem> {
@@ -1747,15 +1747,7 @@ fn find_stale_file(
17471747
} else {
17481748
0
17491749
};
1750-
v.insert((
1751-
mtime,
1752-
current_size,
1753-
FileHash {
1754-
kind: SourceFileHashAlgorithm::Md5,
1755-
hash: String::new(),
1756-
},
1757-
))
1758-
.clone() // Hash calculated only if needed later.
1750+
v.insert((mtime, current_size, None)).clone() // Hash calculated only if needed later.
17591751
}
17601752
};
17611753

@@ -1797,37 +1789,48 @@ fn find_stale_file(
17971789

17981790
// Same size but mtime is different. Probably there's no change...
17991791
// compute hash and compare to prevent change cascade...
1800-
if config.cli_unstable().hash_tracking && reference_hash.hash.len() > 0 {
1801-
// FIXME? We could fail a little faster by seeing if any size discrepencies on _any_ file before checking hashes.
1802-
// but not sure it's worth the additional complexity.
1803-
//FIXME put the result in the mtime_cache rather than hashing each time!
1804-
let mut reader = io::BufReader::new(fs::File::open(&path).unwrap()); //FIXME
1805-
1806-
let hash = match reference_hash.kind {
1807-
SourceFileHashAlgorithm::Md5 => {
1808-
let mut hasher = Md5::new();
1809-
let mut buffer = [0; 1024];
1810-
loop {
1811-
let count = reader.read(&mut buffer).unwrap(); //FIXME
1812-
if count == 0 {
1813-
break;
1792+
if config.cli_unstable().hash_tracking && !reference_hash.hash.is_empty() {
1793+
let hash = if let Some(path_hash) = path_hash {
1794+
//FIXME use unwrap_or
1795+
path_hash.hash
1796+
} else {
1797+
// FIXME? We could fail a little faster by seeing if any size discrepencies on _any_ file before checking hashes.
1798+
// but not sure it's worth the additional complexity.
1799+
//FIXME put the result in the mtime_cache rather than hashing each time!
1800+
let mut reader = io::BufReader::new(fs::File::open(&path).unwrap()); //FIXME
1801+
1802+
let hash = match reference_hash.kind {
1803+
SourceFileHashAlgorithm::Md5 => {
1804+
let mut hasher = Md5::new();
1805+
let mut buffer = [0; 1024];
1806+
loop {
1807+
let count = reader.read(&mut buffer).unwrap(); //FIXME
1808+
if count == 0 {
1809+
break;
1810+
}
1811+
hasher.input(&buffer[..count]);
18141812
}
1815-
hasher.input(&buffer[..count]);
1813+
format!("{:?}", hasher.result())
18161814
}
1817-
format!("{:?}", hasher.result())
1818-
}
1819-
SourceFileHashAlgorithm::Sha1 => {
1820-
let mut hasher = Sha1::new();
1821-
let mut buffer = [0; 1024];
1822-
loop {
1823-
let count = reader.read(&mut buffer).unwrap(); //FIXME
1824-
if count == 0 {
1825-
break;
1815+
SourceFileHashAlgorithm::Sha1 => {
1816+
let mut hasher = Sha1::new();
1817+
let mut buffer = [0; 1024];
1818+
loop {
1819+
let count = reader.read(&mut buffer).unwrap(); //FIXME
1820+
if count == 0 {
1821+
break;
1822+
}
1823+
hasher.input(&buffer[..count]);
18261824
}
1827-
hasher.input(&buffer[..count]);
1825+
format!("{:?}", hasher.result())
18281826
}
1829-
format!("{:?}", hasher.result())
1830-
}
1827+
};
1828+
let cached = mtime_cache.get_mut(&path.to_path_buf()).unwrap();
1829+
cached.2 = Some(FileHash {
1830+
kind: reference_hash.kind,
1831+
hash: hash.clone(),
1832+
});
1833+
hash
18311834
};
18321835

18331836
if hash == reference_hash.hash {

0 commit comments

Comments
 (0)