From 22a18a68e3ee68ae013d647e62e12128433d7230 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Fri, 9 Apr 2021 16:55:24 +0000 Subject: [PATCH] stops consuming pinned vectors with a recycler (#16441) If the vector is pinned and has a recycler, From implementation of Vec should clone (instead of consuming) the underlying vector so that the next allocation of a PinnedVec will recycle an already pinned one. --- core/src/cluster_info.rs | 2 +- core/src/verified_vote_packets.rs | 3 ++- ledger/src/entry.rs | 4 ++-- perf/src/cuda_runtime.rs | 24 ++++++------------------ 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/core/src/cluster_info.rs b/core/src/cluster_info.rs index e233980c9..40edf3f15 100644 --- a/core/src/cluster_info.rs +++ b/core/src/cluster_info.rs @@ -2816,7 +2816,7 @@ impl ClusterInfo { let packets: Vec<_> = requests_receiver.recv_timeout(RECV_TIMEOUT)?.packets.into(); let mut packets = VecDeque::from(packets); while let Ok(packet) = requests_receiver.try_recv() { - packets.extend(packet.packets.into_iter()); + packets.extend(packet.packets.iter().cloned()); let excess_count = packets.len().saturating_sub(MAX_GOSSIP_TRAFFIC); if excess_count > 0 { packets.drain(0..excess_count); diff --git a/core/src/verified_vote_packets.rs b/core/src/verified_vote_packets.rs index f8841ca51..9136b668c 100644 --- a/core/src/verified_vote_packets.rs +++ b/core/src/verified_vote_packets.rs @@ -59,7 +59,8 @@ impl VerifiedVotePackets { } let packets = votes .into_iter() - .flat_map(|(_, (_, packets))| packets.packets.clone()) + .flat_map(|(_, (_, packets))| &packets.packets) + .cloned() .collect(); (new_update_version, Packets::new(packets)) } diff --git a/ledger/src/entry.rs b/ledger/src/entry.rs index a77d3eb44..c52e6f4db 100644 --- a/ledger/src/entry.rs +++ b/ledger/src/entry.rs @@ -283,9 +283,9 @@ impl EntryVerificationState { .zip(entries) .all(|((hash, tx_hash), answer)| { if answer.num_hashes == 0 { - hash == answer.hash + *hash == answer.hash } else { - let mut poh = Poh::new(hash, None); + let mut poh = Poh::new(*hash, None); if let Some(mixin) = tx_hash { poh.record(*mixin).unwrap().hash == answer.hash } else { diff --git a/perf/src/cuda_runtime.rs b/perf/src/cuda_runtime.rs index 45561af50..fee2405b0 100644 --- a/perf/src/cuda_runtime.rs +++ b/perf/src/cuda_runtime.rs @@ -78,6 +78,12 @@ impl Reset for PinnedVec { impl From> for Vec { fn from(mut pinned_vec: PinnedVec) -> Self { if pinned_vec.pinned { + // If the vector is pinned and has a recycler, just return a clone + // so that the next allocation of a PinnedVec will recycle an + // already pinned one. + if pinned_vec.recycler.strong_count() != 0 { + return pinned_vec.x.clone(); + } unpin(pinned_vec.x.as_mut_ptr()); pinned_vec.pinned = false; } @@ -87,15 +93,6 @@ impl From> for Vec { } } -impl IntoIterator for PinnedVec { - type Item = T; - type IntoIter = std::vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - >>::into(self).into_iter() - } -} - impl<'a, T: Clone + Default + Sized> IntoIterator for &'a PinnedVec { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -151,15 +148,6 @@ impl<'a, T: Clone + Send + Sync + Default + Sized> IntoParallelIterator for &'a } } -impl IntoParallelIterator for PinnedVec { - type Item = T; - type Iter = rayon::vec::IntoIter; - - fn into_par_iter(self) -> Self::Iter { - >>::into(self).into_par_iter() - } -} - impl PinnedVec { pub fn reserve_and_pin(&mut self, size: usize) { if self.x.capacity() < size {