Skip to content

Commit 2e486be

Browse files
committed
Auto merge of #107925 - thomcc:sip13, r=cjgillot
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.
2 parents b2b676d + 46a3d28 commit 2e486be

35 files changed

+179
-377
lines changed

compiler/rustc_data_structures/src/sip128.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl SipHasher128 {
247247
for i in 0..BUFFER_CAPACITY {
248248
let elem = self.buf.get_unchecked(i).assume_init().to_le();
249249
self.state.v3 ^= elem;
250-
Sip24Rounds::c_rounds(&mut self.state);
250+
Sip13Rounds::c_rounds(&mut self.state);
251251
self.state.v0 ^= elem;
252252
}
253253

@@ -327,7 +327,7 @@ impl SipHasher128 {
327327
for i in 0..last {
328328
let elem = self.buf.get_unchecked(i).assume_init().to_le();
329329
self.state.v3 ^= elem;
330-
Sip24Rounds::c_rounds(&mut self.state);
330+
Sip13Rounds::c_rounds(&mut self.state);
331331
self.state.v0 ^= elem;
332332
}
333333

@@ -340,7 +340,7 @@ impl SipHasher128 {
340340
for _ in 0..elems_left {
341341
let elem = (msg.as_ptr().add(processed) as *const u64).read_unaligned().to_le();
342342
self.state.v3 ^= elem;
343-
Sip24Rounds::c_rounds(&mut self.state);
343+
Sip13Rounds::c_rounds(&mut self.state);
344344
self.state.v0 ^= elem;
345345
processed += ELEM_SIZE;
346346
}
@@ -368,7 +368,7 @@ impl SipHasher128 {
368368
for i in 0..last {
369369
let elem = unsafe { self.buf.get_unchecked(i).assume_init().to_le() };
370370
state.v3 ^= elem;
371-
Sip24Rounds::c_rounds(&mut state);
371+
Sip13Rounds::c_rounds(&mut state);
372372
state.v0 ^= elem;
373373
}
374374

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

394394
state.v3 ^= b;
395-
Sip24Rounds::c_rounds(&mut state);
395+
Sip13Rounds::c_rounds(&mut state);
396396
state.v0 ^= b;
397397

398398
state.v2 ^= 0xee;
399-
Sip24Rounds::d_rounds(&mut state);
399+
Sip13Rounds::d_rounds(&mut state);
400400
let _0 = state.v0 ^ state.v1 ^ state.v2 ^ state.v3;
401401

402402
state.v1 ^= 0xdd;
403-
Sip24Rounds::d_rounds(&mut state);
403+
Sip13Rounds::d_rounds(&mut state);
404404
let _1 = state.v0 ^ state.v1 ^ state.v2 ^ state.v3;
405405

406406
(_0, _1)
@@ -477,20 +477,18 @@ impl Hasher for SipHasher128 {
477477
}
478478

479479
#[derive(Debug, Clone, Default)]
480-
struct Sip24Rounds;
480+
struct Sip13Rounds;
481481

482-
impl Sip24Rounds {
482+
impl Sip13Rounds {
483483
#[inline]
484484
fn c_rounds(state: &mut State) {
485485
compress!(state);
486-
compress!(state);
487486
}
488487

489488
#[inline]
490489
fn d_rounds(state: &mut State) {
491490
compress!(state);
492491
compress!(state);
493492
compress!(state);
494-
compress!(state);
495493
}
496494
}

0 commit comments

Comments
 (0)