Skip to content

Commit

Permalink
Avoid overallocating or growing Vec in Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Aug 27, 2024
1 parent 633aadb commit 3e2eb9c
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,13 @@ pub struct DashMap<K, V, S = RandomState> {

impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for DashMap<K, V, S> {
fn clone(&self) -> Self {
let mut inner_shards = Vec::new();

for shard in self.shards.iter() {
let shard = shard.read();

inner_shards.push(CachePadded::new(RwLock::new((*shard).clone())));
fn clone_rwlock<T: Clone>(lock: &CachePadded<RwLock<T>>) -> CachePadded<RwLock<T>> {
CachePadded::new(RwLock::new(lock.read().clone()))
}

Self {
shift: self.shift,
shards: inner_shards.into_boxed_slice(),
shards: self.shards.iter().map(clone_rwlock).collect(),
hasher: self.hasher.clone(),
}
}
Expand Down

0 comments on commit 3e2eb9c

Please sign in to comment.