Skip to content

Commit f8c422f

Browse files
committed
Iterate in reverse when comparing Stacks
1 parent ba15da4 commit f8c422f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/stacked_borrows.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl fmt::Debug for Item {
7979
}
8080

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

91+
/// This implementation is primarily used when attempting to merge borrow stacks for adjacent
92+
/// bytes. For adjacent stacks, when the stacks differ at all, they tend to differ either in
93+
/// length or at the end of the borrows array. Iterating in reverse returns faster for `Stack`s
94+
/// that are not equal.
95+
impl PartialEq for Stack {
96+
fn eq(&self, other: &Self) -> bool {
97+
let Stack { borrows: lhs } = self;
98+
let Stack { borrows: rhs } = other;
99+
lhs.len() == rhs.len() && lhs.iter().rev().zip(rhs.iter().rev()).all(|(l, r)| l == r)
100+
}
101+
}
102+
91103
/// Extra per-allocation state.
92104
#[derive(Clone, Debug)]
93105
pub struct Stacks {

0 commit comments

Comments
 (0)