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 Feb 13, 2024
1 parent 626b98d commit c6fd86e
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 @@ -93,17 +93,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(RwLock::new((*shard).clone()));
fn clone_rwlock<T: Clone>(lock: &RwLock<T>) -> RwLock<T> {
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 c6fd86e

Please sign in to comment.