|
15 | 15 | // modified by Bevy contributors
|
16 | 16 |
|
17 | 17 | use crate::{AtomicBorrow, Component, Entity};
|
18 |
| -use bevy_utils::AHasher; |
19 | 18 | use bitflags::bitflags;
|
20 | 19 | use std::{
|
21 | 20 | alloc::{alloc, dealloc, Layout},
|
22 | 21 | any::{type_name, TypeId},
|
23 | 22 | cell::UnsafeCell,
|
24 | 23 | collections::HashMap,
|
25 |
| - hash::{BuildHasherDefault, Hasher}, |
26 | 24 | mem,
|
27 | 25 | ptr::{self, NonNull},
|
28 | 26 | };
|
@@ -557,44 +555,8 @@ fn align(x: usize, alignment: usize) -> usize {
|
557 | 555 |
|
558 | 556 | /// A hasher optimized for hashing a single TypeId.
|
559 | 557 | ///
|
560 |
| -/// TypeId is already thoroughly hashed, so there's no reason to hash it again. |
561 |
| -/// Just leave the bits unchanged. |
562 |
| -#[derive(Default)] |
563 |
| -pub(crate) struct TypeIdHasher { |
564 |
| - hash: u64, |
565 |
| -} |
566 |
| - |
567 |
| -impl Hasher for TypeIdHasher { |
568 |
| - fn write_u64(&mut self, n: u64) { |
569 |
| - // Only a single value can be hashed, so the old hash should be zero. |
570 |
| - debug_assert_eq!(self.hash, 0); |
571 |
| - self.hash = n; |
572 |
| - } |
573 |
| - |
574 |
| - // Tolerate TypeId being either u64 or u128. |
575 |
| - fn write_u128(&mut self, n: u128) { |
576 |
| - debug_assert_eq!(self.hash, 0); |
577 |
| - self.hash = n as u64; |
578 |
| - } |
579 |
| - |
580 |
| - fn write(&mut self, bytes: &[u8]) { |
581 |
| - debug_assert_eq!(self.hash, 0); |
582 |
| - |
583 |
| - // This will only be called if TypeId is neither u64 nor u128, which is not anticipated. |
584 |
| - // In that case we'll just fall back to using a different hash implementation. |
585 |
| - let mut hasher = AHasher::default(); |
586 |
| - hasher.write(bytes); |
587 |
| - self.hash = hasher.finish(); |
588 |
| - } |
589 |
| - |
590 |
| - fn finish(&self) -> u64 { |
591 |
| - self.hash |
592 |
| - } |
593 |
| -} |
594 |
| - |
595 |
| -/// A HashMap with TypeId keys |
596 |
| -/// |
597 |
| -/// Because TypeId is already a fully-hashed u64 (including data in the high seven bits, |
598 |
| -/// which hashbrown needs), there is no need to hash it again. Instead, this uses the much |
599 |
| -/// faster no-op hash. |
600 |
| -pub(crate) type TypeIdMap<V> = HashMap<TypeId, V, BuildHasherDefault<TypeIdHasher>>; |
| 558 | +/// We don't use RandomState from std or Random state from Ahash |
| 559 | +/// because fxhash is [proved to be faster](https://github.com/bevyengine/bevy/pull/1119#issuecomment-751361215) |
| 560 | +/// and we don't need Hash Dos attack protection here |
| 561 | +/// since TypeIds generated during compilation and there is no reason to user attack himself. |
| 562 | +pub(crate) type TypeIdMap<V> = HashMap<TypeId, V, fxhash::FxBuildHasher>; |
0 commit comments