Rollup merge of #131197 - EFanZh:avoid-emptyness-check-in-peekmut-pop, r=Amanieu

Avoid emptiness check in `PeekMut::pop`

This PR avoids an unnecessary emptiness check in `PeekMut::pop` by replacing `Option::unwrap` with `Option::unwrap_unchecked`.
This commit is contained in:
Matthias Krüger 2024-10-03 21:52:47 +02:00 committed by GitHub
commit 29580e12f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -374,7 +374,10 @@ pub fn pop(mut this: PeekMut<'a, T, A>) -> T {
// the caller could've mutated the element. It is removed from the
// heap on the next line and pop() is not sensitive to its value.
}
this.heap.pop().unwrap()
// SAFETY: Have a `PeekMut` element proves that the associated binary heap being non-empty,
// so the `pop` operation will not fail.
unsafe { this.heap.pop().unwrap_unchecked() }
}
}

View File

@ -0,0 +1,13 @@
//@ compile-flags: -O
//@ ignore-debug
#![crate_type = "lib"]
use std::collections::binary_heap::PeekMut;
// CHECK-LABEL: @peek_mut_pop
#[no_mangle]
pub fn peek_mut_pop(peek_mut: PeekMut<u32>) -> u32 {
// CHECK-NOT: panic
// CHECK-NOT: unwrap_failed
PeekMut::pop(peek_mut)
}