Skip to content

Commit

Permalink
Auto merge of #107925 - thomcc:sip13, r=cjgillot
Browse files Browse the repository at this point in the history
Use SipHash-1-3 instead of SipHash-2-4 for StableHasher

Noticed this, and it seems easy and likely a perf win. IIUC we don't need DDOS resistance (just collision) so we ideally would have an even faster hash, but it's hard to beat this SipHash impl here, since it's been so highly tuned for the interface.

It wouldn't surprise me if there's some subtle reason changing this sucks, as it's so obvious it seems likely to have been done. Still, SipHash-1-3 seems to still have the guarantees StableHasher should need (and seemingly more), and is clearly less work. So it's worth a shot.

Not fully tested locally.
  • Loading branch information
bors committed Apr 5, 2023
2 parents b2b676d + 46a3d28 commit 2e486be
Show file tree
Hide file tree
Showing 35 changed files with 179 additions and 377 deletions.
20 changes: 9 additions & 11 deletions compiler/rustc_data_structures/src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl SipHasher128 {
for i in 0..BUFFER_CAPACITY {
let elem = self.buf.get_unchecked(i).assume_init().to_le();
self.state.v3 ^= elem;
Sip24Rounds::c_rounds(&mut self.state);
Sip13Rounds::c_rounds(&mut self.state);
self.state.v0 ^= elem;
}

Expand Down Expand Up @@ -327,7 +327,7 @@ impl SipHasher128 {
for i in 0..last {
let elem = self.buf.get_unchecked(i).assume_init().to_le();
self.state.v3 ^= elem;
Sip24Rounds::c_rounds(&mut self.state);
Sip13Rounds::c_rounds(&mut self.state);
self.state.v0 ^= elem;
}

Expand All @@ -340,7 +340,7 @@ impl SipHasher128 {
for _ in 0..elems_left {
let elem = (msg.as_ptr().add(processed) as *const u64).read_unaligned().to_le();
self.state.v3 ^= elem;
Sip24Rounds::c_rounds(&mut self.state);
Sip13Rounds::c_rounds(&mut self.state);
self.state.v0 ^= elem;
processed += ELEM_SIZE;
}
Expand Down Expand Up @@ -368,7 +368,7 @@ impl SipHasher128 {
for i in 0..last {
let elem = unsafe { self.buf.get_unchecked(i).assume_init().to_le() };
state.v3 ^= elem;
Sip24Rounds::c_rounds(&mut state);
Sip13Rounds::c_rounds(&mut state);
state.v0 ^= elem;
}

Expand All @@ -392,15 +392,15 @@ impl SipHasher128 {
let b: u64 = ((length as u64 & 0xff) << 56) | elem;

state.v3 ^= b;
Sip24Rounds::c_rounds(&mut state);
Sip13Rounds::c_rounds(&mut state);
state.v0 ^= b;

state.v2 ^= 0xee;
Sip24Rounds::d_rounds(&mut state);
Sip13Rounds::d_rounds(&mut state);
let _0 = state.v0 ^ state.v1 ^ state.v2 ^ state.v3;

state.v1 ^= 0xdd;
Sip24Rounds::d_rounds(&mut state);
Sip13Rounds::d_rounds(&mut state);
let _1 = state.v0 ^ state.v1 ^ state.v2 ^ state.v3;

(_0, _1)
Expand Down Expand Up @@ -477,20 +477,18 @@ impl Hasher for SipHasher128 {
}

#[derive(Debug, Clone, Default)]
struct Sip24Rounds;
struct Sip13Rounds;

impl Sip24Rounds {
impl Sip13Rounds {
#[inline]
fn c_rounds(state: &mut State) {
compress!(state);
compress!(state);
}

#[inline]
fn d_rounds(state: &mut State) {
compress!(state);
compress!(state);
compress!(state);
compress!(state);
}
}
Loading

0 comments on commit 2e486be

Please sign in to comment.