implements PR-007 to PR011

This commit is contained in:
2026-03-24 13:40:46 +00:00
parent c0ed9ba8e0
commit a75df486ce
19 changed files with 978 additions and 117 deletions
+80 -16
View File
@@ -47,11 +47,25 @@ pub struct CoroutineData {
pub struct Heap {
// Tombstone-aware store: Some(obj) = live allocation; None = freed slot.
objects: Vec<Option<StoredObject>>,
// Reclaimed slots available for deterministic reuse (LIFO).
free_list: Vec<usize>,
}
impl Heap {
pub fn new() -> Self {
Self { objects: Vec::new() }
Self { objects: Vec::new(), free_list: Vec::new() }
}
fn insert_object(&mut self, obj: StoredObject) -> HeapRef {
if let Some(idx) = self.free_list.pop() {
debug_assert!(self.objects.get(idx).is_some_and(|slot| slot.is_none()));
self.objects[idx] = Some(obj);
HeapRef(idx as u32)
} else {
let idx = self.objects.len();
self.objects.push(Some(obj));
HeapRef(idx as u32)
}
}
/// Allocate a new object with the given kind and raw payload bytes.
@@ -66,10 +80,7 @@ impl Heap {
closure_env: None,
coroutine: None,
};
let idx = self.objects.len();
// No free-list reuse in this PR: append and keep indices stable.
self.objects.push(Some(obj));
HeapRef(idx as u32)
self.insert_object(obj)
}
/// Allocate a new `Array` object with the given `Value` elements.
@@ -84,10 +95,7 @@ impl Heap {
closure_env: None,
coroutine: None,
};
let idx = self.objects.len();
// No free-list reuse in this PR: append and keep indices stable.
self.objects.push(Some(obj));
HeapRef(idx as u32)
self.insert_object(obj)
}
/// Allocate a new `Closure` object with the given function id and captured environment.
@@ -108,9 +116,7 @@ impl Heap {
closure_env: Some(env_values.to_vec()),
coroutine: None,
};
let idx = self.objects.len();
self.objects.push(Some(obj));
HeapRef(idx as u32)
self.insert_object(obj)
}
/// Allocate a new `Coroutine` object with provided initial data.
@@ -131,9 +137,7 @@ impl Heap {
closure_env: None,
coroutine: Some(CoroutineData { pc, state, wake_tick, stack, frames }),
};
let idx = self.objects.len();
self.objects.push(Some(obj));
HeapRef(idx as u32)
self.insert_object(obj)
}
/// Returns true if this handle refers to an allocated object.
@@ -352,7 +356,7 @@ impl Heap {
/// tombstones (None), and clear the mark bit on the remaining live ones
/// to prepare for the next GC cycle. Does not move or compact objects.
pub fn sweep(&mut self) {
for slot in self.objects.iter_mut() {
for (idx, slot) in self.objects.iter_mut().enumerate() {
if let Some(obj) = slot {
if obj.header.is_marked() {
// Live: clear mark for next cycle.
@@ -360,6 +364,7 @@ impl Heap {
} else {
// Unreachable: reclaim by dropping and turning into tombstone.
*slot = None;
self.free_list.push(idx);
}
}
}
@@ -603,6 +608,65 @@ mod tests {
assert_eq!(a.0, a.0); // placeholder sanity check
}
#[test]
fn sweep_reuses_freed_slot_on_next_allocation() {
let mut heap = Heap::new();
let dead = heap.allocate_object(ObjectKind::String, b"dead");
let live = heap.allocate_object(ObjectKind::String, b"live");
heap.mark_from_roots([live]);
heap.sweep();
assert!(!heap.is_valid(dead));
assert_eq!(heap.free_list, vec![dead.0 as usize]);
let reused = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]);
assert_eq!(reused, dead);
assert!(heap.is_valid(reused));
assert!(heap.is_valid(live));
}
#[test]
fn live_handles_remain_stable_when_freelist_is_reused() {
let mut heap = Heap::new();
let live = heap.allocate_object(ObjectKind::String, b"live");
let dead = heap.allocate_object(ObjectKind::String, b"dead");
heap.mark_from_roots([live]);
heap.sweep();
let replacement = heap.allocate_object(ObjectKind::Bytes, &[9]);
assert_eq!(replacement, dead);
assert_eq!(heap.header(live).unwrap().kind, ObjectKind::String);
assert_eq!(heap.header(replacement).unwrap().kind, ObjectKind::Bytes);
assert_eq!(live.0, 0);
}
#[test]
fn freelist_reuse_is_deterministic_lifo() {
let mut heap = Heap::new();
let a = heap.allocate_object(ObjectKind::String, b"a");
let b = heap.allocate_object(ObjectKind::String, b"b");
let c = heap.allocate_object(ObjectKind::String, b"c");
heap.mark_from_roots([]);
heap.sweep();
assert_eq!(heap.free_list, vec![a.0 as usize, b.0 as usize, c.0 as usize]);
let r1 = heap.allocate_object(ObjectKind::Bytes, &[1]);
let r2 = heap.allocate_object(ObjectKind::Bytes, &[2]);
let r3 = heap.allocate_object(ObjectKind::Bytes, &[3]);
assert_eq!(r1, c);
assert_eq!(r2, b);
assert_eq!(r3, a);
}
#[test]
fn sweep_reclaims_unrooted_cycle() {
let mut heap = Heap::new();