stops consuming pinned vectors with a recycler (#16441)

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.
This commit is contained in:
behzad nouri
2021-04-09 16:55:24 +00:00
committed by GitHub
parent 8ec7e2e14f
commit 22a18a68e3
4 changed files with 11 additions and 22 deletions

View File

@ -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;
}
@ -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>;
@ -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 {