Skip to content

Commit a725ebe

Browse files
committed
add support for xxhash, denote which algorithms cargo supports
1 parent 683bda9 commit a725ebe

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

Cargo.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
22272227
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
22282228
dependencies = [
22292229
"cfg-if",
2230-
"windows-targets 0.48.5",
2230+
"windows-targets 0.52.5",
22312231
]
22322232

22332233
[[package]]
@@ -4776,6 +4776,7 @@ dependencies = [
47764776
"sha2",
47774777
"tracing",
47784778
"unicode-width",
4779+
"xxhash-rust",
47794780
]
47804781

47814782
[[package]]
@@ -6620,6 +6621,12 @@ version = "0.13.6"
66206621
source = "registry+https://github.com/rust-lang/crates.io-index"
66216622
checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4"
66226623

6624+
[[package]]
6625+
name = "xxhash-rust"
6626+
version = "0.8.10"
6627+
source = "registry+https://github.com/rust-lang/crates.io-index"
6628+
checksum = "927da81e25be1e1a2901d59b81b37dd2efd1fc9c9345a55007f09bf5a2d3ee03"
6629+
66236630
[[package]]
66246631
name = "xz2"
66256632
version = "0.1.7"

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
636636
rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
637637
rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
638638
rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
639+
rustc_span::SourceFileHashAlgorithm::XxHash => llvm::ChecksumKind::None,
639640
};
640641
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
641642

compiler/rustc_session/src/options.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ mod desc {
412412
pub const parse_merge_functions: &str = "one of: `disabled`, `trampolines`, or `aliases`";
413413
pub const parse_symbol_mangling_version: &str =
414414
"one of: `legacy`, `v0` (RFC 2603), or `hashed`";
415-
pub const parse_src_file_hash: &str = "either `md5`, `sha1`, or `sha256`";
415+
pub const parse_cargo_src_file_hash: &str = "one of `sha256` or `xxhash`";
416+
pub const parse_src_file_hash: &str = "one of `md5`, `sha1` `sha256`, or `xxhash`";
416417
pub const parse_relocation_model: &str =
417418
"one of supported relocation models (`rustc --print relocation-models`)";
418419
pub const parse_code_model: &str = "one of supported code models (`rustc --print code-models`)";
@@ -1222,6 +1223,23 @@ mod parse {
12221223
true
12231224
}
12241225

1226+
pub(crate) fn parse_cargo_src_file_hash(
1227+
slot: &mut Option<SourceFileHashAlgorithm>,
1228+
v: Option<&str>,
1229+
) -> bool {
1230+
match v.and_then(|s| SourceFileHashAlgorithm::from_str(s).ok()) {
1231+
Some(hash_kind) => {
1232+
if hash_kind.supported_in_cargo() {
1233+
*slot = Some(hash_kind);
1234+
} else {
1235+
return false;
1236+
}
1237+
}
1238+
_ => return false,
1239+
}
1240+
true
1241+
}
1242+
12251243
pub(crate) fn parse_target_feature(slot: &mut String, v: Option<&str>) -> bool {
12261244
match v {
12271245
Some(s) => {
@@ -1609,8 +1627,8 @@ options! {
16091627
"instrument control-flow architecture protection"),
16101628
check_cfg_all_expected: bool = (false, parse_bool, [UNTRACKED],
16111629
"show all expected values in check-cfg diagnostics (default: no)"),
1612-
checksum_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
1613-
"hash algorithm of source files used to check freshness in cargo (`md5`, `sha1`, or `sha256`)"),
1630+
checksum_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_cargo_src_file_hash, [TRACKED],
1631+
"hash algorithm of source files used to check freshness in cargo (`sha256` or `xxhash`)"),
16141632
codegen_backend: Option<String> = (None, parse_opt_string, [TRACKED],
16151633
"the backend to use"),
16161634
combine_cgu: bool = (false, parse_bool, [TRACKED],

compiler/rustc_span/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ sha1 = "0.10.0"
1919
sha2 = "0.10.1"
2020
tracing = "0.1"
2121
unicode-width = "0.1.4"
22+
xxhash-rust = { version = "0.8.10", features = ["xxh3"] }
2223
# tidy-alphabetical-end

compiler/rustc_span/src/lib.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4343
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
4444
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4545
use tracing::debug;
46+
use xxhash_rust::xxh3;
4647

4748
mod caching_source_map_view;
4849
pub mod source_map;
@@ -1458,14 +1459,25 @@ pub enum SourceFileHashAlgorithm {
14581459
Md5,
14591460
Sha1,
14601461
Sha256,
1462+
XxHash,
1463+
}
1464+
1465+
impl SourceFileHashAlgorithm {
1466+
pub fn supported_in_cargo(&self) -> bool {
1467+
match self {
1468+
Self::Md5 | Self::Sha1 => false,
1469+
Self::Sha256 | Self::XxHash => true,
1470+
}
1471+
}
14611472
}
14621473

14631474
impl Display for SourceFileHashAlgorithm {
14641475
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14651476
f.write_str(match self {
1466-
SourceFileHashAlgorithm::Md5 => "md5",
1467-
SourceFileHashAlgorithm::Sha1 => "sha1",
1468-
SourceFileHashAlgorithm::Sha256 => "sha256",
1477+
Self::Md5 => "md5",
1478+
Self::Sha1 => "sha1",
1479+
Self::Sha256 => "sha256",
1480+
Self::XxHash => "xxhash",
14691481
})
14701482
}
14711483
}
@@ -1478,6 +1490,7 @@ impl FromStr for SourceFileHashAlgorithm {
14781490
"md5" => Ok(SourceFileHashAlgorithm::Md5),
14791491
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
14801492
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
1493+
"xxhash" => Ok(SourceFileHashAlgorithm::XxHash),
14811494
_ => Err(()),
14821495
}
14831496
}
@@ -1517,6 +1530,9 @@ impl SourceFileHash {
15171530
SourceFileHashAlgorithm::Sha256 => {
15181531
value.copy_from_slice(&Sha256::digest(data));
15191532
}
1533+
SourceFileHashAlgorithm::XxHash => {
1534+
value.copy_from_slice(&xxh3::xxh3_128(data).to_be_bytes());
1535+
}
15201536
};
15211537
hash
15221538
}
@@ -1551,6 +1567,16 @@ impl SourceFileHash {
15511567
}
15521568

15531569
match kind {
1570+
SourceFileHashAlgorithm::XxHash => {
1571+
digest(
1572+
xxh3::Xxh3::new(),
1573+
|h, b| h.update(b),
1574+
|h, out| out.copy_from_slice(&h.digest128().to_be_bytes()),
1575+
src,
1576+
&mut buf,
1577+
value,
1578+
)?;
1579+
}
15541580
SourceFileHashAlgorithm::Sha256 => {
15551581
digest(
15561582
Sha256::new(),
@@ -1607,6 +1633,7 @@ impl SourceFileHash {
16071633
SourceFileHashAlgorithm::Md5 => 16,
16081634
SourceFileHashAlgorithm::Sha1 => 20,
16091635
SourceFileHashAlgorithm::Sha256 => 32,
1636+
SourceFileHashAlgorithm::XxHash => 16,
16101637
}
16111638
}
16121639
}

0 commit comments

Comments
 (0)