Skip to content
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 by Bors] - Proper prehashing #3963

Closed
wants to merge 9 commits into from

Conversation

cart
Copy link
Member

@cart cart commented Feb 16, 2022

For some keys, it is too expensive to hash them on every lookup. Historically in Bevy, we have regrettably done the "wrong" thing in these cases (pre-computing hashes, then re-hashing them) because Rust's built in hashed collections don't give us the tools we need to do otherwise. Doing this is "wrong" because two different values can result in the same hash. Hashed collections generally get around this by falling back to equality checks on hash collisions. You can't do that if the key is the hash. Additionally, re-hashing a hash increase the odds of collision!

#3959 needs pre-hashing to be viable, so I decided to finally properly solve the problem. The solution involves two different changes:

  1. A new generalized "pre-hashing" solution in bevy_utils: Hashed<T> types, which store a value alongside a pre-computed hash. And PreHashMap<K, V> (which uses Hashed<T> internally) . PreHashMap is just an alias for a normal HashMap that uses Hashed<T> as the key and a new PassHash implementation as the Hasher.
  2. Replacing the std::collections re-exports in bevy_utils with equivalent hashbrown impls. Avoiding re-hashes requires the raw_entry_mut api, which isn't stabilized yet (and may never be ... entry_ref has favor now, but also isn't available yet). If std's HashMap ever provides the tools we need, we can move back to that. The latest version of hashbrown adds support for the entity_ref api, so we can move to that in preparation for an std migration, if thats the direction they seem to be going in. Note that adding hashbrown doesn't increase our dependency count because it was already in our tree.

In addition to providing these core tools, I also ported the "table identity hashing" in bevy_ecs to raw_entry_mut, which was a particularly egregious case.

The biggest outstanding case is AssetPathId, which stores a pre-hash. We need AssetPathId to be cheaply clone-able (and ideally Copy), but Hashed<AssetPath> requires ownership of the AssetPath, which makes cloning ids way more expensive. We could consider doing Hashed<Arc<AssetPath>>, but cloning an arc is still a non-trivial expensive that needs to be considered. I would like to handle this in a separate PR. And given that we will be re-evaluating the Bevy Assets implementation in the very near future, I'd prefer to hold off until after that conversation is concluded.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 16, 2022
@cart cart added A-Core Common functionality for all bevy apps and removed S-Needs-Triage This issue needs to be labelled labels Feb 16, 2022
Copy link
Contributor

@Guvante Guvante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except splitting out fast_eq seems like unnecessary complexity

crates/bevy_utils/src/lib.rs Outdated Show resolved Hide resolved
@cart
Copy link
Member Author

cart commented Feb 18, 2022

bors r+

bors bot pushed a commit that referenced this pull request Feb 18, 2022
For some keys, it is too expensive to hash them on every lookup. Historically in Bevy, we have regrettably done the "wrong" thing in these cases (pre-computing hashes, then re-hashing them) because Rust's built in hashed collections don't give us the tools we need to do otherwise. Doing this is "wrong" because two different values can result in the same hash. Hashed collections generally get around this by falling back to equality checks on hash collisions. You can't do that if the key _is_ the hash. Additionally, re-hashing a hash increase the odds of collision!
 
#3959 needs pre-hashing to be viable, so I decided to finally properly solve the problem. The solution involves two different changes:

1. A new generalized "pre-hashing" solution in bevy_utils: `Hashed<T>` types, which store a value alongside a pre-computed hash. And `PreHashMap<K, V>` (which uses `Hashed<T>` internally) . `PreHashMap` is just an alias for a normal HashMap that uses `Hashed<T>` as the key and a new `PassHash` implementation as the Hasher. 
2. Replacing the `std::collections` re-exports in `bevy_utils` with equivalent `hashbrown` impls. Avoiding re-hashes requires the `raw_entry_mut` api, which isn't stabilized yet (and may never be ... `entry_ref` has favor now, but also isn't available yet). If std's HashMap ever provides the tools we need, we can move back to that. The latest version of `hashbrown` adds support for the `entity_ref` api, so we can move to that in preparation for an std migration, if thats the direction they seem to be going in. Note that adding hashbrown doesn't increase our dependency count because it was already in our tree.

In addition to providing these core tools, I also ported the "table identity hashing" in `bevy_ecs` to `raw_entry_mut`, which was a particularly egregious case.

The biggest outstanding case is `AssetPathId`, which stores a pre-hash. We need AssetPathId to be cheaply clone-able (and ideally Copy), but `Hashed<AssetPath>` requires ownership of the AssetPath, which makes cloning ids way more expensive. We could consider doing `Hashed<Arc<AssetPath>>`, but cloning an arc is still a non-trivial expensive that needs to be considered. I would like to handle this in a separate PR. And given that we will be re-evaluating the Bevy Assets implementation in the very near future, I'd prefer to hold off until after that conversation is concluded.
@bors bors bot changed the title Proper prehashing [Merged by Bors] - Proper prehashing Feb 18, 2022
@bors bors bot closed this Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Core Common functionality for all bevy apps
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants