Skip to content

Commit

Permalink
stops consuming pinned vectors with a recycler
Browse files Browse the repository at this point in the history
If the vector is pinned and has a recycler, From<PinnedVec>
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.
  • Loading branch information
behzadnouri committed Apr 9, 2021
1 parent 8ec7e2e commit ad47649
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion core/src/verified_vote_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions ledger/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 6 additions & 18 deletions perf/src/cuda_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl<T: Default + Clone + Sized> Reset for PinnedVec<T> {
impl<T: Clone + Default + Sized> From<PinnedVec<T>> for Vec<T> {
fn from(mut pinned_vec: PinnedVec<T>) -> 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;
}
Expand All @@ -87,15 +93,6 @@ impl<T: Clone + Default + Sized> From<PinnedVec<T>> for Vec<T> {
}
}

impl<T: Clone + Default + Sized> IntoIterator for PinnedVec<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;

fn into_iter(self) -> Self::IntoIter {
<Self as Into<Vec<T>>>::into(self).into_iter()
}
}

impl<'a, T: Clone + Default + Sized> IntoIterator for &'a PinnedVec<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
Expand Down Expand Up @@ -151,15 +148,6 @@ impl<'a, T: Clone + Send + Sync + Default + Sized> IntoParallelIterator for &'a
}
}

impl<T: Clone + Default + Send + Sized> IntoParallelIterator for PinnedVec<T> {
type Item = T;
type Iter = rayon::vec::IntoIter<T>;

fn into_par_iter(self) -> Self::Iter {
<Self as Into<Vec<T>>>::into(self).into_par_iter()
}
}

impl<T: Clone + Default + Sized> PinnedVec<T> {
pub fn reserve_and_pin(&mut self, size: usize) {
if self.x.capacity() < size {
Expand Down

0 comments on commit ad47649

Please sign in to comment.