mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 15:31:18 +00:00
Remove the hashtable seed (#7871)
Hashtables seed every hash with the address of a global,
`ComputeSeed()`, so that ASLR varies layout and iteration order between
runs. Debug builds already vary iteration order directly in `entries()`,
so the seed only matters in release builds. There it costs a register
and an XOR on the latency path of every integer or pointer hash, and it
redraws a table's collisions in each process.
Remove the seed from `HashValue`, `Hasher`, `CarbonHashValue`,
`CustomHashValue::Hash`, and `HashKey`. Hashing starts from a zero
buffer, so integer keys hash as `WeakMix(key)`. `global_addr_seed`
remains in debug builds to seed the iteration-order entropy.
`ClangDeclSignature` chained its hash through the seed, and now hashes
its fields with one `Hasher`.
The seed also kept some short-input paths from collapsing. Without it,
every `{i, 0, 0}` array of `int32_t` hashed the same, all-zero 4-8 byte
inputs lost their size, and the 17-32 byte path multiplied the size
sample by a byte-shifted copy of itself. Each `Mix` operand of the
short-input paths now XORs in a 32-bit constant from
`ImmediateRandomData` in place of a 64-bit chunk of `StaticRandomData`,
which they now only sample to encode sizes. `XorImmediate` keeps these
as `xor $imm32` on x86-64; without its `asm` barrier, a read feeding the
XOR is folded in instead and the constant ends up in a register that
loops hoist.
Cycles against trunk on Zen 5, from `bench_runner.py` with 48 runs per
side, each run at a random code layout, and table sizes of 64, 3584, and
57344:
- Integer and pointer hits: -3.0% to -5.7% in `Map`, -0.8% to -2.4% in
`Set`.
- Integer and pointer misses: up to -12.6%, except `Set<int*>` at +1.4%
(64) and +6.1% (3584).
- String keys: -2.8% to +2.1%, with `Map` updates at +1.1% to +1.6%.
- Hash latency, without layout randomization: integers and pointers
-9.8%, empty strings +5.4% (0.1 cycles), other strings not significant.
Same method on an Apple M1, with #7870 on both sides. Without it, this
change moves benchmark loop counters between `sp`- and `x29`-relative
stack slots, which alone makes `Set<int>` lookups up to 49% slower on
the M1.
- Integer and pointer hits: -1.0% to -10.3% in `Map`, -24.5% to +1.2% in
`Set`.
- Integer and pointer misses: -3.9% to -48.0%, except +7.2% to +59.3% at
3584, from longer probes (instructions +6.4% to +70.6%).
- Inserting 64 integer keys: +12.1% to +15.0%, from store memory-order
violations.
- String keys: -6.6% to +3.9%.
- Hash latency: integers and pointers -9.0% to -10.0%, empty strings
+48.8% (1 cycle).
Release builds now iterate a given table in the same order in every run.
Code that depends on that order still fails in debug builds.
Assisted-by: Claude Code
This commit is contained in:
1 parent
bf64992afb
commit
13ecad24be
17 files changed
+226
-251
No files matched your search
@@ -121,9 +121,8 @@ TEST(HashtableKeyContextTest, HashtableEqAPFloat) {
|
||||
struct CustomHash {
|
||||
int x;
|
||||
|
||||
friend auto CarbonHashValue(const CustomHash& value, uint64_t seed)
|
||||
-> HashCode {
|
||||
return HashValue(value.x + 42, seed);
|
||||
friend auto CarbonHashValue(const CustomHash& value) -> HashCode {
|
||||
return HashValue(value.x + 42);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -155,20 +154,15 @@ TEST(HashtableKeyContextTest, DefaultKeyContext) {
|
||||
EXPECT_FALSE(HashtableEq(zero_float, neg_zero_float));
|
||||
|
||||
// Also check hash dispatching.
|
||||
uint64_t seed = 1234;
|
||||
EXPECT_THAT(context.HashKey(42, seed), Eq(HashValue(42, seed)));
|
||||
EXPECT_THAT(context.HashKey(CustomHash{.x = 1234}, seed),
|
||||
Eq(HashValue(CustomHash{.x = 1234}, seed)));
|
||||
EXPECT_THAT(context.HashKey(one_64, seed), Eq(HashValue(one_64, seed)));
|
||||
EXPECT_THAT(context.HashKey(one_128, seed), Eq(HashValue(one_128, seed)));
|
||||
EXPECT_THAT(context.HashKey(one_64, seed),
|
||||
Ne(context.HashKey(one_128, seed)));
|
||||
EXPECT_THAT(context.HashKey(zero_float, seed),
|
||||
Eq(HashValue(zero_float, seed)));
|
||||
EXPECT_THAT(context.HashKey(neg_zero_float, seed),
|
||||
Eq(HashValue(neg_zero_float, seed)));
|
||||
EXPECT_THAT(context.HashKey(zero_float, seed),
|
||||
Ne(context.HashKey(neg_zero_float, seed)));
|
||||
EXPECT_THAT(context.HashKey(42), Eq(HashValue(42)));
|
||||
EXPECT_THAT(context.HashKey(CustomHash{.x = 1234}),
|
||||
Eq(HashValue(CustomHash{.x = 1234})));
|
||||
EXPECT_THAT(context.HashKey(one_64), Eq(HashValue(one_64)));
|
||||
EXPECT_THAT(context.HashKey(one_128), Eq(HashValue(one_128)));
|
||||
EXPECT_THAT(context.HashKey(one_64), Ne(context.HashKey(one_128)));
|
||||
EXPECT_THAT(context.HashKey(zero_float), Eq(HashValue(zero_float)));
|
||||
EXPECT_THAT(context.HashKey(neg_zero_float), Eq(HashValue(neg_zero_float)));
|
||||
EXPECT_THAT(context.HashKey(zero_float), Ne(context.HashKey(neg_zero_float)));
|
||||
}
|
||||
|
||||
struct TestTranslatingKeyContext
|
||||
@@ -191,13 +185,12 @@ TEST(HashtableKeyContextTest, TranslatingKeyContext) {
|
||||
|
||||
TestTranslatingKeyContext context = {.array = values};
|
||||
|
||||
uint64_t seed = 1234;
|
||||
EXPECT_THAT(context.HashKey(0, seed), Eq(HashValue(one_64, seed)));
|
||||
EXPECT_THAT(context.HashKey(1, seed), Eq(HashValue(two_64, seed)));
|
||||
EXPECT_THAT(context.HashKey(2, seed), Eq(HashValue(one_128, seed)));
|
||||
EXPECT_THAT(context.HashKey(3, seed), Eq(HashValue(two_128, seed)));
|
||||
EXPECT_THAT(context.HashKey(4, seed), Eq(HashValue(one_64, seed)));
|
||||
EXPECT_THAT(context.HashKey(5, seed), Eq(HashValue(one_64, seed)));
|
||||
EXPECT_THAT(context.HashKey(0), Eq(HashValue(one_64)));
|
||||
EXPECT_THAT(context.HashKey(1), Eq(HashValue(two_64)));
|
||||
EXPECT_THAT(context.HashKey(2), Eq(HashValue(one_128)));
|
||||
EXPECT_THAT(context.HashKey(3), Eq(HashValue(two_128)));
|
||||
EXPECT_THAT(context.HashKey(4), Eq(HashValue(one_64)));
|
||||
EXPECT_THAT(context.HashKey(5), Eq(HashValue(one_64)));
|
||||
|
||||
EXPECT_TRUE(context.KeyEq(one_64, 0));
|
||||
EXPECT_TRUE(context.KeyEq(one_64, 4));
|
||||
|
||||
Reference in new issue
Block a user