Save an instruction in EntityHasher
#10648
Merged
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.
Objective
Keep essentially the same structure of
EntityHasher
from #9903, but rephrase the multiplication slightly to save an instruction.cc @superdump
Discord thread: https://discord.com/channels/691052431525675048/1172033156845674507/1174969772522356756
Solution
Today, the hash is
with
i
being(generation << 32) | index
.Expanding things out, we get
What if we do the same thing, but with
+
instead of|
? That's almost the same thing, except that it has carries, which are actually often better in a hash function anyway, since it doesn't saturate. (|
can be dangerous, since once something becomes-1
it'll stay that, and there's no mixing available.)So we can do essentially the same thing using a single multiplication instead of doing multiply-shift-or.
LLVM was already smart enough to merge the shifting into a multiplication, but this saves the extra
or
:https://rust.godbolt.org/z/MEvbz4eo4
It's a very small change, and often will disappear in load latency anyway, but it's a couple percent faster in lookups:
(There was more of an improvement here before #10558, but with
to_bits
being a singleqword
load now, keeping things mostly as it is turned out to be better than the bigger changes I'd tried in #10605.)Changelog
(Probably skip it)
Migration Guide
(none needed)