perf(vm): eliminate recurring stack and return allocations

This commit is contained in:
2026-10-14 10:54:11 +01:00
parent e09f1197bc
commit 46bbad19c2
8 changed files with 308 additions and 30 deletions
@@ -331,3 +331,45 @@ fn recursive_fn_calls_use_bounded_ordinary_frames() {
);
assert_eq!(v.call_stack.len(), 4);
}
#[test]
fn root_return_scratch_refusal_keeps_transferred_roots_without_partial_sweep() {
let mut v = vm(&[
("MAKE_CLOSURE fn=1,type=4\nPOP\nMAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
("PUSH_I32 55\nRET", sig(&[], &[0]), vec![]),
]);
v.gc_alloc_threshold = 1;
for _ in 0..3 {
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
}
let Value::HeapRef(owner) = *v.operand_stack.last().unwrap() else { panic!() };
let live = v.heap.len();
v.budget.test_lower_limit(RuntimeResource::TemporaryBytes, 0);
assert!(matches!(v.step(&mut Native, &mut HostContext::new(None)),
Err(LogicalFrameEndingReason::Trap(t)) if t.code == TRAP_RESOURCE_LIMIT));
assert!(v.call_stack.is_empty());
assert_eq!(v.operand_stack.as_slice(), &[Value::HeapRef(owner)]);
assert!(v.heap.is_valid(owner));
assert_eq!(v.heap.len(), live);
v.budget.test_lower_limit(RuntimeResource::TemporaryBytes, 1024 * 1024);
v.handle_safepoint().unwrap();
assert_eq!(v.heap.len(), 1);
assert!(v.heap.is_valid(owner));
}
#[test]
fn bad_return_height_preserves_frame_and_owners() {
let mut v = vm(&[
("MAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
("PUSH_I32 55\nRET", sig(&[], &[0]), vec![]),
]);
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
v.operand_stack.push(Value::Int32(9)).unwrap();
let before = v.operand_stack.as_slice().to_vec();
assert!(matches!(v.step(&mut Native, &mut HostContext::new(None)),
Err(LogicalFrameEndingReason::Trap(t)) if t.code == prometeu_bytecode::TRAP_BAD_RET_SLOTS));
assert_eq!(v.call_stack.len(), 1);
assert_eq!(v.operand_stack.as_slice(), before);
let Value::HeapRef(owner) = before[before.len() - 2] else { panic!() };
assert!(v.heap.is_valid(owner));
}