perf(vm): eliminate recurring stack and return allocations
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -46,21 +46,13 @@ impl VirtualMachine {
|
||||
)
|
||||
.map_err(|(_, error)| self.value_trap(error, opcode, pc))?;
|
||||
|
||||
// Copy return values (preserving order: pop return_slots values, then reverse to push back)
|
||||
let mut return_vals = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes);
|
||||
return_vals.reserve(return_slots).map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
for _ in 0..return_slots {
|
||||
return_vals
|
||||
.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?)
|
||||
.map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
// Validation is complete. Move the ordered result suffix ahead of this
|
||||
// frame's parameters/locals without cloning owners or reserving scratch.
|
||||
if return_slots != 0 {
|
||||
self.operand_stack[frame.stack_base..].rotate_right(return_slots);
|
||||
}
|
||||
return_vals.reverse();
|
||||
|
||||
self.operand_stack.truncate(frame.stack_base + return_slots);
|
||||
self.call_stack.pop();
|
||||
self.operand_stack.truncate(frame.stack_base);
|
||||
for val in return_vals.drain(..) {
|
||||
self.push_trap(val, opcode, pc)?;
|
||||
}
|
||||
self.pc = frame.return_pc as usize;
|
||||
if self.call_stack.is_empty() {
|
||||
self.handle_safepoint().map_err(|e| self.resource_trap(e, opcode, pc))?;
|
||||
|
||||
@@ -62,7 +62,7 @@ impl VirtualMachine {
|
||||
if self.temporary_slots() == 0 {
|
||||
return Err("Stack underflow".into());
|
||||
}
|
||||
self.operand_stack.pop().ok_or("Stack underflow".into())
|
||||
self.operand_stack.pop().ok_or_else(|| "Stack underflow".into())
|
||||
}
|
||||
|
||||
pub fn pop_number(&mut self) -> Result<f64, String> {
|
||||
@@ -82,7 +82,7 @@ impl VirtualMachine {
|
||||
if self.temporary_slots() == 0 {
|
||||
return Err("Stack underflow".into());
|
||||
}
|
||||
self.operand_stack.last().ok_or("Stack underflow".into())
|
||||
self.operand_stack.last().ok_or_else(|| "Stack underflow".into())
|
||||
}
|
||||
|
||||
pub(super) fn pop_trap<S: Into<String>>(
|
||||
@@ -112,8 +112,22 @@ impl VirtualMachine {
|
||||
where
|
||||
F: FnOnce(Value, Value) -> Result<Value, OpError>,
|
||||
{
|
||||
let b = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
|
||||
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
|
||||
let b = self.pop().map_err(|_| {
|
||||
self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
format!("{:?} requires two operands", opcode),
|
||||
start_pc,
|
||||
)
|
||||
})?;
|
||||
let a = self.pop().map_err(|_| {
|
||||
self.trap(
|
||||
TRAP_STACK_UNDERFLOW,
|
||||
opcode as u16,
|
||||
format!("{:?} requires two operands", opcode),
|
||||
start_pc,
|
||||
)
|
||||
})?;
|
||||
match f(a, b) {
|
||||
Ok(res) => {
|
||||
self.push_trap(res, opcode, start_pc)?;
|
||||
|
||||
@@ -699,28 +699,28 @@ fn ordered_multi_group_return_preserves_native_owners_and_receiver_identity() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_return_and_late_return_resource_failures_keep_current_frame_and_operands() {
|
||||
fn unknown_return_refusal_keeps_frame_but_valid_return_needs_no_scratch() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::GetGlobal, 0);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let image = image(&code, vec![function(0, code.len(), &[], &[0], 0, 1)]);
|
||||
for (value, limits, expected) in [
|
||||
(Value::Boolean(true), RuntimeLimits::default(), TRAP_TYPE),
|
||||
(
|
||||
Value::Int32(3),
|
||||
RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap(),
|
||||
TRAP_RESOURCE_LIMIT,
|
||||
),
|
||||
] {
|
||||
for value in [Value::Boolean(true), Value::Int32(3)] {
|
||||
let limits = RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image.clone(), limits);
|
||||
vm.prepare_boot_call().unwrap();
|
||||
vm.globals.push(value.clone()).unwrap();
|
||||
let result = run(&mut vm);
|
||||
assert!(
|
||||
matches!(result,LogicalFrameEndingReason::Trap(info) if info.code==expected && info.pc==6)
|
||||
);
|
||||
assert_eq!(vm.call_stack.len(), 1);
|
||||
if matches!(value, Value::Boolean(_)) {
|
||||
assert!(
|
||||
matches!(result, LogicalFrameEndingReason::Trap(info) if info.code == TRAP_TYPE && info.pc == 6)
|
||||
);
|
||||
assert_eq!(vm.call_stack.len(), 1);
|
||||
} else {
|
||||
assert_eq!(result, LogicalFrameEndingReason::EndOfRom);
|
||||
assert!(vm.call_stack.is_empty());
|
||||
}
|
||||
assert_eq!(vm.operand_stack.as_slice(), &[value]);
|
||||
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user