Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 1caa5ee

Browse files
author
Hendrik van Antwerpen
committed
Use handle equality
1 parent 5f9f4ae commit 1caa5ee

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

stack-graphs/src/arena.rs

Lines changed: 20 additions & 0 deletions
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

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl DisplayWithPartialPaths for PartialScopedSymbol {
491491
/// A pattern that might match against a symbol stack. Consists of a (possibly empty) list of
492492
/// partial scoped symbols, along with an optional symbol stack variable.
493493
#[repr(C)]
494-
#[derive(Clone, Copy)]
494+
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
495495
pub struct PartialSymbolStack {
496496
symbols: ReversibleList<PartialScopedSymbol>,
497497
length: u32,
@@ -791,24 +791,13 @@ impl PartialSymbolStack {
791791
unreachable!();
792792
}
793793

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

814803
pub fn cmp(
@@ -1204,11 +1193,8 @@ impl PartialScopeStack {
12041193
self.variable.into_option()
12051194
}
12061195

1207-
pub fn equals(self, partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
1208-
self.scopes
1209-
.equals_with(&mut partials.partial_scope_stacks, other.scopes, |a, b| {
1210-
*a == *b
1211-
})
1196+
pub fn equals(self, _partials: &mut PartialPaths, other: PartialScopeStack) -> bool {
1197+
self.scopes == other.scopes
12121198
&& equals_option(
12131199
self.variable.into_option(),
12141200
other.variable.into_option(),
@@ -1852,21 +1838,13 @@ impl PartialPath {
18521838
self.edges.shadows(partials, other.edges)
18531839
}
18541840

1855-
pub fn equals(&self, partials: &mut PartialPaths, other: &PartialPath) -> bool {
1841+
pub fn equals(&self, _partials: &mut PartialPaths, other: &PartialPath) -> bool {
18561842
self.start_node == other.start_node
18571843
&& self.end_node == other.end_node
1858-
&& self
1859-
.symbol_stack_precondition
1860-
.equals(partials, other.symbol_stack_precondition)
1861-
&& self
1862-
.symbol_stack_postcondition
1863-
.equals(partials, other.symbol_stack_postcondition)
1864-
&& self
1865-
.scope_stack_precondition
1866-
.equals(partials, other.scope_stack_precondition)
1867-
&& self
1868-
.scope_stack_postcondition
1869-
.equals(partials, other.scope_stack_postcondition)
1844+
&& self.symbol_stack_precondition == other.symbol_stack_precondition
1845+
&& self.symbol_stack_postcondition == other.symbol_stack_postcondition
1846+
&& self.scope_stack_precondition == other.scope_stack_precondition
1847+
&& self.scope_stack_postcondition == other.scope_stack_postcondition
18701848
}
18711849

18721850
pub fn cmp(

0 commit comments

Comments
 (0)