Skip to content

Commit 90e4d3b

Browse files
committed
Parametrize map and set over allocator
1 parent 45b5fb2 commit 90e4d3b

File tree

7 files changed

+548
-302
lines changed

7 files changed

+548
-302
lines changed

src/external_trait_impls/rayon/map.rs

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Rayon extensions for `HashMap`.
22
33
use crate::hash_map::HashMap;
4+
use crate::raw::{AllocRef, Global};
45
use core::fmt;
56
use core::hash::{BuildHasher, Hash};
67
use rayon::iter::plumbing::UnindexedConsumer;
@@ -15,11 +16,11 @@ use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, Pa
1516
/// [`par_iter`]: /hashbrown/struct.HashMap.html#method.par_iter
1617
/// [`HashMap`]: /hashbrown/struct.HashMap.html
1718
/// [`IntoParallelRefIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefIterator.html
18-
pub struct ParIter<'a, K, V, S> {
19-
map: &'a HashMap<K, V, S>,
19+
pub struct ParIter<'a, K, V, S, A: AllocRef + Clone = Global> {
20+
map: &'a HashMap<K, V, S, A>,
2021
}
2122

22-
impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParIter<'a, K, V, S> {
23+
impl<'a, K: Sync, V: Sync, S: Sync, A: AllocRef + Clone + Sync> ParallelIterator for ParIter<'a, K, V, S, A> {
2324
type Item = (&'a K, &'a V);
2425

2526
#[cfg_attr(feature = "inline-more", inline)]
@@ -38,14 +39,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParIter<'a, K, V, S> {
3839
}
3940
}
4041

41-
impl<K, V, S> Clone for ParIter<'_, K, V, S> {
42+
impl<K, V, S, A: AllocRef + Clone> Clone for ParIter<'_, K, V, S, A> {
4243
#[cfg_attr(feature = "inline-more", inline)]
4344
fn clone(&self) -> Self {
4445
ParIter { map: self.map }
4546
}
4647
}
4748

48-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParIter<'_, K, V, S> {
49+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug for ParIter<'_, K, V, S, A> {
4950
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5051
self.map.iter().fmt(f)
5152
}
@@ -58,11 +59,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for Pa
5859
///
5960
/// [`par_keys`]: /hashbrown/struct.HashMap.html#method.par_keys
6061
/// [`HashMap`]: /hashbrown/struct.HashMap.html
61-
pub struct ParKeys<'a, K, V, S> {
62-
map: &'a HashMap<K, V, S>,
62+
pub struct ParKeys<'a, K, V, S, A: AllocRef + Clone> {
63+
map: &'a HashMap<K, V, S, A>,
6364
}
6465

65-
impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParKeys<'a, K, V, S> {
66+
impl<'a, K: Sync, V: Sync, S: Sync, A: AllocRef + Clone + Sync> ParallelIterator for ParKeys<'a, K, V, S, A> {
6667
type Item = &'a K;
6768

6869
#[cfg_attr(feature = "inline-more", inline)]
@@ -78,14 +79,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParKeys<'a, K, V, S> {
7879
}
7980
}
8081

81-
impl<K, V, S> Clone for ParKeys<'_, K, V, S> {
82+
impl<K, V, S, A: AllocRef + Clone> Clone for ParKeys<'_, K, V, S, A> {
8283
#[cfg_attr(feature = "inline-more", inline)]
8384
fn clone(&self) -> Self {
8485
ParKeys { map: self.map }
8586
}
8687
}
8788

88-
impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K, V, S> {
89+
impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher, A: AllocRef + Clone> fmt::Debug for ParKeys<'_, K, V, S, A> {
8990
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9091
self.map.keys().fmt(f)
9192
}
@@ -98,11 +99,11 @@ impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K,
9899
///
99100
/// [`par_values`]: /hashbrown/struct.HashMap.html#method.par_values
100101
/// [`HashMap`]: /hashbrown/struct.HashMap.html
101-
pub struct ParValues<'a, K, V, S> {
102-
map: &'a HashMap<K, V, S>,
102+
pub struct ParValues<'a, K, V, S, A: AllocRef + Clone = Global> {
103+
map: &'a HashMap<K, V, S, A>,
103104
}
104105

