Skip to content

Commit 39864b2

Browse files
authored
Avoid overallocating or growing Vec in Clone (#295)
1 parent f2d248e commit 39864b2

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,13 @@ pub struct DashMap<K, V, S = RandomState> {
9494

9595
impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for DashMap<K, V, S> {
9696
fn clone(&self) -> Self {
97-
let mut inner_shards = Vec::new();
98-
99-
for shard in self.shards.iter() {
100-
let shard = shard.read();
101-
102-
inner_shards.push(CachePadded::new(RwLock::new((*shard).clone())));
97+
fn clone_rwlock<T: Clone>(lock: &CachePadded<RwLock<T>>) -> CachePadded<RwLock<T>> {
98+
CachePadded::new(RwLock::new(lock.read().clone()))
10399
}
104100

105101
Self {
106102
shift: self.shift,
107-
shards: inner_shards.into_boxed_slice(),
103+
shards: self.shards.iter().map(clone_rwlock).collect(),
108104
hasher: self.hasher.clone(),
109105
}
110106
}

0 commit comments

Comments
 (0)