perf(vm): eliminate recurring stack and return allocations
This commit is contained in:
@@ -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<Option<(usize, usize)>> = 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}");
|
||||
}
|
||||
}
|
||||
@@ -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)]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user