Skip to content

Commit c4e8e1f

Browse files
committed
fix(runtime): keep comparison continuations bounded
1 parent 1de0f61 commit c4e8e1f

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

‎crates/nera-runtime/src/ffi/compare.rs‎

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,64 @@ pub(crate) unsafe fn map_iteration_order(ptr: *const u64, npairs: usize) -> Vec<
176176
order
177177
}
178178

179+
enum CompareWork {
180+
Terms(ErlTerm, ErlTerm, bool),
181+
TupleFields { a: ErlTerm, b: ErlTerm, next: usize, exact: bool },
182+
MapFields(Box<MapComparison>),
183+
}
184+
185+
struct MapComparison {
186+
a: Vec<(ErlTerm, ErlTerm)>,
187+
b: Vec<(ErlTerm, ErlTerm)>,
188+
next: usize,
189+
exact: bool,
190+
}
191+
179192
/// Map keys require exact ordering throughout their graph, including map values
180193
/// nested inside a key. Ordinary map values retain the caller's comparison mode.
181194
/// The term lifetime contract is the same as compare_terms's.
182195
unsafe fn compare_terms_mode(a: ErlTerm, b: ErlTerm, exact: bool) -> i64 {
183196
let mut pending = Vec::new();
184-
let mut current = (a, b, exact);
197+
let mut current = CompareWork::Terms(a, b, exact);
185198
loop {
186-
let (a, b, exact) = current;
199+
let (a, b, exact) = match current {
200+
CompareWork::Terms(a, b, exact) => (a, b, exact),
201+
CompareWork::TupleFields { a, b, next, exact } => {
202+
let pa = a.as_boxed_ptr().unwrap();
203+
let pb = b.as_boxed_ptr().unwrap();
204+
if next < BoxedHeader(*pa).arity() as usize {
205+
pending.push(CompareWork::TupleFields { a, b, next: next + 1, exact });
206+
}
207+
(ErlTerm(*pa.add(next)), ErlTerm(*pb.add(next)), exact)
208+
}
209+
CompareWork::MapFields(mut fields) => {
210+
let n = fields.a.len();
211+
let index = fields.next % n;
212+
let keys = fields.next < n;
213+
let (a, b, exact) = if keys {
214+
(fields.a[index].0, fields.b[index].0, true)
215+
} else {
216+
(fields.a[index].1, fields.b[index].1, fields.exact)
217+
};
218+
fields.next += 1;
219+
if fields.next < 2 * n {
220+
pending.push(CompareWork::MapFields(fields));
221+
}
222+
(a, b, exact)
223+
}
224+
};
187225
let result = compare_terms_step(a, b, exact, &mut pending);
188226
if result != 0 { return result; }
189227
let Some(next) = pending.pop() else { return 0; };
190228
current = next;
191229
}
192230
}
193231

194-
// Equal structural prefixes schedule their remaining comparisons in reverse
195-
// order. Nesting consumes heap storage, not native call frames.
232+
// Equal structural prefixes save one indexed continuation per container,
233+
// not every remaining field. Wide tuples still stop at the first mismatch.
196234
unsafe fn compare_terms_step(
197235
a: ErlTerm, b: ErlTerm, exact: bool,
198-
pending: &mut Vec<(ErlTerm, ErlTerm, bool)>,
236+
pending: &mut Vec<CompareWork>,
199237
) -> i64 {
200238
use crate::term::*;
201239

@@ -230,8 +268,8 @@ unsafe fn compare_terms_step(
230268
// Cons cells: compare heads before tails
231269
if let (Some(pa), Some(pb)) = (a.as_cons_ptr(), b.as_cons_ptr()) {
232270
unsafe {
233-
pending.push(((*pa).tail, (*pb).tail, exact));
234-
pending.push(((*pa).head, (*pb).head, exact));
271+
pending.push(CompareWork::Terms((*pa).tail, (*pb).tail, exact));
272+
pending.push(CompareWork::Terms((*pa).head, (*pb).head, exact));
235273
return 0;
236274
}
237275
}
@@ -246,8 +284,11 @@ unsafe fn compare_terms_step(
246284
let aa = ha.arity();
247285
let ab = hb.arity();
248286
if aa != ab { return if aa < ab { -1 } else { 1 }; }
249-
for i in (1..=aa as usize).rev() {
250-
pending.push((ErlTerm(*pa.add(i)), ErlTerm(*pb.add(i)), exact));
287+
if aa > 0 {
288+
if aa > 1 {
289+
pending.push(CompareWork::TupleFields { a, b, next: 2, exact });
290+
}
291+
pending.push(CompareWork::Terms(ErlTerm(*pa.add(1)), ErlTerm(*pb.add(1)), exact));
251292
}
252293
return 0;
253294
}
@@ -279,11 +320,10 @@ unsafe fn compare_terms_step(
279320
r if r > 0 => std::cmp::Ordering::Greater,
280321
_ => std::cmp::Ordering::Equal,
281322
});
282-
for i in (0..na).rev() {
283-
pending.push((ka[i].1, kb[i].1, exact));
284-
}
285-
for i in (0..na).rev() {
286-
pending.push((ka[i].0, kb[i].0, true));
323+
if na > 0 {
324+
pending.push(CompareWork::MapFields(Box::new(MapComparison {
325+
a: ka, b: kb, next: 0, exact,
326+
})));
287327
}
288328
return 0;
289329
}

‎crates/nera-runtime/src/phash2.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub(crate) unsafe fn hash(term: ErlTerm) -> u32 {
203203
let full = (bits / 8) as u32 as usize;
204204
let tail = bits % 8;
205205
let initial = hc(13).wrapping_add(hash);
206-
hash = if bits == 0 {
206+
hash = if full == 0 && tail == 0 {
207207
initial
208208
} else {
209209
block_hash(&bytes[..full], initial)

0 commit comments

Comments
 (0)