From 46bbad19c299174b73d759a6ba220aa2c7f3eafd Mon Sep 17 00:00:00 2001 From: bQUARKz Date: Wed, 14 Oct 2026 10:54:11 +0100 Subject: [PATCH] perf(vm): eliminate recurring stack and return allocations --- .../src/virtual_machine/callable_tests.rs | 42 ++++++++ .../src/virtual_machine/control.rs | 18 +--- .../prometeu-vm/src/virtual_machine/stack.rs | 22 ++++- .../src/virtual_machine/typed_tests.rs | 26 ++--- .../tests/execution_allocations.rs | 99 +++++++++++++++++++ .../tests/execution_regressions.rs | 44 +++++++++ ...-not-the-same-as-hot-path-copy-pressure.md | 25 +++++ .../execution-allocation-qualification.md | 62 ++++++++++++ 8 files changed, 308 insertions(+), 30 deletions(-) create mode 100644 crates/console/prometeu-vm/tests/execution_allocations.rs create mode 100644 docs/vm-arch/execution-allocation-qualification.md diff --git a/crates/console/prometeu-vm/src/virtual_machine/callable_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/callable_tests.rs index 852a7fb..885a7f1 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/callable_tests.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/callable_tests.rs @@ -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)); +} diff --git a/crates/console/prometeu-vm/src/virtual_machine/control.rs b/crates/console/prometeu-vm/src/virtual_machine/control.rs index 5111cf0..7f7aac9 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/control.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/control.rs @@ -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))?; diff --git a/crates/console/prometeu-vm/src/virtual_machine/stack.rs b/crates/console/prometeu-vm/src/virtual_machine/stack.rs index 0faf914..db79c4c 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/stack.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/stack.rs @@ -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 { @@ -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>( @@ -112,8 +112,22 @@ impl VirtualMachine { where F: FnOnce(Value, Value) -> Result, { - 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)?; diff --git a/crates/console/prometeu-vm/src/virtual_machine/typed_tests.rs b/crates/console/prometeu-vm/src/virtual_machine/typed_tests.rs index 60e8395..8fc5a68 100644 --- a/crates/console/prometeu-vm/src/virtual_machine/typed_tests.rs +++ b/crates/console/prometeu-vm/src/virtual_machine/typed_tests.rs @@ -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); } } diff --git a/crates/console/prometeu-vm/tests/execution_allocations.rs b/crates/console/prometeu-vm/tests/execution_allocations.rs new file mode 100644 index 0000000..1bfd880 --- /dev/null +++ b/crates/console/prometeu-vm/tests/execution_allocations.rs @@ -0,0 +1,99 @@ +//! Count only this thread's warmed instruction region, including reallocations. +use prometeu_bytecode::{assemble, load_limits::LoadBudget, registry::*}; +use prometeu_hal::{HostContext, syscalls::caps}; +use prometeu_test_support::program::{TestProgram, int64_signature}; +use prometeu_vm::{ValidatedProgramImage, VirtualMachine}; +use std::alloc::{GlobalAlloc, Layout, System}; +use std::cell::Cell; +struct Native; +impl prometeu_hal::NativeInterface for Native { + fn syscall( + &mut self, + _: u32, + _: &[prometeu_bytecode::Value], + _: &mut prometeu_hal::HostReturn, + _: &mut HostContext, + ) -> Result<(), prometeu_hal::vm_fault::VmFault> { + panic!("no syscalls in allocation fixtures") + } +} + +thread_local! { + static COUNTS: Cell> = const { Cell::new(None) }; +} +struct Counter; +fn count(alloc: usize, free: usize) { + let _ = COUNTS.try_with(|c| { + if let Some((a, d)) = c.get() { + c.set(Some((a + alloc, d + free))); + } + }); +} +unsafe impl GlobalAlloc for Counter { + unsafe fn alloc(&self, l: Layout) -> *mut u8 { + count(1, 0); + unsafe { System.alloc(l) } + } + unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 { + count(1, 0); + unsafe { System.alloc_zeroed(l) } + } + unsafe fn dealloc(&self, p: *mut u8, l: Layout) { + count(0, 1); + unsafe { System.dealloc(p, l) } + } + unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 { + count(1, 1); + unsafe { System.realloc(p, l, n) } + } +} +#[global_allocator] +static ALLOCATOR: Counter = Counter; + +#[test] +fn warmed_instruction_regions_do_not_allocate() { + let mut observations = vec![]; + for (name, caller, callee, results, steps) in [ + ("scalar", "CALL 1\nPOP\nJMP 0", "PUSH_I64 42\nRET", 1, 5), + ("void", "CALL 1\nJMP 0", "RET", 0, 3), + ("multi", "CALL 1\nPOP\nPOP\nJMP 0", "PUSH_I64 11\nPUSH_I64 22\nRET", 2, 7), + ("arithmetic_peek", "PUSH_I64 3\nDUP\nADD\nPOP\nJMP 0", "RET", 0, 5), + ] { + let mut code = assemble(caller).unwrap(); + let offset = code.len(); + let body = assemble(callee).unwrap(); + let mut program = TestProgram::single(code.clone(), int64_signature(0, 0), 4, 8); + program.functions.push(FunctionDecl { + signature: int64_signature(0, results), + code_offset: offset as u32, + code_len: body.len() as u32, + local_slots: 4, + max_stack_slots: 8, + ..Default::default() + }); + code.extend(body); + program.code = code; + let image = ValidatedProgramImage::load( + &program.serialize(), + &mut LoadBudget::new(Default::default()), + caps::NONE, + ) + .unwrap(); + let mut vm = VirtualMachine::from_image(image, Default::default()); + vm.prepare_boot_call().unwrap(); + let mut host = HostContext::new(None); + for _ in 0..steps * 10 { + vm.step(&mut Native, &mut host).unwrap(); + } + COUNTS.with(|c| c.set(Some((0, 0)))); + for _ in 0..steps * 1000 { + vm.step(&mut Native, &mut host).unwrap(); + } + let counts = COUNTS.with(|c| c.replace(None).unwrap()); + println!("{name}: 1000 iterations, {counts:?} allocations/deallocations"); + observations.push((name, counts)); + } + for (name, counts) in observations { + assert_eq!(counts, (0, 0), "{name}"); + } +} diff --git a/crates/console/prometeu-vm/tests/execution_regressions.rs b/crates/console/prometeu-vm/tests/execution_regressions.rs index 2a09a2a..c579c38 100644 --- a/crates/console/prometeu-vm/tests/execution_regressions.rs +++ b/crates/console/prometeu-vm/tests/execution_regressions.rs @@ -216,3 +216,47 @@ fn integer_division_and_remainder_overflow_wrap_without_host_panic() { assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(expected)]); } } + +#[test] +fn maximum_width_mixed_return_preserves_order_and_caller_locals() { + // TestProgram catalog: Int64, Vec2, Float = 0, 6, 2. + let mut caller = String::from("PUSH_I64 73\nSET_LOCAL 0\nCALL 1\n"); + caller.push_str(&"POP\n".repeat(1024)); + caller.push_str("GET_LOCAL 0\nRET"); + let mut callee = String::new(); + let mut results = vec![]; + let mut expected = vec![]; + for i in 0..256 { + callee.push_str(&format!("PUSH_I64 {i}\nPUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nPUSH_F64 f64:0x4008000000000000\n")); + results.extend([TypeId(0), TypeId(6), TypeId(2)]); + expected.extend([Value::Int64(i), Value::Float(1.), Value::Float(2.), Value::Float(3.)]); + } + callee.push_str("RET"); + let caller_code = assemble(&caller).unwrap(); + let body = assemble(&callee).unwrap(); + let mut program = TestProgram::single(caller_code.clone(), int64_signature(0, 1), 1, 1024); + program.functions.push(FunctionDecl { + signature: Signature { parameters: vec![], results }, + code_offset: caller_code.len() as u32, + code_len: body.len() as u32, + local_slots: 3, + max_stack_slots: 1024, + ..Default::default() + }); + program.code.extend(body); + let image = ValidatedProgramImage::load( + &program.serialize(), + &mut LoadBudget::new(Default::default()), + caps::NONE, + ) + .unwrap(); + let mut machine = VirtualMachine::from_image(image, Default::default()); + machine.prepare_boot_call().unwrap(); + for _ in 0..3 + 1024 + 1 { + machine.step(&mut Native, &mut HostContext::new(None)).unwrap(); + } + expected.reverse(); + assert_eq!(machine.operand_stack_top(1024), expected); + assert_eq!(run(&mut machine, 10000).reason, LogicalFrameEndingReason::EndOfRom); + assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(73)]); +} diff --git a/discussion/lessons/DSC-0014-perf-vm-allocation-and-copy-pressure/LSN-0035-first-materialization-is-not-the-same-as-hot-path-copy-pressure.md b/discussion/lessons/DSC-0014-perf-vm-allocation-and-copy-pressure/LSN-0035-first-materialization-is-not-the-same-as-hot-path-copy-pressure.md index d7a4116..4293a03 100644 --- a/discussion/lessons/DSC-0014-perf-vm-allocation-and-copy-pressure/LSN-0035-first-materialization-is-not-the-same-as-hot-path-copy-pressure.md +++ b/discussion/lessons/DSC-0014-perf-vm-allocation-and-copy-pressure/LSN-0035-first-materialization-is-not-the-same-as-hot-path-copy-pressure.md @@ -67,3 +67,28 @@ String concatenation still creates a new string when the language semantics requ - The right performance question is often "where is the repeated cost?" rather than "where is any cost at all?" - Immutable payload sharing is a strong default when public semantics must remain stable and hot-path copies are the real issue. - Internal telemetry should help engineering decisions without silently becoming a product contract. + +## Successful execution can allocate without materializing guest values + +The later stack/return correction (DEC-0061) demonstrated another source of +recurring native allocations: eager diagnostic Strings and disposable return +storage. A warmed scalar call/return loop allocated and freed 3,000 times per +1,000 iterations; lazy diagnostics and in-place return transfer reduced both +counts to zero in debug and release. The original string materialization evidence +above remains a separate observation. + +Validate before moving owners. Rotating only the returning frame's suffix and +then truncating locals preserves ordered logical groups, Arc identity and heap +aliases without cloning or scratch storage. Remove the frame after transfer so +the existing last-root safepoint sees the results. GC scratch refusal is a later +failure boundary: transferred roots must remain intact and sweeping must not +start after failed marking admission. + +Account for storage that actually exists. Eliminating temporary result storage +can make a return succeed under a zero temporary budget without changing any +quota ceiling. A triggered collector still needs its own admitted scratch. + +The [execution allocation qualification](../../../docs/vm-arch/execution-allocation-qualification.md) +records instruction regions, before/after counts and limits. The +[memory contract](../../../docs/specs/runtime/03-memory-stack-heap-and-allocation.md) +remains normative. These measurements say nothing about gameplay throughput. diff --git a/docs/vm-arch/execution-allocation-qualification.md b/docs/vm-arch/execution-allocation-qualification.md new file mode 100644 index 0000000..8980399 --- /dev/null +++ b/docs/vm-arch/execution-allocation-qualification.md @@ -0,0 +1,62 @@ +# VM execution allocation qualification + +Successful stack reads construct diagnostics only on failure. Return completion +validates the exact frame height and ordered signature groups before rotating the +current frame suffix right by its result width and truncating parameters and +locals. Void returns truncate directly. This preserves result order and owners +without a temporary result vector or Value clones. + +Only the returning frame is removed. Caller locals and continuation remain intact. +Direct RET and branches to function end share completion while retaining their +existing instruction charges. The last-frame safepoint still runs after results +are rooted in the operand stack. If GC scratch admission fails, those roots remain +published and no sweep occurs. + +## Measurement + +On 2026-10-14, aarch64 macOS with rustc 1.98.1, the dedicated +`crates/console/prometeu-vm/tests/execution_allocations.rs` integration binary +measured the following native allocation/deallocation calls. Both debug and +release produced the same counts: + +| Instruction loop | Iterations | Before alloc/free | After alloc/free | +| --- | ---: | ---: | ---: | +| CALL / PUSH_I64 / RET / POP / JMP | 1,000 | 3,000 / 3,000 | 0 / 0 | +| CALL / RET / JMP (void) | 1,000 | 0 / 0 | 0 / 0 | +| CALL / two PUSH_I64 / RET / two POP / JMP | 1,000 | 5,000 / 5,000 | 0 / 0 | +| PUSH_I64 / DUP / ADD / POP / JMP | 1,000 | 6,000 / 6,000 | 0 / 0 | + +Run `cargo test -p prometeu-vm --test execution_allocations -- --nocapture` +and the same command with `--release` before `-p`. + +The allocator wrapper enables counting only on the executing test thread after +ten warm-up iterations. It includes alloc, alloc_zeroed, dealloc and realloc +(realloc contributes to both counters). Fixture construction, image validation, +reporting and destruction occur outside the measured region. The fixtures do not +create GC pressure. Error diagnostics, deliberate guest allocations, collection +and dead-string reclamation are outside this zero-allocation claim. This is an +instruction-region qualification, not a gameplay throughput measurement. + +## Semantic and resource evidence + +VM regressions exercise direct, closure and trait calls, taken and untaken +function-end branches, captured strings, ordered mixed result groups, Arc pointer +identity and heap receiver identity. A 1,024-slot mixed return checks the published +result-width ceiling and caller-local preservation. Internal tests check height +and type refusal before mutation, last-root closure survival, GC scratch refusal +without partial sweep, and successful collection after scratch admission returns. +Lifecycle tests continue to check saved continuation and completion boundaries. + +Actual storage remains quota-accounted. Removing the disposable return vector +removes its TemporaryBytes reservation; a valid scalar return can therefore +succeed with zero temporary capacity when no collection is needed. This does not +raise a ceiling, introduce an unmetered cache, or bypass the temporary memory +required by a triggered collector. Operand and call stack capacity accounting, +GC thresholds, verifier contracts and syscall ABI are unchanged. + +The focused VM and transitive-root layer tests qualify these behaviors. Workspace +quality-gate status is tracked separately; these measurements do not establish +that all repository diagnostics or platform checks pass. + +See the [memory and allocation contract](../specs/runtime/03-memory-stack-heap-and-allocation.md) +and the [materialization and copy-pressure lesson](../../discussion/lessons/DSC-0014-perf-vm-allocation-and-copy-pressure/LSN-0035-first-materialization-is-not-the-same-as-hot-path-copy-pressure.md).