105-
impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParValues<'a, K, V, S> {
106+
impl<'a, K: Sync, V: Sync, S: Sync, A: AllocRef + Clone + Sync> ParallelIterator for ParValues<'a, K, V, S, A> {
106107
type Item = &'a V;
107108

108109
#[cfg_attr(feature = "inline-more", inline)]
@@ -118,14 +119,14 @@ impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParValues<'a, K, V, S>
118119
}
119120
}
120121

121-
impl<K, V, S> Clone for ParValues<'_, K, V, S> {
122+
impl<K, V, S, A: AllocRef + Clone> Clone for ParValues<'_, K, V, S, A> {
122123
#[cfg_attr(feature = "inline-more", inline)]
123124
fn clone(&self) -> Self {
124125
ParValues { map: self.map }
125126
}
126127
}
127128

128-
impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K, V, S> {
129+
impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug for ParValues<'_, K, V, S, A> {
129130
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130131
self.map.values().fmt(f)
131132
}
@@ -140,11 +141,11 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K
140141
/// [`par_iter_mut`]: /hashbrown/struct.HashMap.html#method.par_iter_mut
141142
/// [`HashMap`]: /hashbrown/struct.HashMap.html
142143
/// [`IntoParallelRefMutIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefMutIterator.html
143-
pub struct ParIterMut<'a, K, V, S> {
144-
map: &'a mut HashMap<K, V, S>,
144+
pub struct ParIterMut<'a, K, V, S, A: AllocRef + Clone> {
145+
map: &'a mut HashMap<K, V, S, A>,
145146
}
146147

147-
impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K, V, S> {
148+
impl<'a, K: Send + Sync, V: Send, S: Send, A: AllocRef + Clone + Sync> ParallelIterator for ParIterMut<'a, K, V, S, A> {
148149
type Item = (&'a K, &'a mut V);
149150

150151
#[cfg_attr(feature = "inline-more", inline)]
@@ -163,8 +164,8 @@ impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K
163164
}
164165
}
165166

166-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
167-
for ParIterMut<'_, K, V, S>
167+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug
168+
for ParIterMut<'_, K, V, S, A>
168169
{
169170
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170171
self.map.iter().fmt(f)
@@ -178,11 +179,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
178179
///
179180
/// [`par_values_mut`]: /hashbrown/struct.HashMap.html#method.par_values_mut
180181
/// [`HashMap`]: /hashbrown/struct.HashMap.html
181-
pub struct ParValuesMut<'a, K, V, S> {
182-
map: &'a mut HashMap<K, V, S>,
182+
pub struct ParValuesMut<'a, K, V, S, A: AllocRef + Clone = Global> {
183+
map: &'a mut HashMap<K, V, S, A>,
183184
}
184185

185-
impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V, S> {
186+
impl<'a, K: Send, V: Send, S: Send, A: AllocRef + Clone + Send> ParallelIterator for ParValuesMut<'a, K, V, S, A> {
186187
type Item = &'a mut V;
187188

188189
#[cfg_attr(feature = "inline-more", inline)]
@@ -198,7 +199,7 @@ impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V,
198199
}
199200
}
200201

