Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate in reverse when comparing Stacks #2214

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/stacked_borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl fmt::Debug for Item {
}

/// Extra per-location state.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, Eq)]
pub struct Stack {
/// Used *mostly* as a stack; never empty.
/// Invariants:
Expand All @@ -88,6 +88,18 @@ pub struct Stack {
borrows: Vec<Item>,
}

/// This implementation is primarily used when attempting to merge borrow stacks for adjacent
/// bytes. For adjacent stacks, when the stacks differ at all, they tend to differ either in
/// length or at the end of the borrows array. Iterating in reverse returns faster for `Stack`s
/// that are not equal.
impl PartialEq for Stack {
fn eq(&self, other: &Self) -> bool {
let Stack { borrows: lhs } = self;
let Stack { borrows: rhs } = other;
lhs.len() == rhs.len() && lhs.iter().rev().zip(rhs.iter().rev()).all(|(l, r)| l == r)
}
}

/// Extra per-allocation state.
#[derive(Clone, Debug)]
pub struct Stacks {
Expand Down