-
Notifications
You must be signed in to change notification settings - Fork 109
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
Add fast, byte-oriented Hash
derive
#2075
Comments
On further investigation, I don't think there's any benefit to the more complex approach. We should assume that
|
The standard library's derive for `Hash` generates a recursive descent into the fields of the type it is applied to. This commit adds an alternative derive that generates an optimized, byte-oriented `Hash` implementation for types that implement `IntoBytes`. Instead of a recursive descent, the generated implementation makes a single call to `Hasher::write()` in both `Hash::hash()` and `Hash::hash_slice()`, feeding the hasher the bytes of the type or slice all at once. Resolves google#2075
The standard library's derive for `Hash` generates a recursive descent into the fields of the type it is applied to. This commit adds a `ByteHash` derive that generates an optimized, byte-oriented `Hash` implementation for types that implement `IntoBytes`. Instead of a recursive descent, the generated implementation makes a single call to `Hasher::write()` in both `Hash::hash()` and `Hash::hash_slice()`, feeding the hasher the bytes of the type or slice all at once. Resolves google#2075
The standard library's derive for `Hash` generates a recursive descent into the fields of the type it is applied to. This commit adds a `ByteHash` derive that generates an optimized, byte-oriented `Hash` implementation for types that implement `IntoBytes`. Instead of a recursive descent, the generated implementation makes a single call to `Hasher::write()` in both `Hash::hash()` and `Hash::hash_slice()`, feeding the hasher the bytes of the type or slice all at once. Resolves google#2075
The standard library's derive for `Hash` generates a recursive descent into the fields of the type it is applied to. This commit adds a `ByteHash` derive that generates an optimized, byte-oriented `Hash` implementation for types that implement `IntoBytes`. Instead of a recursive descent, the generated implementation makes a single call to `Hasher::write()` in both `Hash::hash()` and `Hash::hash_slice()`, feeding the hasher the bytes of the type or slice all at once. Resolves #2075
Partially completed in #2159; still needs a port to 0.9. |
Given that #2159 is labeled needs-backport and given that our 0.9 release roadmap lists needs-backport as a blocker, I think we can close this issue. Reopen if you disagree. |
For types that are
IntoBytes
, we could derive optimized implementations ofHash
.Adding
derive(Hash)
to a struct generates a recursive descent into the fields of the struct. Instead, for a type that isIntoBytes
, we could relatively easily havederive(zerocopy::Hash)
expand to something like this:Alternatively, if we run with the assumption that
Hasher
s benefit from writing the greatest number of bytes at a time, we can leverage the known size and well-alignedness of the user type to emit calls tostate.write_u128
,state.write_u64
,state.write_u32
, and so on. A trailing DST suffix, if any, can be serialized withstate.write(trailing.as_bytes())
.We should benchmark these approaches. I don't have a strong intuition for the performance characteristics of Rust's leading
Hasher
implementations. My guess is that we couldn't do better in the more complicated approach than a well optimizedHasher::write
could do, so we're best off doing the simple thing.The text was updated successfully, but these errors were encountered: