pub struct VMPool { /* private fields */ }Expand description
Pool of reusable VM instances.
VM::new does ~3 Vec allocations (stack, frames, globals) at
construction. Callers that run many small chunks back-to-back —
REPL-style invocation, batch script execution, eval loops — pay
that cost on every call. VMPool recycles the allocations: the
first acquire allocates, subsequent acquires pop a previously-
released VM and reset it via VM::reset.
§Example
use fusevm::{ChunkBuilder, Op, VMPool, VMResult, Value};
let mut pool = VMPool::new();
for _ in 0..1000 {
let mut b = ChunkBuilder::new();
b.emit(Op::LoadInt(40), 1);
b.emit(Op::LoadInt(2), 1);
b.emit(Op::Add, 1);
let mut vm = pool.acquire(b.build());
let result = vm.run();
assert!(matches!(result, VMResult::Ok(Value::Int(42))));
pool.release(vm);
}Implementations§
Source§impl VMPool
impl VMPool
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Construct with a pre-allocated capacity.
Sourcepub fn acquire(&mut self, chunk: Chunk) -> VM
pub fn acquire(&mut self, chunk: Chunk) -> VM
Acquire a VM ready to run chunk. Pops a recycled VM if
available; otherwise constructs a fresh one. The returned VM
inherits the pool’s previously-released VMs’ allocations
(Vec capacities preserved).
Sourcepub fn release(&mut self, vm: VM)
pub fn release(&mut self, vm: VM)
Return a VM to the pool for later reuse. The VM’s allocations
are kept; only state is cleared on the next acquire.
Sourcepub fn with<F, T>(&mut self, chunk: Chunk, f: F) -> T
pub fn with<F, T>(&mut self, chunk: Chunk, f: F) -> T
Run a closure against an acquired VM, returning it to the pool after the closure finishes (RAII-style scope).