From a4d27380b7e4b70f87ff5fb9346077ffa71b622d Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Sat, 25 Feb 2023 16:59:32 +0900 Subject: [PATCH 1/2] binary_heap: Make RebuildOnDrop a common helper. This helper was written for retain() but will also be useful for extend(). --- collections/binary_heap/mod.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/collections/binary_heap/mod.rs b/collections/binary_heap/mod.rs index f1d0a30..b6cb791 100644 --- a/collections/binary_heap/mod.rs +++ b/collections/binary_heap/mod.rs @@ -400,6 +400,17 @@ impl fmt::Debug for BinaryHeap { } } +struct RebuildOnDrop<'a, T: Ord> { + heap: &'a mut BinaryHeap, + rebuild_from: usize, +} + +impl<'a, T: Ord> Drop for RebuildOnDrop<'a, T> { + fn drop(&mut self) { + self.heap.rebuild_tail(self.rebuild_from); + } +} + impl BinaryHeap { /// Creates an empty `BinaryHeap` as a max-heap. /// @@ -851,30 +862,19 @@ impl BinaryHeap { where F: FnMut(&T) -> bool, { - struct RebuildOnDrop<'a, T: Ord> { - heap: &'a mut BinaryHeap, - first_removed: usize, - } - - let mut guard = RebuildOnDrop { first_removed: self.len(), heap: self }; - + // rebuild_start will be updated to the first touched element below, and the rebuild will + // only be done for the tail. + let mut guard = RebuildOnDrop { rebuild_from: self.len(), heap: self }; let mut i = 0; + guard.heap.data.retain(|e| { let keep = f(e); - if !keep && i < guard.first_removed { - guard.first_removed = i; + if !keep && i < guard.rebuild_from { + guard.rebuild_from = i; } i += 1; keep }); - - impl<'a, T: Ord> Drop for RebuildOnDrop<'a, T> { - fn drop(&mut self) { - // data[..first_removed] is untouched, so we only need to - // rebuild the tail: - self.heap.rebuild_tail(self.first_removed); - } - } } } From cd3ee1904ed4dbe0d91be143bd5d7763dfc056ad Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Sat, 25 Feb 2023 17:03:03 +0900 Subject: [PATCH 2/2] binary_heap: Unify Extend implementation. Previously the bulk rebuild specialization was only available with Vec, and for general iterators Extend only provided pre-allocation through reserve(). By using a drop guard, we can safely bulk rebuild even if the iterator may panic. This allows benefiting from the bulk rebuild optimization without collecting iterator elements into a Vec beforehand, which would nullify any performance gains from bulk rebuild. --- collections/binary_heap/mod.rs | 36 ++-------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/collections/binary_heap/mod.rs b/collections/binary_heap/mod.rs index b6cb791..7c8cdf6 100644 --- a/collections/binary_heap/mod.rs +++ b/collections/binary_heap/mod.rs @@ -154,8 +154,6 @@ use crate::collections::TryReserveError; use crate::slice; use crate::vec::{self, AsVecIntoIter, Vec}; -use super::SpecExtend; - #[cfg(test)] mod tests; @@ -1715,7 +1713,8 @@ impl<'a, T> IntoIterator for &'a BinaryHeap { impl Extend for BinaryHeap { #[inline] fn extend>(&mut self, iter: I) { - >::spec_extend(self, iter); + let guard = RebuildOnDrop { rebuild_from: self.len(), heap: self }; + guard.heap.data.extend(iter); } #[inline] @@ -1729,37 +1728,6 @@ impl Extend for BinaryHeap { } } -impl> SpecExtend for BinaryHeap { - default fn spec_extend(&mut self, iter: I) { - self.extend_desugared(iter.into_iter()); - } -} - -impl SpecExtend> for BinaryHeap { - fn spec_extend(&mut self, ref mut other: Vec) { - let start = self.data.len(); - self.data.append(other); - self.rebuild_tail(start); - } -} - -impl SpecExtend> for BinaryHeap { - fn spec_extend(&mut self, ref mut other: BinaryHeap) { - self.append(other); - } -} - -impl BinaryHeap { - fn extend_desugared>(&mut self, iter: I) { - let iterator = iter.into_iter(); - let (lower, _) = iterator.size_hint(); - - self.reserve(lower); - - iterator.for_each(move |elem| self.push(elem)); - } -} - #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap { fn extend>(&mut self, iter: I) {