Skip to content

Commit 5afabb3

Browse files
Use handle equality
1 parent 78098ae commit 5afabb3

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

stack-graphs/src/arena.rs

+20
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ pub trait Arena<T> {
204204

205205
/// Returns the number of instances stored in this arena.
206206
fn len(&self) -> usize;
207+
208+
/// Returns whether the values associated with the arena are equal, if this can be determined from the
209+
/// handles alone. Otherwise, no value is returned.
210+
fn try_equals(&self, lhs: Handle<T>, rhs: Handle<T>) -> Option<bool>;
207211
}
208212

209213
/// Manages the life cycle of instances of type `T`. You can allocate new instances of `T` from
@@ -261,6 +265,11 @@ impl<T> Arena<T> for VecArena<T> {
261265
fn len(&self) -> usize {
262266
self.items.len()
263267
}
268+
269+
#[inline]
270+
fn try_equals(&self, _lhs: Handle<T>, _rhs: Handle<T>) -> Option<bool> {
271+
None
272+
}
264273
}
265274

266275
/// An interning arena allocation. Equal handles means equal elements.
@@ -320,6 +329,11 @@ where
320329
fn len(&self) -> usize {
321330
self.arena.len()
322331
}
332+
333+
#[inline]
334+
fn try_equals(&self, lhs: Handle<T>, rhs: Handle<T>) -> Option<bool> {
335+
Some(lhs == rhs)
336+
}
323337
}
324338

325339
//-------------------------------------------------------------------------------------------------
@@ -672,6 +686,9 @@ where
672686
T: Eq,
673687
{
674688
pub fn equals(self, arena: &impl ListArena<T>, other: List<T>) -> bool {
689+
if let Some(equals) = arena.try_equals(self.cells, other.cells) {
690+
return equals;
691+
}
675692
self.equals_with(arena, other, |a, b| *a == *b)
676693
}
677694
}
@@ -987,6 +1004,9 @@ where
9871004
T: Eq,
9881005
{
9891006
pub fn equals(self, arena: &impl ReversibleListArena<T>, other: ReversibleList<T>) -> bool {
1007+
if let Some(equals) = arena.try_equals(self.cells, other.cells) {
1008+
return equals;
1009+
}
9901010
self.equals_with(arena, other, |a, b| *a == *b)
9911011
}
9921012
}

stack-graphs/src/partial.rs

+15-37
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl DisplayWithPartialPaths for PartialScopedSymbol {
493493
/// A pattern that might match against a symbol stack. Consists of a (possibly empty) list of
494494
/// partial scoped symbols, along with an optional symbol stack variable.
495495
#[repr(C)]
496-
#[derive(Clone, Copy)]
496+
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
497497
pub struct PartialSymbolStack {
498498
symbols: ReversibleList<PartialScopedSymbol>,
499499
length: u32,
@@ -793,24 +793,13 @@ impl PartialSymbolStack {
793793
unreachable!();
794794
}
795795

796-
pub fn equals(mut self, partials: &mut PartialPaths, mut other: PartialSymbolStack) -> bool {
797-
while let Some(self_symbol) = self.pop_front(partials) {
798-
if let Some(other_symbol) = other.pop_front(partials) {
799-
if !self_symbol.equals(partials, &other_symbol) {
800-
return false;
801-
}
802-
} else {
803-
return false;
804-
}
805-
}
806-
if !other.symbols.is_empty() {
807-
return false;
808-
}
809-
equals_option(
810-
self.variable.into_option(),
811-
other.variable.into_option(),
812-
|a, b| a == b,
813-
)
796+
pub fn equals(self, _partials: &mut PartialPaths, other: PartialSymbolStack) -> bool {
797+
self.symbols == other.symbols
798+
&& equals_option(
799+
self.variable.into_option(),
800+
other.variable.into_option(),
801+
|a, b| a == b,
802+
)
814803
}
815804

816805
pub fn cmp(
@@ -1206,11 +1195,8 @@ impl PartialScopeStack {
12061195
self.variable.into_option()
12071196
}
12081197

1209-
pub fn equals(self, partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
1210-
self.scopes
1211-
.equals_with(&mut partials.partial_scope_stacks, other.scopes, |a, b| {
1212-
*a == *b
1213-
})
1198+
pub fn equals(self, _partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
1199+
self.scopes == other.scopes
12141200
&& equals_option(
12151201
self.variable.into_option(),
12161202
other.variable.into_option(),
@@ -1819,21 +1805,13 @@ impl PartialPath {
18191805
self.edges.shadows(partials, other.edges)
18201806
}
18211807

1822-
pub fn equals(&self, partials: &mut PartialPaths, other: &PartialPath) -> bool {
1808+
pub fn equals(&self, _partials: &mut PartialPaths, other: &PartialPath) -> bool {
18231809
self.start_node == other.start_node
18241810
&& self.end_node == other.end_node
1825-
&& self
1826-
.symbol_stack_precondition
1827-
.equals(partials, other.symbol_stack_precondition)
1828-
&& self
1829-
.symbol_stack_postcondition
1830-
.equals(partials, other.symbol_stack_postcondition)
1831-
&& self
1832-
.scope_stack_precondition
1833-
.equals(partials, other.scope_stack_precondition)
1834-
&& self
1835-
.scope_stack_postcondition
1836-
.equals(partials, other.scope_stack_postcondition)
1811+
&& self.symbol_stack_precondition == other.symbol_stack_precondition
1812+
&& self.symbol_stack_postcondition == other.symbol_stack_postcondition
1813+
&& self.scope_stack_precondition == other.scope_stack_precondition
1814+
&& self.scope_stack_postcondition == other.scope_stack_postcondition
18371815
}
18381816

18391817
pub fn cmp(

0 commit comments

Comments
 (0)