Skip to content

Commit 87f8763

Browse files
authored
Rollup merge of rust-lang#69471 - nnethercote:rm-sip-Hasher-short_write, r=dtolnay
Remove `sip::Hasher::short_write`. `sip::Hasher::short_write` is currently unused. It is called by `sip::Hasher::write_{u8,usize}`, but those methods are also unused, because `DefaultHasher`, `SipHasher` and `SipHasher13` don't implement any of the `write_xyz` methods, so all their write operations end up calling `sip::Hasher::write`. (I confirmed this by inserting a `panic!` in `sip::Hasher::short_write` and running the tests -- they all passed.) The alternative would be to add all the missing `write_xyz` methods. This does give some significant speed-ups, but it hurts compile times a little in some cases. See rust-lang#69152 for details. This commit does the conservative thing and doesn't change existing behaviour. r? @rust-lang/libs
2 parents 131772c + 54d1c50 commit 87f8763

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

src/libcore/hash/sip.rs

+7-46
Original file line numberDiff line numberDiff line change
@@ -220,37 +220,6 @@ impl<S: Sip> Hasher<S> {
220220
self.state.v3 = self.k1 ^ 0x7465646279746573;
221221
self.ntail = 0;
222222
}
223-
224-
// Specialized write function that is only valid for buffers with len <= 8.
225-
// It's used to force inlining of write_u8 and write_usize, those would normally be inlined
226-
// except for composite types (that includes slices and str hashing because of delimiter).
227-
// Without this extra push the compiler is very reluctant to inline delimiter writes,
228-
// degrading performance substantially for the most common use cases.
229-
#[inline]
230-
fn short_write(&mut self, msg: &[u8]) {
231-
debug_assert!(msg.len() <= 8);
232-
let length = msg.len();
233-
self.length += length;
234-
235-
let needed = 8 - self.ntail;
236-
let fill = cmp::min(length, needed);
237-
if fill == 8 {
238-
self.tail = unsafe { load_int_le!(msg, 0, u64) };
239-
} else {
240-
self.tail |= unsafe { u8to64_le(msg, 0, fill) } << (8 * self.ntail);
241-
if length < needed {
242-
self.ntail += length;
243-
return;
244-
}
245-
}
246-
self.state.v3 ^= self.tail;
247-
S::c_rounds(&mut self.state);
248-
self.state.v0 ^= self.tail;
249-
250-
// Buffered tail is now flushed, process new input.
251-
self.ntail = length - needed;
252-
self.tail = unsafe { u8to64_le(msg, needed, self.ntail) };
253-
}
254223
}
255224

256225
#[stable(feature = "rust1", since = "1.0.0")]
@@ -280,21 +249,13 @@ impl super::Hasher for SipHasher13 {
280249
}
281250

282251
impl<S: Sip> super::Hasher for Hasher<S> {
283-
// see short_write comment for explanation
284-
#[inline]
285-
fn write_usize(&mut self, i: usize) {
286-
let bytes = unsafe {
287-
crate::slice::from_raw_parts(&i as *const usize as *const u8, mem::size_of::<usize>())
288-
};
289-
self.short_write(bytes);
290-
}
291-
292-
// see short_write comment for explanation
293-
#[inline]
294-
fn write_u8(&mut self, i: u8) {
295-
self.short_write(&[i]);
296-
}
297-
252+
// Note: no integer hashing methods (`write_u*`, `write_i*`) are defined
253+
// for this type. We could add them, copy the `short_write` implementation
254+
// in librustc_data_structures/sip128.rs, and add `write_u*`/`write_i*`
255+
// methods to `SipHasher`, `SipHasher13`, and `DefaultHasher`. This would
256+
// greatly speed up integer hashing by those hashers, at the cost of
257+
// slightly slowing down compile speeds on some benchmarks. See #69152 for
258+
// details.
298259
#[inline]
299260
fn write(&mut self, msg: &[u8]) {
300261
let length = msg.len();

0 commit comments

Comments
 (0)