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
@@ -1,3 +1,6 @@
## 2024-01-20 - Lifetime Refactoring in Parser to Eliminate Token Cloning
**Learning:** In the Rust parser (`compiler/parser/src/parser.rs`), methods like `advance()` and `previous()` originally returned a reference tied to `&mut self`. Because `Token` was still borrowing `self` mutably, the parser couldn't call methods like `self.parse_prefix` (which requires another `&mut self` borrow) without first calling `.clone()` on the token to drop the initial borrow.
**Action:** By explicitly defining the return lifetime as `&'a Token` (tied to the lifetime of the underlying token slice `&'a [Token]`, rather than the `Parser` instance), the mutable borrow of `self` ends immediately. This elegantly satisfies the borrow checker while removing the overhead of cloning tokens throughout `expressions.rs` and `statements.rs`. Look for similar lifetime constraints elsewhere in the compiler that force unnecessary copies.
## 2024-08-18 - Eliminating instruction cloning in VM executor loop
**Learning:** In the bytecode executor (`runtime/vm/src/executor.rs`), fetching instructions inside the tight `execute_loop` originally involved `inst.clone()`. Since `Instruction` contains a `Vec<Operand>`, cloning it in the inner execution loop causes massive allocation overhead on every instruction dispatch, severely degrading performance. By maintaining references to instructions from the chunk rather than cloning, we bypassed significant garbage collection and allocation costs.
**Action:** Always scrutinize `.clone()` inside hot execution loops, especially for structs that own heap-allocated fields (like `Vec` or `String`).
7 changes: 5 additions & 2 deletions runtime/vm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ impl VM {
let inst = &func.chunk.instructions[frame.ip];
let current_ip = frame.ip;
frame.ip += 1;
(inst.clone(), current_ip)
// Avoid cloning the instruction on every tick
// Since this loop handles execution, cloning Instruction (which contains a Vec of Operands)
// is extremely slow and allocates on every fetch.
(inst, current_ip)
};

// Diagnostics and tracing
Expand All @@ -37,7 +40,7 @@ impl VM {
let current_func =
&self.module.functions[self.frames.last().unwrap().function_idx as usize];
self.debugger
.trace_instruction(current_func, ip, &inst, &self.stack.get_dump());
.trace_instruction(current_func, ip, inst, &self.stack.get_dump());

match inst.op {
Opcode::NoOp => {}
Expand Down
Loading