Skip to content

Commit 1bc34b4

Browse files
committed
bevy_utils: Re-introduce with_capacity(). (#2393)
# Objective Re-introduce `AHashExt` and respective `with_capacity()` implementations to give a more ergonomic way to set a `HashMap` / `HashSet` capacity. As a note, this has also been discussed and agreed on issue #2115, which this PR addresses (leaving `new()` out of the `AHashExt` trait). Fixes #2115. ## Solution PR #1235 had removed the `AHashExt` trait and respective `with_capacity()`s implementations, leaving only the less ergonomic `HashMap::with_capacity_and_hasher(size, Default::default())` option available. This re-introduces `AHashExt` and respective `with_capacity()` implementations to give a more ergonomic way to set a `HashMap` / `HashSet` capacity.
1 parent 52e8a19 commit 1bc34b4

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

crates/bevy_utils/src/lib.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ impl std::hash::BuildHasher for FixedState {
3737
/// AHash is designed for performance and is NOT cryptographically secure.
3838
pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
3939

40+
pub trait AHashExt {
41+
fn with_capacity(capacity: usize) -> Self;
42+
}
43+
44+
impl<K, V> AHashExt for HashMap<K, V> {
45+
/// Creates an empty `HashMap` with the specified capacity with AHash.
46+
///
47+
/// The hash map will be able to hold at least `capacity` elements without
48+
/// reallocating. If `capacity` is 0, the hash map will not allocate.
49+
///
50+
/// # Examples
51+
///
52+
/// ```
53+
/// use bevy_utils::{HashMap, AHashExt};
54+
/// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
55+
/// assert!(map.capacity() >= 10);
56+
/// ```
57+
#[inline]
58+
fn with_capacity(capacity: usize) -> Self {
59+
HashMap::with_capacity_and_hasher(capacity, RandomState::default())
60+
}
61+
}
62+
4063
/// A stable std hash map implementing AHash, a high speed keyed hashing algorithm
4164
/// intended for use in in-memory hashmaps.
4265
///
@@ -46,12 +69,50 @@ pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
4669
/// AHash is designed for performance and is NOT cryptographically secure.
4770
pub type StableHashMap<K, V> = std::collections::HashMap<K, V, FixedState>;
4871

72+
impl<K, V> AHashExt for StableHashMap<K, V> {
73+
/// Creates an empty `StableHashMap` with the specified capacity with AHash.
74+
///
75+
/// The hash map will be able to hold at least `capacity` elements without
76+
/// reallocating. If `capacity` is 0, the hash map will not allocate.
77+
///
78+
/// # Examples
79+
///
80+
/// ```
81+
/// use bevy_utils::{StableHashMap, AHashExt};
82+
/// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10);
83+
/// assert!(map.capacity() >= 10);
84+
/// ```
85+
#[inline]
86+
fn with_capacity(capacity: usize) -> Self {
87+
StableHashMap::with_capacity_and_hasher(capacity, FixedState::default())
88+
}
89+
}
90+
4991
/// A std hash set implementing AHash, a high speed keyed hashing algorithm
5092
/// intended for use in in-memory hashmaps.
5193
///
5294
/// AHash is designed for performance and is NOT cryptographically secure.
5395
pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
5496

97+
impl<K> AHashExt for HashSet<K> {
98+
/// Creates an empty `HashSet` with the specified capacity with AHash.
99+
///
100+
/// The hash set will be able to hold at least `capacity` elements without
101+
/// reallocating. If `capacity` is 0, the hash set will not allocate.
102+
///
103+
/// # Examples
104+
///
105+
/// ```
106+
/// use bevy_utils::{HashSet, AHashExt};
107+
/// let set: HashSet<i32> = HashSet::with_capacity(10);
108+
/// assert!(set.capacity() >= 10);
109+
/// ```
110+
#[inline]
111+
fn with_capacity(capacity: usize) -> Self {
112+
HashSet::with_capacity_and_hasher(capacity, RandomState::default())
113+
}
114+
}
115+
55116
/// A stable std hash set implementing AHash, a high speed keyed hashing algorithm
56117
/// intended for use in in-memory hashmaps.
57118
///
@@ -60,3 +121,22 @@ pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
60121
///
61122
/// AHash is designed for performance and is NOT cryptographically secure.
62123
pub type StableHashSet<K> = std::collections::HashSet<K, FixedState>;
124+
125+
impl<K> AHashExt for StableHashSet<K> {
126+
/// Creates an empty `StableHashSet` with the specified capacity with AHash.
127+
///
128+
/// The hash set will be able to hold at least `capacity` elements without
129+
/// reallocating. If `capacity` is 0, the hash set will not allocate.
130+
///
131+
/// # Examples
132+
///
133+
/// ```
134+
/// use bevy_utils::{StableHashSet, AHashExt};
135+
/// let set: StableHashSet<i32> = StableHashSet::with_capacity(10);
136+
/// assert!(set.capacity() >= 10);
137+
/// ```
138+
#[inline]
139+
fn with_capacity(capacity: usize) -> Self {
140+
StableHashSet::with_capacity_and_hasher(capacity, FixedState::default())
141+
}
142+
}

0 commit comments

Comments
 (0)