Skip to content

Commit bdb86b0

Browse files
committed
Merge branch 'arthurprs-small-optimizations'
2 parents 5514533 + 4cdfc39 commit bdb86b0

18 files changed

+413
-447
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline = ["hashbrown/inline-more"]
2020
[dependencies]
2121
lock_api = "0.4.10"
2222
parking_lot_core = "0.9.8"
23-
hashbrown = { version = "0.14.0", default-features = false }
23+
hashbrown = { version = "0.14.0", default-features = false, features = ["raw"] }
2424
serde = { version = "1.0.188", optional = true, features = ["derive"] }
2525
cfg-if = "1.0.0"
2626
rayon = { version = "1.7.0", optional = true }

src/iter.rs

+33-38
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use super::mapref::multiple::{RefMulti, RefMutMulti};
2-
use super::util;
32
use crate::lock::{RwLockReadGuard, RwLockWriteGuard};
43
use crate::t::Map;
54
use crate::util::SharedValue;
65
use crate::{DashMap, HashMap};
76
use core::hash::{BuildHasher, Hash};
87
use core::mem;
9-
use hashbrown::hash_map;
108
use std::collections::hash_map::RandomState;
9+
use std::marker::PhantomData;
1110
use std::sync::Arc;
1211

1312
/// Iterator over a DashMap yielding key value pairs.
@@ -39,7 +38,7 @@ impl<K: Eq + Hash, V, S: BuildHasher + Clone> OwningIter<K, V, S> {
3938
}
4039
}
4140

42-
type GuardOwningIter<K, V> = hash_map::IntoIter<K, SharedValue<V>>;
41+
type GuardOwningIter<K, V> = hashbrown::raw::RawIntoIter<(K, SharedValue<V>)>;
4342

4443
impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for OwningIter<K, V, S> {
4544
type Item = (K, V);
@@ -59,9 +58,7 @@ impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for OwningIter<K, V, S> {
5958
//let guard = unsafe { self.map._yield_read_shard(self.shard_i) };
6059
let mut shard_wl = unsafe { self.map._yield_write_shard(self.shard_i) };
6160

62-
let hasher = self.map._hasher();
63-
64-
let map = mem::replace(&mut *shard_wl, HashMap::with_hasher(hasher));
61+
let map = mem::take(&mut *shard_wl);
6562

6663
drop(shard_wl);
6764

@@ -91,14 +88,14 @@ where
9188
{
9289
}
9390

94-
type GuardIter<'a, K, V, S> = (
95-
Arc<RwLockReadGuard<'a, HashMap<K, V, S>>>,
96-
hash_map::Iter<'a, K, SharedValue<V>>,
91+
type GuardIter<'a, K, V> = (
92+
Arc<RwLockReadGuard<'a, HashMap<K, V>>>,
93+
hashbrown::raw::RawIter<(K, SharedValue<V>)>,
9794
);
9895

99-
type GuardIterMut<'a, K, V, S> = (
100-
Arc<RwLockWriteGuard<'a, HashMap<K, V, S>>>,
101-
hash_map::IterMut<'a, K, SharedValue<V>>,
96+
type GuardIterMut<'a, K, V> = (
97+
Arc<RwLockWriteGuard<'a, HashMap<K, V>>>,
98+
hashbrown::raw::RawIter<(K, SharedValue<V>)>,
10299
);
103100

104101
/// Iterator over a DashMap yielding immutable references.
@@ -115,7 +112,8 @@ type GuardIterMut<'a, K, V, S> = (
115112
pub struct Iter<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {
116113
map: &'a M,
117114
shard_i: usize,
118-
current: Option<GuardIter<'a, K, V, S>>,
115+
current: Option<GuardIter<'a, K, V>>,
116+
marker: PhantomData<S>,
119117
}
120118

121119
impl<'i, K: Clone + Hash + Eq, V: Clone, S: Clone + BuildHasher> Clone for Iter<'i, K, V, S> {
@@ -148,22 +146,25 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
148146
map,
149147
shard_i: 0,
150148
current: None,
149+
marker: PhantomData,
151150
}
152151
}
153152
}
154153

