Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
**Learning:** The inner loop of the VM interpreter (`execute_loop`) had an expensive, redundant deep indexing operation to fetch `inst_operands` which was already available on the `inst` reference. Re-fetching it via `self.module.functions[...].chunk.instructions[...].operands` adds unnecessary bounds checks and pointer chasing in the hottest part of the VM.
**Action:** Always prefer using existing local references over redundant deep lookups, especially in tight loops like an interpreter fetch-decode-execute loop.
## 2026-08-19 - Reused local frame and func references in VM opcodes\n**Learning:** The VM executor repeatedly looked up the current frame via `self.frames.last()` and current function via `self.module.functions[...]` in several opcodes (LoadConst, FieldLoad, StoreLocal, etc.). This adds unnecessary bounds checking and pointer dereferencing on the hottest path since `frame` and `func` are already computed at the start of the while loop iteration.\n**Action:** Always reuse existing local references in tight loop opcodes rather than repeatedly querying collections or stacks when the target element is already known and borrowed.
## 2024-05-18 - Rust lifetime limits reuse of mutably borrowed locals in hot VM loop
**Learning:** In `runtime/vm/src/executor.rs`, the main interpreter loop `execute_loop` defines variables `frame` and `func` for the current execution frame and function. While avoiding redundant deep indexing (e.g. `self.frames.last_mut().ok_or(VMError::StackUnderflow)?;`) inside match arms for `Opcode::Jump`, `Opcode::JumpIfTrue`, `Opcode::JumpIfFalse`, `Opcode::Try`, and `Opcode::EndTry` by reusing the existing local `frame` variable reduces bounds checks and overhead, this local `frame` reference cannot be reused inside other match arms like `Opcode::Return` without triggering severe Rust borrow checker issues (e.g., cannot call `self.frames.len()` while `self.frames` is mutably borrowed via `frame`). The previous implementation relied on Non-Lexical Lifetimes (NLL) implicitly ending the borrow of `frame` before reaching opcodes that needed to borrow `self.frames` again. Removing the redundant inner lookups caused the compiler to extend the mutable borrow across the entire loop iteration if not careful, but safely removing them just from control flow opcodes where no further frame manipulation is needed works correctly.
**Action:** Be extremely cautious when extending the lifetime of mutable borrows (especially on central state like a call stack) across large `match` blocks in Rust interpreters, as even correct performance optimizations can easily introduce fatal compilation errors if the borrow inadvertently overlaps with other mutable or immutable accesses.
10 changes: 5 additions & 5 deletions runtime/vm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,13 @@ impl VM {
self.stack.push(RuntimeValue::Bool(res))?;
}

// PERFORMANCE OPTIMIZATION (Bolt):
// We reuse the existing mutable `frame` reference acquired at the start of
// the loop iteration rather than redundantly calling `self.frames.last_mut()`
// for these control flow and exception opcodes. This reduces bounds checking
// and RefCell borrow overhead on the hottest execution paths.
Opcode::Jump => {
if let Some(Operand::JumpOffset(offset)) = inst_operands.first() {
let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?;
frame.ip = ((frame.ip as i32 - 1) + offset) as usize;
} else {
return Err(VMError::InvalidOpcode);
Expand All @@ -462,7 +466,6 @@ impl VM {
if let Some(Operand::JumpOffset(offset)) = inst_operands.first() {
let cond = self.stack.pop()?;
if cond.is_truthy() {
let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?;
frame.ip = ((frame.ip as i32 - 1) + offset) as usize;
}
} else {
Expand All @@ -474,7 +477,6 @@ impl VM {
if let Some(Operand::JumpOffset(offset)) = inst_operands.first() {
let cond = self.stack.pop()?;
if !cond.is_truthy() {
let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?;
frame.ip = ((frame.ip as i32 - 1) + offset) as usize;
}
} else {
Expand Down Expand Up @@ -644,7 +646,6 @@ impl VM {

Opcode::Try => {
if let Some(Operand::JumpOffset(offset)) = inst_operands.first() {
let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?;
let catch_ip = ((frame.ip as i32 - 1) + offset) as usize;
frame.handlers.push(ExceptionHandler {
catch_ip,
Expand All @@ -656,7 +657,6 @@ impl VM {
}

Opcode::EndTry => {
let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?;
frame.handlers.pop();
}

Expand Down
Loading