-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Move EntityHash related types into bevy_ecs
#11498
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
Merged
alice-i-cecile
merged 19 commits into
bevyengine:main
from
doonv:move-entityhash-into-bevy-ecs
Feb 12, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a642589
Move `EntityHash` related types into `bevy_ecs`
doonv 448d396
Merge branch 'main' into move-entityhash-into-bevy-ecs
doonv 98cec11
Hardcode `Entity` key into `EntityHashMap`
doonv 3e072f4
`cargo fmt`
doonv f65f623
Fix CHANGELOG.md
doonv 0a2c7f7
Fix benches
doonv 5d34c0c
Actually fix benches
doonv 186eb91
Fix doc
doonv 630d561
Additional improvements
doonv 874295b
Merge branch 'main' into move-entityhash-into-bevy-ecs
doonv 894d7e6
Fix merge
doonv b5ca4a6
Remove temporary file
doonv 9c7fe43
Fix tests and format
doonv ff5fd46
Remove unusd import
doonv 5c1ef35
Don't modify CHANGELOG.md
doonv cb57097
Add `entity_hash` as a benchmark
doonv 1c81848
Fix doc
doonv 04a83e8
Actually undo CHANGELOG.md changes
doonv 290efce
Fix CI
doonv 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| use std::hash::{BuildHasher, Hasher}; | ||
|
|
||
| #[cfg(feature = "bevy_reflect")] | ||
| use bevy_reflect::Reflect; | ||
| use bevy_utils::hashbrown; | ||
|
|
||
| use super::Entity; | ||
|
|
||
| /// A [`BuildHasher`] that results in a [`EntityHasher`]. | ||
| #[derive(Default, Clone)] | ||
| #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] | ||
| pub struct EntityHash; | ||
|
|
||
| impl BuildHasher for EntityHash { | ||
| type Hasher = EntityHasher; | ||
|
|
||
| fn build_hasher(&self) -> Self::Hasher { | ||
| Self::Hasher::default() | ||
| } | ||
| } | ||
|
|
||
| /// A very fast hash that is only designed to work on generational indices | ||
| /// like [`Entity`]. It will panic if attempting to hash a type containing | ||
| /// non-u64 fields. | ||
| /// | ||
| /// This is heavily optimized for typical cases, where you have mostly live | ||
| /// entities, and works particularly well for contiguous indices. | ||
| /// | ||
| /// If you have an unusual case -- say all your indices are multiples of 256 | ||
| /// or most of the entities are dead generations -- then you might want also to | ||
| /// try [`AHasher`](bevy_utils::AHasher) for a slower hash computation but fewer lookup conflicts. | ||
| #[derive(Debug, Default)] | ||
| pub struct EntityHasher { | ||
| hash: u64, | ||
| } | ||
|
|
||
| impl Hasher for EntityHasher { | ||
| #[inline] | ||
| fn finish(&self) -> u64 { | ||
| self.hash | ||
| } | ||
|
|
||
| fn write(&mut self, _bytes: &[u8]) { | ||
| panic!("EntityHasher can only hash u64 fields."); | ||
| } | ||
|
|
||
| #[inline] | ||
| fn write_u64(&mut self, bits: u64) { | ||
| // 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 | ||
| // For more see <https://abseil.io/about/design/swisstables#metadata-layout> | ||
|
|
||
| // This hash function assumes that the entity ids are still well-distributed, | ||
| // so for H1 leaves the entity id alone in the low bits so that id locality | ||
| // will also give memory locality for things spawned together. | ||
| // For H2, take advantage of the fact that while multiplication doesn't | ||
| // spread entropy to the low bits, it's incredibly good at spreading it | ||
| // upward, which is exactly where we need it the most. | ||
|
|
||
| // While this does include the generation in the output, it doesn't do so | ||
| // *usefully*. H1 won't care until you have over 3 billion entities in | ||
| // the table, and H2 won't care until something hits generation 33 million. | ||
| // Thus the comment suggesting that this is best for live entities, | ||
| // where there won't be generation conflicts where it would matter. | ||
|
|
||
| // The high 32 bits of this are ⅟φ for Fibonacci hashing. That works | ||
| // particularly well for hashing for the same reason as described in | ||
| // <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 make that part of the just product a pass-through. | ||
| const UPPER_PHI: u64 = 0x9e37_79b9_0000_0001; | ||
|
|
||
| // This is `(MAGIC * index + generation) << 32 + index`, in a single instruction. | ||
| self.hash = bits.wrapping_mul(UPPER_PHI); | ||
| } | ||
| } | ||
|
|
||
| /// A [`HashMap`](hashbrown::HashMap) pre-configured to use [`EntityHash`] hashing. | ||
| pub type EntityHashMap<V> = hashbrown::HashMap<Entity, V, EntityHash>; | ||
|
|
||
| /// A [`HashSet`](hashbrown::HashSet) pre-configured to use [`EntityHash`] hashing. | ||
| pub type EntityHashSet = hashbrown::HashSet<Entity, EntityHash>; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| #[cfg(feature = "bevy_reflect")] | ||
| use bevy_reflect::Reflect; | ||
| use static_assertions::assert_impl_all; | ||
|
|
||
| // Check that the HashMaps are Clone if the key/values are Clone | ||
| assert_impl_all!(EntityHashMap::<usize>: Clone); | ||
| // EntityHashMap should implement Reflect | ||
| #[cfg(feature = "bevy_reflect")] | ||
| assert_impl_all!(EntityHashMap::<i32>: Reflect); | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.