diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 0d671ec653..0a94308171 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -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: @@ -88,6 +88,18 @@ pub struct Stack { borrows: Vec, } +/// 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 {