Skip to content

Commit 6c22b22

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

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/stacked_borrows.rs

+12-1
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,17 @@ 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+
self.borrows.len() == other.borrows.len()
98+
&& self.borrows.iter().rev().zip(other.borrows.iter().rev()).all(|(s, o)| s == o)
99+
}
100+
}
101+
91102
/// Extra per-allocation state.
92103
#[derive(Clone, Debug)]
93104
pub struct Stacks {

0 commit comments

Comments
 (0)