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 @@ -5,3 +5,6 @@
## 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.
## 2024-05-18 - Removed redundant clone of VM stack during trace logs
**Learning:** In `runtime/vm/src/executor.rs`, the debugging instruction trace `self.debugger.trace_instruction` was cloning the entire VM stack using `&self.stack.get_dump()` for every single instruction executed. This caused significant `O(N)` overhead inside the main fetch-decode-execute loop just to format debug output. A new `data_slice()` method was added to `ValueStack` to provide zero-copy slice access (`&[RuntimeValue]`) instead, completely eliminating the allocation overhead.
**Action:** Always scrutinize deep clones in logging, tracing, or hot path loops. Use slice references (`&[T]`) instead of `Vec::clone` when the caller only needs read-only access to a collection.
2 changes: 1 addition & 1 deletion runtime/vm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl VM {
ip,
inst_op,
inst_operands,
&self.stack.get_dump(),
self.stack.data_slice(),
);
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/vm/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ impl ValueStack {
pub fn get_dump(&self) -> Vec<RuntimeValue> {
self.data.clone()
}

/// Returns a slice of the stack data.
pub fn data_slice(&self) -> &[RuntimeValue] {
&self.data
}
}
Loading