155154
impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iterator
156155
for Iter<'a, K, V, S, M>
157156
{
158-
type Item = RefMulti<'a, K, V, S>;
157+
type Item = RefMulti<'a, K, V>;
159158

160159
fn next(&mut self) -> Option<Self::Item> {
161160
loop {
162161
if let Some(current) = self.current.as_mut() {
163-
if let Some((k, v)) = current.1.next() {
164-
let guard = current.0.clone();
165-
166-
return unsafe { Some(RefMulti::new(guard, k, v.get())) };
162+
if let Some(b) = current.1.next() {
163+
return unsafe {
164+
let (k, v) = b.as_ref();
165+
let guard = current.0.clone();
166+
Some(RefMulti::new(guard, k, v.get()))
167+
};
167168
}
168169
}
169170

@@ -173,9 +174,7 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
173174

174175
let guard = unsafe { self.map._yield_read_shard(self.shard_i) };
175176

176-
let sref: &HashMap<K, V, S> = unsafe { util::change_lifetime_const(&*guard) };
177-
178-
let iter = sref.iter();
177+
let iter = unsafe { guard.iter() };
179178

180179
self.current = Some((Arc::new(guard), iter));
181180

@@ -199,7 +198,8 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iter
199198
pub struct IterMut<'a, K, V, S = RandomState, M = DashMap<K, V, S>> {
200199
map: &'a M,
201200
shard_i: usize,
202-
current: Option<GuardIterMut<'a, K, V, S>>,
201+
current: Option<GuardIterMut<'a, K, V>>,
202+
marker: PhantomData<S>,
203203
}
204204

205205
unsafe impl<'a, 'i, K, V, S, M> Send for IterMut<'i, K, V, S, M>
@@ -228,40 +228,35 @@ impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>>
228228
map,
229229
shard_i: 0,
230230
current: None,
231+
marker: PhantomData,
231232
}
232233
}
233234
}
234235

235236
impl<'a, K: Eq + Hash, V, S: 'a + BuildHasher + Clone, M: Map<'a, K, V, S>> Iterator
236237
for IterMut<'a, K, V, S, M>
237238
{
238-
type Item = RefMutMulti<'a, K, V, S>;
239+
type Item = RefMutMulti<'a, K, V>;
239240

240241
fn next(&mut self) -> Option<Self::Item> {
241242
loop {
242243
if let Some(current) = self.current.as_mut() {
243-
if let Some((k, v)) = current.1.next() {
244-
let guard = current.0.clone();
245-
246-
unsafe {
247-
let k = util::change_lifetime_const(k);
248-
249-
let v = &mut *v.as_ptr();
250-
251-
return Some(RefMutMulti::new(guard, k, v));
252-
}
244+
if let Some(b) = current.1.next() {
245+
return unsafe {
246+
let (k, v) = b.as_mut();
247+
let guard = current.0.clone();
248+
Some(RefMutMulti::new(guard, k, v.get_mut()))
249+
};
253250
}
254251
}
255252

256253
if self.shard_i == self.map._shard_count() {
257254
return None;
258255
}
259256

260-
let mut guard = unsafe { self.map._yield_write_shard(self.shard_i) };
261-
262-
let sref: &mut HashMap<K, V, S> = unsafe { util::change_lifetime_mut(&mut *guard) };
257+
let guard = unsafe { self.map._yield_write_shard(self.shard_i) };
263258

264-
let iter = sref.iter_mut();
259+
let iter = unsafe { guard.iter() };
265260

266261
self.current = Some((Arc::new(guard), iter));
267262

@@ -285,7 +280,7 @@ mod tests {
285280
let mut c = 0;
286281

287282
for shard in map.shards() {
288-
c += shard.write().iter_mut().count();
283+
c += unsafe { shard.write().iter().count() };
289284
}
290285

291286
assert_eq!(c, 1);

src/iter_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'a, K: Eq + Hash, S: 'a + BuildHasher + Clone, M: Map<'a, K, (), S>> Iter<'
6363
impl<'a, K: Eq + Hash, S: 'a + BuildHasher + Clone, M: Map<'a, K, (), S>> Iterator
6464
for Iter<'a, K, S, M>
6565
{
66-
type Item = RefMulti<'a, K, S>;
66+
type Item = RefMulti<'a, K>;
6767

6868
fn next(&mut self) -> Option<Self::Item> {
6969
self.inner.next().map(RefMulti::new)

0 commit comments

Comments
 (0)