1
Fork 0

Auto merge of #75974 - SkiFire13:peekmut-opt-sift, r=LukasKalbertodt

Avoid useless sift_down when std::collections::binary_heap::PeekMut is never mutably dereferenced

If `deref_mut` is never called then it's not possible for the element to be mutated without internal mutability, meaning there's no need to call `sift_down`.

This could be a little improvement in cases where you want to mutate the biggest element of the heap only if it satisfies a certain predicate that needs only read access to the element.
This commit is contained in:
bors 2020-09-21 05:31:01 +00:00
commit a409a233e0
3 changed files with 96 additions and 2 deletions

View file

@ -291,6 +291,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
impl<T: Ord> DerefMut for PeekMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
debug_assert!(!self.heap.is_empty());
self.sift = true;
// SAFE: PeekMut is only instantiated for non-empty heaps
unsafe { self.heap.data.get_unchecked_mut(0) }
}
@ -396,10 +397,11 @@ impl<T: Ord> BinaryHeap<T> {
///
/// # Time complexity
///
/// Cost is *O*(1) in the worst case.
/// If the item is modified then the worst case time complexity is *O*(log(*n*)),
/// otherwise it's *O*(1).
#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>> {
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: true }) }
if self.is_empty() { None } else { Some(PeekMut { heap: self, sift: false }) }
}
/// Removes the greatest item from the binary heap and returns it, or `None` if it