201-
impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_, K, V, S> {
202+
impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug for ParValuesMut<'_, K, V, S, A> {
202203
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203204
self.map.values().fmt(f)
204205
}
@@ -213,11 +214,11 @@ impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_
213214
/// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
214215
/// [`HashMap`]: /hashbrown/struct.HashMap.html
215216
/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
216-
pub struct IntoParIter<K, V, S> {
217-
map: HashMap<K, V, S>,
217+
pub struct IntoParIter<K, V, S, A: AllocRef + Clone = Global> {
218+
map: HashMap<K, V, S, A>,
218219
}
219220

220-
impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
221+
impl<K: Send, V: Send, S: Send, A: AllocRef + Clone + Send> ParallelIterator for IntoParIter<K, V, S, A> {
221222
type Item = (K, V);
222223

223224
#[cfg_attr(feature = "inline-more", inline)]
@@ -229,7 +230,7 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
229230
}
230231
}
231232

232-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for IntoParIter<K, V, S> {
233+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug for IntoParIter<K, V, S, A> {
233234
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234235
self.map.iter().fmt(f)
235236
}
@@ -242,11 +243,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for In
242243
///
243244
/// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
244245
/// [`HashMap`]: /hashbrown/struct.HashMap.html
245-
pub struct ParDrain<'a, K, V, S> {
246-
map: &'a mut HashMap<K, V, S>,
246+
pub struct ParDrain<'a, K, V, S, A: AllocRef + Clone = Global> {
247+
map: &'a mut HashMap<K, V, S, A>,
247248
}
248249

249-
impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
250+
impl<K: Send, V: Send, S: Send, A: AllocRef + Clone + Sync> ParallelIterator for ParDrain<'_, K, V, S, A> {
250251
type Item = (K, V);
251252

252253
#[cfg_attr(feature = "inline-more", inline)]
@@ -258,48 +259,49 @@ impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
258259
}
259260
}
260261

261-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
262-
for ParDrain<'_, K, V, S>
262+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher, A: AllocRef + Clone> fmt::Debug
263+
for ParDrain<'_, K, V, S, A>
263264
{
264265
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
265266
self.map.iter().fmt(f)
266267
}
267268
}
268269

269-
impl<K: Sync, V: Sync, S: Sync> HashMap<K, V, S> {
270+
impl<K: Sync, V: Sync, S: Sync, A: AllocRef + Clone + Sync> HashMap<K, V, S, A> {
270271
/// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
271272
#[cfg_attr(feature = "inline-more", inline)]
272-
pub fn par_keys(&self) -> ParKeys<'_, K, V, S> {
273+
pub fn par_keys(&self) -> ParKeys<'_, K, V, S, A> {
273274
ParKeys { map: self }
274275
}
275276

276277
/// Visits (potentially in parallel) immutably borrowed values in an arbitrary order.
277278
#[cfg_attr(feature = "inline-more", inline)]
278-
pub fn par_values(&self) -> ParValues<'_, K, V, S> {
279+
pub fn par_values(&self) -> ParValues<'_, K, V, S, A> {
279280
ParValues { map: self }
280281
}
281282
}
282283

283-
impl<K: Send, V: Send, S: Send> HashMap<K, V, S> {
284+
impl<K: Send, V: Send, S: Send, A: AllocRef + Clone + Sync> HashMap<K, V, S, A> {
284285
/// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
285286
#[cfg_attr(feature = "inline-more", inline)]
286-
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V, S> {
287+
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V, S, A> {
287288
ParValuesMut { map: self }
288289
}
289290

290291
/// Consumes (potentially in parallel) all values in an arbitrary order,
291292
/// while preserving the map's allocated memory for reuse.
292293
#[cfg_attr(feature = "inline-more", inline)]
293-
pub fn par_drain(&mut self) -> ParDrain<'_, K, V, S> {
294+
pub fn par_drain(&mut self) -> ParDrain<'_, K, V, S, A> {
294295
ParDrain { map: self }
295296
}
296297
}
297298

298-
impl<K, V, S> HashMap<K, V, S>
299+
impl<K, V, S, A> HashMap<K, V, S, A>
299300
where
300301
K: Eq + Hash + Sync,
301302
V: PartialEq + Sync,
302303
S: BuildHasher + Sync,
304+
A: AllocRef + Clone + Sync,
303305
{
304306
/// Returns `true` if the map is equal to another,
305307
/// i.e. both maps contain the same keys mapped to the same values.
@@ -313,29 +315,29 @@ where
313315
}
314316
}
315317

