Skip to content

Commit a5521f5

Browse files
committed
use hashbrown for bevy hashmaps / hashsets
1 parent e6b695a commit a5521f5

File tree

11 files changed

+104
-102
lines changed

11 files changed

+104
-102
lines changed

crates/bevy_asset/src/asset_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use anyhow::Result;
88
use bevy_ecs::system::{Res, ResMut};
99
use bevy_log::warn;
1010
use bevy_tasks::TaskPool;
11-
use bevy_utils::{HashMap, Uuid};
11+
use bevy_utils::{Entry, HashMap, Uuid};
1212
use crossbeam_channel::TryRecvError;
1313
use parking_lot::{Mutex, RwLock};
14-
use std::{collections::hash_map::Entry, path::Path, sync::Arc};
14+
use std::{path::Path, sync::Arc};
1515
use thiserror::Error;
1616

1717
/// Errors that occur while loading assets with an `AssetServer`

crates/bevy_ecs/src/entity/map_entities.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::entity::Entity;
2-
use bevy_utils::HashMap;
3-
use std::collections::hash_map::Entry;
2+
use bevy_utils::{Entry, HashMap};
43
use thiserror::Error;
54

65
#[derive(Error, Debug)]

crates/bevy_input/src/input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ use bevy_ecs::schedule::State;
2929
/// * Call the [`Input::release`] method for each release event.
3030
/// * Call the [`Input::clear`] method at each frame start, before processing events.
3131
#[derive(Debug, Clone)]
32-
pub struct Input<T> {
32+
pub struct Input<T: Eq + Hash> {
3333
pressed: HashSet<T>,
3434
just_pressed: HashSet<T>,
3535
just_released: HashSet<T>,
3636
}
3737

38-
impl<T> Default for Input<T> {
38+
impl<T: Eq + Hash> Default for Input<T> {
3939
fn default() -> Self {
4040
Self {
4141
pressed: Default::default(),

crates/bevy_reflect/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ thiserror = "1.0"
2525
serde = "1"
2626
smallvec = { version = "1.6", features = ["serde", "union", "const_generics"], optional = true }
2727
glam = { version = "0.20.0", features = ["serde"], optional = true }
28+
hashbrown = { version = "0.11", features = ["serde"], optional = true }
2829

2930
[dev-dependencies]
3031
ron = "0.7.0"

crates/bevy_reflect/src/impls/std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
};
77

88
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
9-
use bevy_utils::{AHashExt, Duration, HashMap, HashSet};
9+
use bevy_utils::{Duration, HashMap, HashSet};
1010
use serde::{Deserialize, Serialize};
1111
use std::{
1212
any::Any,

crates/bevy_reflect/src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{any::Any, collections::hash_map::Entry};
1+
use std::any::Any;
22

3-
use bevy_utils::HashMap;
3+
use bevy_utils::{Entry, HashMap};
44

55
use crate::{serde::Serializable, Reflect, ReflectMut, ReflectRef};
66

crates/bevy_reflect/src/struct_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{serde::Serializable, Reflect, ReflectMut, ReflectRef};
2-
use bevy_utils::HashMap;
3-
use std::{any::Any, borrow::Cow, collections::hash_map::Entry};
2+
use bevy_utils::{Entry, HashMap};
3+
use std::{any::Any, borrow::Cow};
44

55
/// A reflected Rust regular struct type.
66
///

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
use bevy_app::EventReader;
1111
use bevy_asset::{AssetEvent, Assets, Handle};
1212
use bevy_ecs::system::{Res, ResMut};
13-
use bevy_utils::{tracing::error, HashMap, HashSet};
14-
use std::{collections::hash_map::Entry, hash::Hash, ops::Deref, sync::Arc};
13+
use bevy_utils::{tracing::error, Entry, HashMap, HashSet};
14+
use std::{hash::Hash, ops::Deref, sync::Arc};
1515
use thiserror::Error;
1616
use wgpu::{PipelineLayoutDescriptor, ShaderModule, VertexBufferLayout as RawVertexBufferLayout};
1717

crates/bevy_render/src/texture/texture_cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
renderer::RenderDevice,
44
};
55
use bevy_ecs::prelude::ResMut;
6-
use bevy_utils::HashMap;
6+
use bevy_utils::{Entry, HashMap};
77
use wgpu::{TextureDescriptor, TextureViewDescriptor};
88

99
/// The internal representation of a [`CachedTexture`] used to track whether it was recently used
@@ -39,7 +39,7 @@ impl TextureCache {
3939
descriptor: TextureDescriptor<'static>,
4040
) -> CachedTexture {
4141
match self.textures.entry(descriptor) {
42-
std::collections::hash_map::Entry::Occupied(mut entry) => {
42+
Entry::Occupied(mut entry) => {
4343
for texture in entry.get_mut().iter_mut() {
4444
if !texture.taken {
4545
texture.frames_since_last_use = 0;
@@ -64,7 +64,7 @@ impl TextureCache {
6464
default_view,
6565
}
6666
}
67-
std::collections::hash_map::Entry::Vacant(entry) => {
67+
Entry::Vacant(entry) => {
6868
let texture = render_device.create_texture(entry.key());
6969
let default_view = texture.create_view(&TextureViewDescriptor::default());
7070
entry.insert(vec![CachedTextureMeta {

crates/bevy_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ahash = "0.7.0"
1414
tracing = {version = "0.1", features = ["release_max_level_info"]}
1515
instant = { version = "0.1", features = ["wasm-bindgen"] }
1616
uuid = { version = "0.8", features = ["v4", "serde"] }
17+
hashbrown = { version = "0.11", features = ["serde"] }
1718

1819
[target.'cfg(target_arch = "wasm32")'.dependencies]
1920
getrandom = {version = "0.2.0", features = ["js"]}

crates/bevy_utils/src/lib.rs

Lines changed: 87 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod label;
33

44
pub use ahash::AHasher;
55
pub use enum_variant_meta::*;
6+
pub type Entry<'a, K, V> = hashbrown::hash_map::Entry<'a, K, V, RandomState>;
67
pub use instant::{Duration, Instant};
78
pub use tracing;
89
pub use uuid::Uuid;
@@ -32,7 +33,7 @@ impl std::hash::BuildHasher for FixedState {
3233
}
3334
}
3435

35-
/// A [`HashMap`][std::collections::HashMap] implementing [`aHash`], a high
36+
/// A [`HashMap`][hashbrown::HashMap] implementing [`aHash`], a high
3637
/// speed keyed hashing algorithm intended for use in in-memory hashmaps.
3738
///
3839
/// `aHash` is designed for performance and is NOT cryptographically secure.
@@ -50,7 +51,7 @@ impl std::hash::BuildHasher for FixedState {
5051
/// # }
5152
/// ```
5253
///
53-
/// The standard library's [`HashMap::new`][std::collections::HashMap::new] is
54+
/// The standard library's [`HashMap::new`][hashbrown::HashMap::new] is
5455
/// implemented only for `HashMap`s which use the
5556
/// [`DefaultHasher`][std::collections::hash_map::DefaultHasher], so it's not
5657
/// available for Bevy's `HashMap`.
@@ -69,30 +70,30 @@ impl std::hash::BuildHasher for FixedState {
6970
/// ```
7071
///
7172
/// [`aHash`]: https://github.com/tkaitchuck/aHash
72-
pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
73-
74-
pub trait AHashExt {
75-
fn with_capacity(capacity: usize) -> Self;
76-
}
77-
78-
impl<K, V> AHashExt for HashMap<K, V> {
79-
/// Creates an empty `HashMap` with the specified capacity with aHash.
80-
///
81-
/// The hash map will be able to hold at least `capacity` elements without
82-
/// reallocating. If `capacity` is 0, the hash map will not allocate.
83-
///
84-
/// # Examples
85-
///
86-
/// ```
87-
/// use bevy_utils::{HashMap, AHashExt};
88-
/// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
89-
/// assert!(map.capacity() >= 10);
90-
/// ```
91-
#[inline]
92-
fn with_capacity(capacity: usize) -> Self {
93-
HashMap::with_capacity_and_hasher(capacity, RandomState::default())
94-
}
95-
}
73+
pub type HashMap<K, V> = hashbrown::HashMap<K, V, RandomState>;
74+
75+
// pub trait AHashExt {
76+
// fn with_capacity(capacity: usize) -> Self;
77+
// }
78+
79+
// impl<K, V> AHashExt for HashMap<K, V> {
80+
// /// Creates an empty `HashMap` with the specified capacity with aHash.
81+
// ///
82+
// /// The hash map will be able to hold at least `capacity` elements without
83+
// /// reallocating. If `capacity` is 0, the hash map will not allocate.
84+
// ///
85+
// /// # Examples
86+
// ///
87+
// /// ```
88+
// /// use bevy_utils::{HashMap, AHashExt};
89+
// /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
90+
// /// assert!(map.capacity() >= 10);
91+
// /// ```
92+
// #[inline]
93+
// fn with_capacity(capacity: usize) -> Self {
94+
// HashMap::with_capacity_and_hasher(capacity, RandomState::default())
95+
// }
96+
// }
9697

9798
/// A stable std hash map implementing `aHash`, a high speed keyed hashing algorithm
9899
/// intended for use in in-memory hashmaps.
@@ -101,26 +102,26 @@ impl<K, V> AHashExt for HashMap<K, V> {
101102
/// of insertions and deletions and not a random source.
102103
///
103104
/// `aHash` is designed for performance and is NOT cryptographically secure.
104-
pub type StableHashMap<K, V> = std::collections::HashMap<K, V, FixedState>;
105-
106-
impl<K, V> AHashExt for StableHashMap<K, V> {
107-
/// Creates an empty `StableHashMap` with the specified capacity with `aHash`.
108-
///
109-
/// The hash map will be able to hold at least `capacity` elements without
110-
/// reallocating. If `capacity` is 0, the hash map will not allocate.
111-
///
112-
/// # Examples
113-
///
114-
/// ```
115-
/// use bevy_utils::{StableHashMap, AHashExt};
116-
/// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10);
117-
/// assert!(map.capacity() >= 10);
118-
/// ```
119-
#[inline]
120-
fn with_capacity(capacity: usize) -> Self {
121-
StableHashMap::with_capacity_and_hasher(capacity, FixedState::default())
122-
}
123-
}
105+
pub type StableHashMap<K, V> = hashbrown::HashMap<K, V, FixedState>;
106+
107+
// impl<K, V> AHashExt for StableHashMap<K, V> {
108+
// /// Creates an empty `StableHashMap` with the specified capacity with `aHash`.
109+
// ///
110+
// /// The hash map will be able to hold at least `capacity` elements without
111+
// /// reallocating. If `capacity` is 0, the hash map will not allocate.
112+
// ///
113+
// /// # Examples
114+
// ///
115+
// /// ```
116+
// /// use bevy_utils::{StableHashMap, AHashExt};
117+
// /// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10);
118+
// /// assert!(map.capacity() >= 10);
119+
// /// ```
120+
// #[inline]
121+
// fn with_capacity(capacity: usize) -> Self {
122+
// StableHashMap::with_capacity_and_hasher(capacity, FixedState::default())
123+
// }
124+
// }
124125

125126
/// A [`HashSet`][std::collections::HashSet] implementing [`aHash`], a high
126127
/// speed keyed hashing algorithm intended for use in in-memory hashmaps.
@@ -159,26 +160,26 @@ impl<K, V> AHashExt for StableHashMap<K, V> {
159160
/// ```
160161
///
161162
/// [`aHash`]: https://github.com/tkaitchuck/aHash
162-
pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
163-
164-
impl<K> AHashExt for HashSet<K> {
165-
/// Creates an empty `HashSet` with the specified capacity with aHash.
166-
///
167-
/// The hash set will be able to hold at least `capacity` elements without
168-
/// reallocating. If `capacity` is 0, the hash set will not allocate.
169-
///
170-
/// # Examples
171-
///
172-
/// ```
173-
/// use bevy_utils::{HashSet, AHashExt};
174-
/// let set: HashSet<i32> = HashSet::with_capacity(10);
175-
/// assert!(set.capacity() >= 10);
176-
/// ```
177-
#[inline]
178-
fn with_capacity(capacity: usize) -> Self {
179-
HashSet::with_capacity_and_hasher(capacity, RandomState::default())
180-
}
181-
}
163+
pub type HashSet<K> = hashbrown::HashSet<K, RandomState>;
164+
165+
// impl<K> AHashExt for HashSet<K> {
166+
// /// Creates an empty `HashSet` with the specified capacity with aHash.
167+
// ///
168+
// /// The hash set will be able to hold at least `capacity` elements without
169+
// /// reallocating. If `capacity` is 0, the hash set will not allocate.
170+
// ///
171+
// /// # Examples
172+
// ///
173+
// /// ```
174+
// /// use bevy_utils::{HashSet, AHashExt};
175+
// /// let set: HashSet<i32> = HashSet::with_capacity(10);
176+
// /// assert!(set.capacity() >= 10);
177+
// /// ```
178+
// #[inline]
179+
// fn with_capacity(capacity: usize) -> Self {
180+
// HashSet::with_capacity_and_hasher(capacity, RandomState::default())
181+
// }
182+
// }
182183

183184
/// A stable std hash set implementing `aHash`, a high speed keyed hashing algorithm
184185
/// intended for use in in-memory hashmaps.
@@ -187,23 +188,23 @@ impl<K> AHashExt for HashSet<K> {
187188
/// of insertions and deletions and not a random source.
188189
///
189190
/// `aHash` is designed for performance and is NOT cryptographically secure.
190-
pub type StableHashSet<K> = std::collections::HashSet<K, FixedState>;
191-
192-
impl<K> AHashExt for StableHashSet<K> {
193-
/// Creates an empty `StableHashSet` with the specified capacity with `aHash`.
194-
///
195-
/// The hash set will be able to hold at least `capacity` elements without
196-
/// reallocating. If `capacity` is 0, the hash set will not allocate.
197-
///
198-
/// # Examples
199-
///
200-
/// ```
201-
/// use bevy_utils::{StableHashSet, AHashExt};
202-
/// let set: StableHashSet<i32> = StableHashSet::with_capacity(10);
203-
/// assert!(set.capacity() >= 10);
204-
/// ```
205-
#[inline]
206-
fn with_capacity(capacity: usize) -> Self {
207-
StableHashSet::with_capacity_and_hasher(capacity, FixedState::default())
208-
}
209-
}
191+
pub type StableHashSet<K> = hashbrown::HashSet<K, FixedState>;
192+
193+
// impl<K> AHashExt for StableHashSet<K> {
194+
// /// Creates an empty `StableHashSet` with the specified capacity with `aHash`.
195+
// ///
196+
// /// The hash set will be able to hold at least `capacity` elements without
197+
// /// reallocating. If `capacity` is 0, the hash set will not allocate.
198+
// ///
199+
// /// # Examples
200+
// ///
201+
// /// ```
202+
// /// use bevy_utils::{StableHashSet, AHashExt};
203+
// /// let set: StableHashSet<i32> = StableHashSet::with_capacity(10);
204+
// /// assert!(set.capacity() >= 10);
205+
// /// ```
206+
// #[inline]
207+
// fn with_capacity(capacity: usize) -> Self {
208+
// StableHashSet::with_capacity_and_hasher(capacity, FixedState::default())
209+
// }
210+
// }

0 commit comments

Comments
 (0)