Skip to content

Commit d85b071

Browse files
Urgaumichaelwoerister
authored andcommitted
Switch SipHasher128::finish to a non-consuming method
1 parent e7d5b1a commit d85b071

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/sip128.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,41 +378,47 @@ impl SipHasher128 {
378378
}
379379
}
380380

381-
#[inline]
381+
#[inline(always)]
382382
pub fn finish128(mut self) -> [u64; 2] {
383-
debug_assert!(self.nbuf < BUFFER_SIZE);
383+
SipHasher128::finish128_inner(self.nbuf, &mut self.buf, self.state, self.processed)
384+
}
384385

385-
// Process full elements in buffer.
386-
let last = self.nbuf / ELEM_SIZE;
386+
#[inline]
387+
fn finish128_inner(
388+
nbuf: usize,
389+
buf: &mut [MaybeUninit<u64>; BUFFER_WITH_SPILL_CAPACITY],
390+
mut state: State,
391+
processed: usize,
392+
) -> [u64; 2] {
393+
debug_assert!(nbuf < BUFFER_SIZE);
387394

388-
// Since we're consuming self, avoid updating members for a potential
389-
// performance gain.
390-
let mut state = self.state;
395+
// Process full elements in buffer.
396+
let last = nbuf / ELEM_SIZE;
391397

392398
for i in 0..last {
393-
let elem = unsafe { self.buf.get_unchecked(i).assume_init().to_le() };
399+
let elem = unsafe { buf.get_unchecked(i).assume_init().to_le() };
394400
state.v3 ^= elem;
395401
Sip13Rounds::c_rounds(&mut state);
396402
state.v0 ^= elem;
397403
}
398404

399405
// Get remaining partial element.
400-
let elem = if self.nbuf % ELEM_SIZE != 0 {
406+
let elem = if nbuf % ELEM_SIZE != 0 {
401407
unsafe {
402408
// Ensure element is initialized by writing zero bytes. At most
403409
// `ELEM_SIZE - 1` are required given the above check. It's safe
404410
// to write this many because we have the spill and we maintain
405411
// `self.nbuf` such that this write will start before the spill.
406-
let dst = (self.buf.as_mut_ptr() as *mut u8).add(self.nbuf);
412+
let dst = (buf.as_mut_ptr() as *mut u8).add(nbuf);
407413
ptr::write_bytes(dst, 0, ELEM_SIZE - 1);
408-
self.buf.get_unchecked(last).assume_init().to_le()
414+
buf.get_unchecked(last).assume_init().to_le()
409415
}
410416
} else {
411417
0
412418
};
413419

414420
// Finalize the hash.
415-
let length = self.processed.debug_strict_add(self.nbuf);
421+
let length = processed.debug_strict_add(nbuf);
416422
let b: u64 = ((length as u64 & 0xff) << 56) | elem;
417423

418424
state.v3 ^= b;

0 commit comments

Comments
 (0)