-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Tweak EntityHasher
for more speed
#10605
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,20 @@ impl Hasher for EntityHasher { | |
|
||
#[inline] | ||
fn write_u64(&mut self, i: u64) { | ||
// We ignore the generation entirely. It's always functionally correct | ||
// to omit things when hashing, so long as it's consistent, just a perf | ||
// trade-off. This hasher is designed for "normal" cases, where nearly | ||
// everything in the table is a live entity, meaning there are few | ||
// generation conflicts. And thus it's overall faster to just ignore | ||
// the generation during hashing, leaving it to the `Entity::eq` to | ||
// confirm the generation matches -- just like `Entity::eq` checks that | ||
// the index is actually the right one, since there's always the chance | ||
// of a conflict in the index despite a good hash function. | ||
// | ||
// This masking actually ends up with negative cost after optimization, | ||
// since it saves needing to do the shift-and-or between the fields. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Demonstration of this by comparing the codegen without the masking to having the masking: https://rust.godbolt.org/z/3vvsT38ar |
||
let index = i & 0xFFFF_FFFF; | ||
|
||
// SwissTable (and thus `hashbrown`) cares about two things from the hash: | ||
// - H1: low bits (masked by `2ⁿ-1`) to pick the slot in which to store the item | ||
// - H2: high 7 bits are used to SIMD optimize hash collision probing | ||
|
@@ -302,11 +316,10 @@ impl Hasher for EntityHasher { | |
// <https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/> | ||
// It loses no information because it has a modular inverse. | ||
// (Specifically, `0x144c_bc89_u32 * 0x9e37_79b9_u32 == 1`.) | ||
// | ||
// The low 32 bits are just 1, to leave the entity id there unchanged. | ||
const UPPER_PHI: u64 = 0x9e37_79b9_0000_0001; | ||
// This bit-masking is free, as the optimizer can just not load the generation. | ||
let id = i & 0xFFFF_FFFF; | ||
self.hash = id.wrapping_mul(UPPER_PHI); | ||
self.hash = index.wrapping_mul(UPPER_PHI); | ||
} | ||
|
||
#[inline] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Demonstration of the overall assembly diff with this change: https://rust.godbolt.org/z/9zxfMdEfv