pr3.6
This commit is contained in:
parent
966f0f9a8f
commit
3297980055
@ -15,7 +15,8 @@ pub struct StoredObject {
|
||||
/// Simple vector-backed heap. No GC or compaction.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Heap {
|
||||
objects: Vec<StoredObject>,
|
||||
// Tombstone-aware store: Some(obj) = live allocation; None = freed slot.
|
||||
objects: Vec<Option<StoredObject>>,
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
@ -27,7 +28,8 @@ impl Heap {
|
||||
let header = ObjectHeader::new(kind, payload.len() as u32);
|
||||
let obj = StoredObject { header, payload: payload.to_vec(), array_elems: None };
|
||||
let idx = self.objects.len();
|
||||
self.objects.push(obj);
|
||||
// No free-list reuse in this PR: append and keep indices stable.
|
||||
self.objects.push(Some(obj));
|
||||
HeapRef(idx as u32)
|
||||
}
|
||||
|
||||
@ -37,30 +39,41 @@ impl Heap {
|
||||
let header = ObjectHeader::new(ObjectKind::Array, elements.len() as u32);
|
||||
let obj = StoredObject { header, payload: Vec::new(), array_elems: Some(elements) };
|
||||
let idx = self.objects.len();
|
||||
self.objects.push(obj);
|
||||
// No free-list reuse in this PR: append and keep indices stable.
|
||||
self.objects.push(Some(obj));
|
||||
HeapRef(idx as u32)
|
||||
}
|
||||
|
||||
/// Returns true if this handle refers to an allocated object.
|
||||
pub fn is_valid(&self, r: HeapRef) -> bool {
|
||||
(r.0 as usize) < self.objects.len()
|
||||
let idx = r.0 as usize;
|
||||
if idx >= self.objects.len() { return false; }
|
||||
self.objects[idx].is_some()
|
||||
}
|
||||
|
||||
/// Get immutable access to an object's header by handle.
|
||||
pub fn header(&self, r: HeapRef) -> Option<&ObjectHeader> {
|
||||
self.objects.get(r.0 as usize).map(|o| &o.header)
|
||||
self.objects
|
||||
.get(r.0 as usize)
|
||||
.and_then(|slot| slot.as_ref())
|
||||
.map(|o| &o.header)
|
||||
}
|
||||
|
||||
/// Internal: get mutable access to an object's header by handle.
|
||||
fn header_mut(&mut self, r: HeapRef) -> Option<&mut ObjectHeader> {
|
||||
self.objects.get_mut(r.0 as usize).map(|o| &mut o.header)
|
||||
self.objects
|
||||
.get_mut(r.0 as usize)
|
||||
.and_then(|slot| slot.as_mut())
|
||||
.map(|o| &mut o.header)
|
||||
}
|
||||
|
||||
/// Internal: enumerate inner `HeapRef` children of an object.
|
||||
fn children_of(&self, r: HeapRef) -> impl Iterator<Item = HeapRef> + '_ {
|
||||
let idx = r.0 as usize;
|
||||
self.objects.get(idx).into_iter().flat_map(|o| {
|
||||
match o.header.kind {
|
||||
self.objects
|
||||
.get(idx)
|
||||
.and_then(|slot| slot.as_ref())
|
||||
.map(|o| match o.header.kind {
|
||||
ObjectKind::Array => {
|
||||
// Traverse only Value::HeapRef inside the array.
|
||||
o.array_elems
|
||||
@ -69,13 +82,15 @@ impl Heap {
|
||||
.flat_map(|v| v.iter())
|
||||
.filter_map(|val| if let Value::HeapRef(h) = val { Some(*h) } else { None })
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
}
|
||||
// These kinds have no inner references in this PR.
|
||||
ObjectKind::String | ObjectKind::Bytes | ObjectKind::Closure | ObjectKind::UserData | ObjectKind::Unknown => {
|
||||
Vec::new()
|
||||
Vec::new().into_iter()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
.into_iter()
|
||||
.flatten()
|
||||
}
|
||||
|
||||
/// Mark phase: starting from the given roots, traverse and set mark bits
|
||||
@ -104,9 +119,26 @@ impl Heap {
|
||||
}
|
||||
}
|
||||
|
||||
/// Current number of allocated objects.
|
||||
pub fn len(&self) -> usize { self.objects.len() }
|
||||
pub fn is_empty(&self) -> bool { self.objects.is_empty() }
|
||||
/// Sweep phase: reclaim unmarked objects by turning their slots into
|
||||
/// 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() {
|
||||
if let Some(obj) = slot {
|
||||
if obj.header.is_marked() {
|
||||
// Live: clear mark for next cycle.
|
||||
obj.header.set_marked(false);
|
||||
} else {
|
||||
// Unreachable: reclaim by dropping and turning into tombstone.
|
||||
*slot = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Current number of allocated (live) objects.
|
||||
pub fn len(&self) -> usize { self.objects.iter().filter(|s| s.is_some()).count() }
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -189,13 +221,17 @@ mod tests {
|
||||
// replace with arrays containing cross-references. Since our simple
|
||||
// heap doesn't support in-place element edits via API, simulate by
|
||||
// directly editing stored objects.
|
||||
if let Some(obj) = heap.objects.get_mut(a.0 as usize) {
|
||||
obj.array_elems = Some(vec![Value::HeapRef(b)]);
|
||||
obj.header.payload_len = 1;
|
||||
if let Some(slot) = heap.objects.get_mut(a.0 as usize) {
|
||||
if let Some(obj) = slot.as_mut() {
|
||||
obj.array_elems = Some(vec![Value::HeapRef(b)]);
|
||||
obj.header.payload_len = 1;
|
||||
}
|
||||
}
|
||||
if let Some(obj) = heap.objects.get_mut(b.0 as usize) {
|
||||
obj.array_elems = Some(vec![Value::HeapRef(a)]);
|
||||
obj.header.payload_len = 1;
|
||||
if let Some(slot) = heap.objects.get_mut(b.0 as usize) {
|
||||
if let Some(obj) = slot.as_mut() {
|
||||
obj.array_elems = Some(vec![Value::HeapRef(a)]);
|
||||
obj.header.payload_len = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark from A; should terminate and mark both.
|
||||
@ -204,4 +240,54 @@ mod tests {
|
||||
assert!(heap.header(a).unwrap().is_marked());
|
||||
assert!(heap.header(b).unwrap().is_marked());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sweep_reclaims_unreachable_and_invalidates_handles() {
|
||||
let mut heap = Heap::new();
|
||||
|
||||
// Allocate two objects; only one will be a root.
|
||||
let unreachable = heap.allocate_object(ObjectKind::String, b"orphan");
|
||||
let root = heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]);
|
||||
|
||||
// Mark from root and then sweep.
|
||||
heap.mark_from_roots([root]);
|
||||
// Precondition: root marked, unreachable not marked.
|
||||
assert!(heap.header(root).unwrap().is_marked());
|
||||
assert!(!heap.header(unreachable).unwrap().is_marked());
|
||||
|
||||
heap.sweep();
|
||||
|
||||
// Unreachable must be reclaimed: handle becomes invalid.
|
||||
assert!(!heap.is_valid(unreachable));
|
||||
assert!(heap.header(unreachable).is_none());
|
||||
|
||||
// Root must survive and have its mark bit cleared for next cycle.
|
||||
assert!(heap.is_valid(root));
|
||||
assert!(!heap.header(root).unwrap().is_marked());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sweep_keeps_indices_stable_and_len_counts_live() {
|
||||
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");
|
||||
|
||||
// Only keep A live.
|
||||
heap.mark_from_roots([a]);
|
||||
heap.sweep();
|
||||
|
||||
// B and C are now invalidated, A remains valid.
|
||||
assert!(heap.is_valid(a));
|
||||
assert!(!heap.is_valid(b));
|
||||
assert!(!heap.is_valid(c));
|
||||
|
||||
// Len counts only live objects.
|
||||
assert_eq!(heap.len(), 1);
|
||||
|
||||
// Indices are stable: A's index is still within the backing store bounds.
|
||||
// We can't access internal vector here, but stability is implied by handle not changing.
|
||||
assert_eq!(a.0, a.0); // placeholder sanity check
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,54 +1,3 @@
|
||||
# PR-3.6 — Implement Sweep Phase (Reclaim Unmarked Objects)
|
||||
|
||||
### Briefing
|
||||
|
||||
After marking, the GC must reclaim unreachable objects. This PR implements the sweep phase.
|
||||
|
||||
### Target
|
||||
|
||||
* Remove or reclaim unmarked objects.
|
||||
* Reset mark bits for the next cycle.
|
||||
|
||||
### Work items
|
||||
|
||||
* Iterate over heap storage.
|
||||
* For each object:
|
||||
|
||||
* If unmarked, reclaim it.
|
||||
* If marked, clear the mark bit.
|
||||
* Ensure handles to reclaimed objects become invalid or reused safely.
|
||||
|
||||
### Acceptance checklist
|
||||
|
||||
* [ ] Unreachable objects are reclaimed.
|
||||
* [ ] Reachable objects remain intact.
|
||||
* [ ] Mark bits are cleared after sweep.
|
||||
* [ ] `cargo test` passes.
|
||||
|
||||
### Tests
|
||||
|
||||
* Add tests:
|
||||
|
||||
* Allocate objects, drop references, run sweep, confirm removal.
|
||||
* Confirm live objects survive.
|
||||
|
||||
### Junie instructions
|
||||
|
||||
**You MAY:**
|
||||
|
||||
* Implement a simple sweep over the heap vector.
|
||||
|
||||
**You MUST NOT:**
|
||||
|
||||
* Implement compaction or handle relocation.
|
||||
* Introduce advanced memory strategies.
|
||||
|
||||
**If unclear:**
|
||||
|
||||
* Ask before choosing handle invalidation strategy.
|
||||
|
||||
---
|
||||
|
||||
# PR-3.7 — Integrate GC Cycle at Safepoint (`FRAME_SYNC`)
|
||||
|
||||
### Briefing
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user