Skip to content

Commit

Permalink
perf(data_structures): optimize NonEmptyStack::pop (#7021)
Browse files Browse the repository at this point in the history
Shave a few bytes off `NonEmptyStack::pop`.
  • Loading branch information
overlookmotel committed Oct 30, 2024
1 parent b8daab3 commit c58ec89
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/oxc_data_structures/src/stack/non_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,22 @@ impl<T> NonEmptyStack<T> {
/// Panics if the stack has only 1 entry on it.
#[inline]
pub fn pop(&mut self) -> T {
// Panic if trying to remove last entry from stack
assert!(self.cursor != self.start, "Cannot pop all entries");
// Panic if trying to remove last entry from stack.
//
// Putting the panic in an `#[inline(never)]` + `#[cold]` function removes a 6-byte `lea`
// instruction vs `assert!(self.cursor != self.start, "Cannot pop all entries")`.
// This reduces this function on x86_64 from 32 bytes to 26 bytes.
// This function is commonly used, and we want it to be inlined, so every byte counts.
// https://godbolt.org/z/5587z99rM
#[inline(never)]
#[cold]
fn error() -> ! {
panic!("Cannot pop all entries");
}

if self.cursor == self.start {
error();
}

// SAFETY: Assertion above ensures stack has at least 2 entries
unsafe { self.pop_unchecked() }
Expand Down

0 comments on commit c58ec89

Please sign in to comment.