-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Spec request. Hasher: is write_u32 eqivalent to 4 calls of write_u8? #42951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I’m fairly sure there was a discussion about it before and we decided against doing any changes to the libstd impl. Don’t exactly remember the reason, though. |
If any decision is made, I wish it would be stated in the documentation of |
|
I don't think there's any such requirement. The FxHasher in the compiler does exactly what your example does. |
I think I would be opposed to this requirement; it imposes a performance penalty for systems of the "wrong" endianness, since they would need to correct their endianness. The only requirement on |
My spec request: given two byte slices This way, we could define all the non-slice methods as explicitly being transmutes, always. |
removing |
My personal take on this is that we should only guarantee that For an example of where this could make a difference, consider this code from Abseil which hashes short slices by reading 0-16 bytes and padding anything not included in the length with zeroes or duplicates of the existing bytes. |
Based on the libs team decision in #80303 (comment), I'm going to close this issue. The answer to the question in the issue title is no: |
Was there any updates to the documentation of Hasher? I didn't see any in a quick scan of threads. |
Yeah, it seems like the thread was closed because the original idea was decided to not be done, but I agree that it shouldn't be closed until documentation is added. |
Document Hasher spec decision from rust-lang#42951 Since that ticket was closed without the decision actually being documented. Fixes rust-lang#42951.
Currently
Hasher::write_u32
is implemented asself.write(&unsafe { mem::transmute::<_, [u8; 4]>(i) })
. The question is: is it a requirement for allHasher
implementations?In other words, is is true, that for each hasher implementation, call
must be equivalent to
?
(assuming host is little-endian).
Why is it important.
Suppose, you want to implement very simple hasher with state updated like
state = state * 31 + update
.If
Hasher
does not have requirement as above, hasher can be implemented by simply casting any operand tou64
and avoiding dealing with unaligned/smaller than u64 integers:The text was updated successfully, but these errors were encountered: