Skip rehashing TypeIds#11268
Conversation
8ba1fe4 to
7a4251d
Compare
|
Would it be better to panic if the behavior changes, instead of silently switching to a low quality fallback? I think there might be a few other places where this would be useful. Maybe this should go in 'bevy_util' instead? |
crates/bevy_ecs/src/lib.rs
Outdated
| // This will never be called: TypeId always just calls write_u64 once! | ||
| // This is unlikely to ever change, but as it isn't officially guaranteed, | ||
| // here's a fallback (which can be very low quality). | ||
| self.0 = bytes.iter().fold(self.0, |hash, b| { | ||
| hash.rotate_left(8).wrapping_add(*b as u64) | ||
| }); |
There was a problem hiding this comment.
Depending on how likely this is to occur it might be more beneficial to panic or to emit a warning message if this happens.
There was a problem hiding this comment.
I'm pretty ambivalent about what's best here. An unimplemented!("explanation here") would probably be fine. A compile time warning would be ideal, but I don't see a reasonable way to do that.
There was a problem hiding this comment.
I think I'd prefer unimplemented here as well.
There was a problem hiding this comment.
Changed to unimplemented.
There was a problem hiding this comment.
I don't think unimplemented! is the right panic to use here. If this will never get called then unreachable! seems like the better choice.
Possibly, but that's orthogonal to this PR and can just be a followup PR. |
+1 |
|
Yeah I'm fine to move this later if we find downstream consumers (perhaps bevy_reflect). |
I will probably make a follow up to this if #11228 is approved. It makes heavy use of type id hashmaps. |
|
This PR relies on Rust std/core lib's internal implement. IMO we should have a last resort when |
Note that Bevy isn't the only crate that relies on the
The libs team deliberately does not endorse doing this, but as they're aware of this in the unlikely (other Hash impls were already discussed for this) case the
I'm not sure that's worth it, build.rs is stuff can be a bit messy. An alternative I'd be happy with would be to add back in the |
This seems like a good path forward. |
Objective
TypeIdcontains a high-quality hash. Whenever a lookup based on aTypeIdis performed (e.g. to insert/remove components), the hash is run through a second hash function. This is unnecessary.Solution
Skip re-hashing
TypeIds.In my testing, this improves lookup performance consistently by 10%-15% (of course, the lookup is only a small part of e.g. a bundle insertion).