replace eager_stackful_iter's mark stack with a second unmark phase

This commit is contained in:
Mark
2023-10-05 20:21:28 -06:00
parent 0ad4427f83
commit fa68fa211c

View File

@@ -28,64 +28,62 @@ pub fn eager_stackful_preorder_iter(
*/ */
pub struct EagerStackfulPreOrderHeapIter<'a> { pub struct EagerStackfulPreOrderHeapIter<'a> {
start_value: HeapCellValue,
iter_stack: Vec<HeapCellValue>, iter_stack: Vec<HeapCellValue>,
mark_stack: Vec<usize>, mark_phase: bool,
heap: &'a mut Heap, heap: &'a mut Heap,
} }
impl<'a> Drop for EagerStackfulPreOrderHeapIter<'a> { impl<'a> Drop for EagerStackfulPreOrderHeapIter<'a> {
fn drop(&mut self) { fn drop(&mut self) {
while let Some(h) = self.mark_stack.pop() { self.mark_phase = false;
self.heap[h].set_mark_bit(false);
} self.iter_stack.clear();
self.start_value.set_mark_bit(true);
self.iter_stack.push(self.start_value);
while let Some(_) = self.follow() {}
} }
} }
impl<'a> EagerStackfulPreOrderHeapIter<'a> { impl<'a> EagerStackfulPreOrderHeapIter<'a> {
pub fn new(heap: &'a mut Heap, value: HeapCellValue) -> Self { pub fn new(heap: &'a mut Heap, value: HeapCellValue) -> Self {
Self { Self {
start_value: value,
iter_stack: vec![value], iter_stack: vec![value],
mark_stack: vec![], mark_phase: true,
heap, heap,
} }
} }
fn follow(&mut self) -> Option<HeapCellValue> { fn follow(&mut self) -> Option<HeapCellValue> {
while let Some(value) = self.iter_stack.pop() { while let Some(value) = self.iter_stack.pop() {
if value.get_mark_bit() { if value.get_mark_bit() == self.mark_phase {
continue; continue;
} }
read_heap_cell!(value, read_heap_cell!(value,
(HeapCellValueTag::Str, s) => { (HeapCellValueTag::Str, s) => {
if self.heap[s].get_mark_bit() {
continue;
}
let arity = cell_as_atom_cell!(self.heap[s]).get_arity(); let arity = cell_as_atom_cell!(self.heap[s]).get_arity();
self.heap[s].set_mark_bit(true);
self.mark_stack.push(s);
for idx in (s + 1 .. s + arity + 1).rev() { for idx in (s + 1 .. s + arity + 1).rev() {
self.iter_stack.push(self.heap[idx]); self.iter_stack.push(self.heap[idx]);
self.heap[idx].set_mark_bit(self.mark_phase);
} }
} }
(HeapCellValueTag::Lis, l) => { (HeapCellValueTag::Lis, l) => {
self.iter_stack.push(self.heap[l+1]); self.iter_stack.push(self.heap[l+1]);
self.iter_stack.push(self.heap[l]); self.iter_stack.push(self.heap[l]);
self.heap[l].set_mark_bit(true); self.heap[l].set_mark_bit(self.mark_phase);
self.mark_stack.push(l); self.heap[l+1].set_mark_bit(self.mark_phase);
self.heap[l+1].set_mark_bit(true);
self.mark_stack.push(l+1);
} }
(HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => { (HeapCellValueTag::AttrVar | HeapCellValueTag::Var, h) => {
let var_value = self.heap[h]; let var_value = self.heap[h];
if !(var_value.is_var() && var_value.get_value() as usize == h) { if !(var_value.is_var() && var_value.get_value() as usize == h) {
self.iter_stack.push(self.heap[h]); self.heap[h].set_mark_bit(self.mark_phase);
self.iter_stack.push(var_value);
continue; continue;
} }
} }
@@ -97,14 +95,12 @@ impl<'a> EagerStackfulPreOrderHeapIter<'a> {
self.heap[h].get_value() as usize self.heap[h].get_value() as usize
}; };
if self.heap[h].get_mark_bit() { if self.heap[h].get_mark_bit() == self.mark_phase {
continue; continue;
} }
self.heap[h].set_mark_bit(true); self.heap[h].set_mark_bit(self.mark_phase);
self.iter_stack.push(self.heap[h+1]); self.iter_stack.push(self.heap[h+1]);
self.mark_stack.push(h);
} }
_ => { _ => {
} }