From d5042d5a9fb2964227ecd77e9db2b1f630fb4b00 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 05:17:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Remove=20redundant=20frame?= =?UTF-8?q?=20lookups=20in=20VM=20executor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- .jules/bolt.md | 3 +++ runtime/vm/src/executor.rs | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 7e8ee7bc..f86707a6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -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. diff --git a/runtime/vm/src/executor.rs b/runtime/vm/src/executor.rs index 226c2fc9..d4032d97 100644 --- a/runtime/vm/src/executor.rs +++ b/runtime/vm/src/executor.rs @@ -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); @@ -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 { @@ -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 { @@ -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, @@ -656,7 +657,6 @@ impl VM { } Opcode::EndTry => { - let frame = self.frames.last_mut().ok_or(VMError::StackUnderflow)?; frame.handlers.pop(); }