Skip to content

Commit 0675c0e

Browse files
committed
Optimisation: No need to figure out if bin files are up to date if they have svh hashes in their filenames.
1 parent fecc6da commit 0675c0e

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ pub type FileSize = u64;
350350

351351
#[derive(Clone)]
352352
pub struct FileHash {
353-
kind: SourceFileHashAlgorithm,
353+
kind: FileHashAlgorithm,
354354
hash: String,
355355
}
356356

@@ -763,7 +763,7 @@ impl LocalFingerprint {
763763
pkg_root.join(p),
764764
0u64,
765765
FileHash {
766-
kind: SourceFileHashAlgorithm::Md5,
766+
kind: FileHashAlgorithm::Md5,
767767
hash: String::new(),
768768
},
769769
)
@@ -1800,7 +1800,7 @@ fn find_stale_file(
18001800
let mut reader = io::BufReader::new(fs::File::open(&path).unwrap()); //FIXME
18011801

18021802
let hash = match reference_hash.kind {
1803-
SourceFileHashAlgorithm::Md5 => {
1803+
FileHashAlgorithm::Md5 => {
18041804
let mut hasher = Md5::new();
18051805
let mut buffer = [0; 1024];
18061806
loop {
@@ -1812,7 +1812,7 @@ fn find_stale_file(
18121812
}
18131813
format!("{:?}", hasher.result())
18141814
}
1815-
SourceFileHashAlgorithm::Sha1 => {
1815+
FileHashAlgorithm::Sha1 => {
18161816
let mut hasher = Sha1::new();
18171817
let mut buffer = [0; 1024];
18181818
loop {
@@ -1824,6 +1824,9 @@ fn find_stale_file(
18241824
}
18251825
format!("{:?}", hasher.result())
18261826
}
1827+
FileHashAlgorithm::Filename => {
1828+
"0".to_string()
1829+
}
18271830
};
18281831
let cached = mtime_cache.get_mut(&path.to_path_buf()).unwrap();
18291832
cached.2 = Some(FileHash {
@@ -1854,18 +1857,22 @@ fn find_stale_file(
18541857
}
18551858

18561859
#[derive(Clone, Copy, Eq, PartialEq)]
1857-
pub enum SourceFileHashAlgorithm {
1860+
pub enum FileHashAlgorithm {
18581861
Md5,
18591862
Sha1,
1863+
/// If the hash is in the filename then as long as the file exists we can
1864+
/// assume it is up to date.
1865+
Filename,
18601866
}
18611867

1862-
impl FromStr for SourceFileHashAlgorithm {
1868+
impl FromStr for FileHashAlgorithm {
18631869
type Err = ();
18641870

1865-
fn from_str(s: &str) -> Result<SourceFileHashAlgorithm, ()> {
1871+
fn from_str(s: &str) -> Result<FileHashAlgorithm, ()> {
18661872
match s {
1867-
"md5" => Ok(SourceFileHashAlgorithm::Md5),
1868-
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
1873+
"md5" => Ok(FileHashAlgorithm::Md5),
1874+
"sha1" => Ok(FileHashAlgorithm::Sha1),
1875+
"hash_in_filename" => Ok(FileHashAlgorithm::Filename),
18691876
_ => Err(()),
18701877
}
18711878
}
@@ -2015,8 +2022,9 @@ impl EncodedDepInfo {
20152022

20162023
//debug!("read hash as {}", hash);
20172024
let kind = match read_u8(bytes)? {
2018-
0 => SourceFileHashAlgorithm::Md5,
2019-
1 => SourceFileHashAlgorithm::Sha1,
2025+
0 => FileHashAlgorithm::Md5,
2026+
1 => FileHashAlgorithm::Sha1,
2027+
2 => FileHashAlgorithm::Filename,
20202028
_ => return None,
20212029
};
20222030
let ty = match read_u8(bytes)? {
@@ -2097,8 +2105,9 @@ impl EncodedDepInfo {
20972105
write_bytes(dst, hash.hash.as_bytes());
20982106
//write(dst, hash.hash);
20992107
match hash.kind {
2100-
SourceFileHashAlgorithm::Md5 => dst.push(0),
2101-
SourceFileHashAlgorithm::Sha1 => dst.push(1),
2108+
FileHashAlgorithm::Md5 => dst.push(0),
2109+
FileHashAlgorithm::Sha1 => dst.push(1),
2110+
FileHashAlgorithm::Filename => dst.push(2),
21022111
}
21032112
match ty {
21042113
DepInfoPathType::PackageRootRelative => dst.push(0),
@@ -2179,7 +2188,7 @@ pub fn parse_rustc_dep_info(rustc_dep_info: &Path) -> CargoResult<RustcDepInfo>
21792188
let kind_hash: Vec<_> = parts[1].split(":").collect();
21802189
let hash = kind_hash[1];
21812190
ret.files[i].2 = FileHash {
2182-
kind: SourceFileHashAlgorithm::from_str(kind_hash[0])
2191+
kind: FileHashAlgorithm::from_str(kind_hash[0])
21832192
.expect("unknown hashing algo"),
21842193
hash: hash.to_string(),
21852194
};
@@ -2208,7 +2217,7 @@ pub fn parse_rustc_dep_info(rustc_dep_info: &Path) -> CargoResult<RustcDepInfo>
22082217
file.into(),
22092218
0,
22102219
FileHash {
2211-
kind: SourceFileHashAlgorithm::Md5,
2220+
kind: FileHashAlgorithm::Md5,
22122221
hash: String::new(),
22132222
},
22142223
));

0 commit comments

Comments
 (0)