Skip to content

Commit 8de59be

Browse files
committed
Compress amount of hashed bytes for isize values in StableHasher
1 parent e0e70c0 commit 8de59be

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,35 @@ impl Hasher for StableHasher {
137137
// platforms. This is important for symbol hashes when cross compiling,
138138
// for example. Sign extending here is preferable as it means that the
139139
// same negative number hashes the same on both 32 and 64 bit platforms.
140-
self.state.write_i64((i as i64).to_le());
140+
let value = (i as i64).to_le() as u64;
141+
142+
// Cold path
143+
#[cold]
144+
#[inline(never)]
145+
fn hash_value(state: &mut SipHasher128, value: u64) {
146+
state.write_u8(0xFF);
147+
state.write_u64(value);
148+
}
149+
150+
// `isize` values often seem to have a small (positive) numeric value in practice.
151+
// To exploit this, if the value is small, we will hash a smaller amount of bytes.
152+
// However, we cannot just skip the leading zero bytes, as that would produce the same hash
153+
// e.g. if you hash two values that have the same bit pattern when they are swapped.
154+
// See https://github.com/rust-lang/rust/pull/93014 for context.
155+
//
156+
// Therefore, we employ the following strategy:
157+
// 1) When we encounter a value that fits within a single byte (the most common case), we
158+
// hash just that byte. This is the most common case that is being optimized. However, we do
159+
// not do this for the value 0xFF, as that is a reserved prefix (a bit like in UTF-8).
160+
// 2) When we encounter a larger value, we hash a "marker" 0xFF and then the corresponding
161+
// 8 bytes. Since this prefix cannot occur when we hash a single byte, when we hash two
162+
// `isize`s that fit within a different amount of bytes, they should always produce a different
163+
// byte stream for the hasher.
164+
if value < 0xFF {
165+
self.state.write_u8(value as u8);
166+
} else {
167+
hash_value(&mut self.state, value);
168+
}
141169
}
142170
}
143171

compiler/rustc_data_structures/src/stable_hasher/tests.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn test_hash_integers() {
3939
test_isize.hash(&mut h);
4040

4141
// This depends on the hashing algorithm. See note at top of file.
42-
let expected = (2736651863462566372, 8121090595289675650);
42+
let expected = (1784307454142909076, 11471672289340283879);
4343

4444
assert_eq!(h.finalize(), expected);
4545
}
@@ -67,7 +67,7 @@ fn test_hash_isize() {
6767
test_isize.hash(&mut h);
6868

6969
// This depends on the hashing algorithm. See note at top of file.
70-
let expected = (14721296605626097289, 11385941877786388409);
70+
let expected = (2789913510339652884, 674280939192711005);
7171

7272
assert_eq!(h.finalize(), expected);
7373
}
@@ -140,3 +140,23 @@ fn test_attribute_permutation() {
140140
test_type!(i64);
141141
test_type!(i128);
142142
}
143+
144+
// Check that the `isize` hashing optimization does not produce the same hash when permuting two
145+
// values.
146+
#[test]
147+
fn test_isize_compression() {
148+
fn check_hash(a: u64, b: u64) {
149+
let hash_a = hash(&(a as isize, b as isize));
150+
let hash_b = hash(&(b as isize, a as isize));
151+
assert_ne!(
152+
hash_a, hash_b,
153+
"The hash stayed the same when permuting values `{a}` and `{b}!",
154+
);
155+
}
156+
157+
check_hash(0xAA, 0xAAAA);
158+
check_hash(0xFF, 0xFFFF);
159+
check_hash(0xAAAA, 0xAAAAAA);
160+
check_hash(0xAAAAAA, 0xAAAAAAAA);
161+
check_hash(0xFF, 0xFFFFFFFFFFFFFFFF);
162+
}

src/test/debuginfo/function-names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Const generic parameter
3838
// gdb-command:info functions -q function_names::const_generic_fn.*
3939
// gdb-check:[...]static fn function_names::const_generic_fn_bool<false>();
40-
// gdb-check:[...]static fn function_names::const_generic_fn_non_int<{CONST#fe3cfa0214ac55c7}>();
40+
// gdb-check:[...]static fn function_names::const_generic_fn_non_int<{CONST#3fcd7c34c1555be6}>();
4141
// gdb-check:[...]static fn function_names::const_generic_fn_signed_int<-7>();
4242
// gdb-check:[...]static fn function_names::const_generic_fn_unsigned_int<14>();
4343

@@ -76,7 +76,7 @@
7676
// Const generic parameter
7777
// cdb-command:x a!function_names::const_generic_fn*
7878
// cdb-check:[...] a!function_names::const_generic_fn_bool<false> (void)
79-
// cdb-check:[...] a!function_names::const_generic_fn_non_int<CONST$fe3cfa0214ac55c7> (void)
79+
// cdb-check:[...] a!function_names::const_generic_fn_non_int<CONST$3fcd7c34c1555be6> (void)
8080
// cdb-check:[...] a!function_names::const_generic_fn_unsigned_int<14> (void)
8181
// cdb-check:[...] a!function_names::const_generic_fn_signed_int<-7> (void)
8282

src/test/ui/symbol-names/basic.legacy.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN5basic4main17h7c2c715a9b77648bE)
1+
error: symbol-name(_ZN5basic4main17h611df9c6948c15f7E)
22
--> $DIR/basic.rs:8:1
33
|
44
LL | #[rustc_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(basic::main::h7c2c715a9b77648b)
7+
error: demangling(basic::main::h611df9c6948c15f7)
88
--> $DIR/basic.rs:8:1
99
|
1010
LL | #[rustc_symbol_name]

src/test/ui/symbol-names/issue-60925.legacy.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h419983d0842a72aeE)
1+
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h5425dadb5b1e5fb6E)
22
--> $DIR/issue-60925.rs:21:9
33
|
44
LL | #[rustc_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h419983d0842a72ae)
7+
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h5425dadb5b1e5fb6)
88
--> $DIR/issue-60925.rs:21:9
99
|
1010
LL | #[rustc_symbol_name]

0 commit comments

Comments
 (0)