316-
impl<K: Send, V: Send, S: Send> IntoParallelIterator for HashMap<K, V, S> {
318+
impl<K: Send, V: Send, S: Send, A: AllocRef + Clone + Send> IntoParallelIterator for HashMap<K, V, S, A> {
317319
type Item = (K, V);
318-
type Iter = IntoParIter<K, V, S>;
320+
type Iter = IntoParIter<K, V, S, A>;
319321

320322
#[cfg_attr(feature = "inline-more", inline)]
321323
fn into_par_iter(self) -> Self::Iter {
322324
IntoParIter { map: self }
323325
}
324326
}
325327

326-
impl<'a, K: Sync, V: Sync, S: Sync> IntoParallelIterator for &'a HashMap<K, V, S> {
328+
impl<'a, K: Sync, V: Sync, S: Sync, A: AllocRef + Clone + Sync> IntoParallelIterator for &'a HashMap<K, V, S, A> {
327329
type Item = (&'a K, &'a V);
328-
type Iter = ParIter<'a, K, V, S>;
330+
type Iter = ParIter<'a, K, V, S, A>;
329331

330332
#[cfg_attr(feature = "inline-more", inline)]
331333
fn into_par_iter(self) -> Self::Iter {
332334
ParIter { map: self }
333335
}
334336
}
335337

336-
impl<'a, K: Send + Sync, V: Send, S: Send> IntoParallelIterator for &'a mut HashMap<K, V, S> {
338+
impl<'a, K: Send + Sync, V: Send, S: Send, A: AllocRef + Clone + Sync> IntoParallelIterator for &'a mut HashMap<K, V, S, A> {
337339
type Item = (&'a K, &'a mut V);
338-
type Iter = ParIterMut<'a, K, V, S>;
340+
type Iter = ParIterMut<'a, K, V, S, A>;
339341

340342
#[cfg_attr(feature = "inline-more", inline)]
341343
fn into_par_iter(self) -> Self::Iter {
@@ -347,7 +349,7 @@ impl<'a, K: Send + Sync, V: Send, S: Send> IntoParallelIterator for &'a mut Hash
347349
/// hashmap. If multiple pairs correspond to the same key, then the
348350
/// ones produced earlier in the parallel iterator will be
349351
/// overwritten, just as with a sequential iterator.
350-
impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
352+
impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S, Global>
351353
where
352354
K: Eq + Hash + Send,
353355
V: Send,
@@ -364,11 +366,12 @@ where
364366
}
365367

366368
/// Extend a hash map with items from a parallel iterator.
367-
impl<K, V, S> ParallelExtend<(K, V)> for HashMap<K, V, S>
369+
impl<K, V, S, A> ParallelExtend<(K, V)> for HashMap<K, V, S, A>
368370
where
369371
K: Eq + Hash + Send,
370372
V: Send,
371373
S: BuildHasher,
374+
A: AllocRef + Clone,
372375
{
373376
fn par_extend<I>(&mut self, par_iter: I)
374377
where
@@ -379,11 +382,12 @@ where
379382
}
380383

381384
/// Extend a hash map with copied items from a parallel iterator.
382-
impl<'a, K, V, S> ParallelExtend<(&'a K, &'a V)> for HashMap<K, V, S>
385+
impl<'a, K, V, S, A> ParallelExtend<(&'a K, &'a V)> for HashMap<K, V, S, A>
383386
where
384387
K: Copy + Eq + Hash + Sync,
385388
V: Copy + Sync,
386389
S: BuildHasher,
390+
A: AllocRef + Clone,
387391
{
388392
fn par_extend<I>(&mut self, par_iter: I)
389393
where
@@ -394,12 +398,13 @@ where
394398
}
395399

396400
// This is equal to the normal `HashMap` -- no custom advantage.
397-
fn extend<K, V, S, I>(map: &mut HashMap<K, V, S>, par_iter: I)
401+
fn extend<K, V, S, A, I>(map: &mut HashMap<K, V, S, A>, par_iter: I)
398402
where
399403
K: Eq + Hash,
400404
S: BuildHasher,
401405
I: IntoParallelIterator,
402-
HashMap<K, V, S>: Extend<I::Item>,
406+
A: AllocRef + Clone,
407+
HashMap<K, V, S, A>: Extend<I::Item>,
403408
{
404409
let (list, len) = super::helpers::collect(par_iter);
405410

src/external_trait_impls/rayon/raw.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::raw::Bucket;
2-
use crate::raw::{Alloc, RawIterRange, RawTable};
2+
use crate::raw::{AllocRef, RawIterRange, RawTable};
33
use crate::scopeguard::guard;
44
use alloc::alloc::dealloc;
55
use core::marker::PhantomData;
@@ -54,11 +54,11 @@ impl<T> UnindexedProducer for ParIterProducer<T> {
5454
}
5555

5656
/// Parallel iterator which consumes a table and returns elements.
57-
pub struct RawIntoParIter<T, A: Alloc + Clone> {
57+
pub struct RawIntoParIter<T, A: AllocRef + Clone> {
5858
table: RawTable<T, A>,
5959
}
6060

61-
impl<T: Send, A: Alloc + Clone> ParallelIterator for RawIntoParIter<T, A> {
61+
impl<T: Send, A: AllocRef + Clone> ParallelIterator for RawIntoParIter<T, A> {
6262
type Item = T;
6363

6464
#[cfg_attr(feature = "inline-more", inline)]
@@ -80,16 +80,16 @@ impl<T: Send, A: Alloc + Clone> ParallelIterator for RawIntoParIter<T, A> {
8080
}
8181

8282
/// Parallel iterator which consumes elements without freeing the table storage.
83-
pub struct RawParDrain<'a, T, A: Alloc + Clone> {
83+
pub struct RawParDrain<'a, T, A: AllocRef + Clone> {
8484
// We don't use a &'a mut RawTable<T> because we want RawParDrain to be
8585
// covariant over T.
8686
table: NonNull<RawTable<T, A>>,
8787
marker: PhantomData<&'a RawTable<T, A>>,
8888
}
8989

90-
unsafe impl<T, A: Alloc + Clone> Send for RawParDrain<'_, T, A> {}
90+
unsafe impl<T, A: AllocRef + Clone> Send for RawParDrain<'_, T, A> {}
9191

92-
impl<T: Send, A: Alloc + Clone> ParallelIterator for RawParDrain<'_, T, A> {
92+
impl<T: Send, A: AllocRef + Clone> ParallelIterator for RawParDrain<'_, T, A> {
9393
type Item = T;
9494

9595
#[cfg_attr(feature = "inline-more", inline)]
@@ -107,7 +107,7 @@ impl<T: Send, A: Alloc + Clone> ParallelIterator for RawParDrain<'_, T, A> {
107107
}
108108
}
109109

110-
impl<T, A: Alloc + Clone> Drop for RawParDrain<'_, T, A> {
110+
impl<T, A: AllocRef + Clone> Drop for RawParDrain<'_, T, A> {
111111
fn drop(&mut self) {
112112
// If drive_unindexed is not called then simply clear the table.
113113
unsafe { self.table.as_mut().clear() }
@@ -166,7 +166,7 @@ impl<T> Drop for ParDrainProducer<T> {
166166
}
167167
}
168168

169-
impl<T, A: Alloc + Clone> RawTable<T, A> {
169+
impl<T, A: AllocRef + Clone> RawTable<T, A> {
170170
/// Returns a parallel iterator over the elements in a `RawTable`.
171171
#[cfg_attr(feature = "inline-more", inline)]
172172
pub fn par_iter(&self) -> RawParIter<T> {

0 commit comments

Comments
 (0)