perf(vm): add peephole pass to elide dead Load; Pop pairs ✂️ - #163
Merged
Conversation
Runs to a fixed point over the IR, removing consecutive `Constant|GetGlobal|GetLocal` followed by `Pop` when neither is a jump target. Skipped on the REPL's resumable path so `halt_ip` stays stable across resume-from-halt. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
timfennis
force-pushed
the
feature/peephole-optimization
branch
from
May 29, 2026 19:54
05963a7 to
76937fe
Compare
…es 🔬 Adds `Compiler::compile_unoptimized` and `Interpreter::compile_str_unoptimized` so the existing compiler-tests crate can document the raw compiler output again. Reverts the seven assertions that PR #163 updated for the post-peephole form, and introduces `tests/optimizer.rs` with focused smoke tests for the peephole pass itself. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cdd03bb1ad
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The nested `fn_compiler` was constructed with `Self::default()` and called `peephole()` unconditionally, so function bodies were optimized even when the caller used `compile_unoptimized` or `compile_str_unoptimized` for the top-level. Inherit `self.optimize` and gate the call. Adds `CompiledFunction::body()` as a public accessor so the new test can walk the top-level constants table to inspect a nested function's raw vs optimized bytecode. Spotted by Codex on PR #163. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
First peephole-style optimization pass on the bytecode IR. Motivated less by raw speed gains and more by setting up a place for future passes to live —
Constant; Popand friends are the obvious starting case (dead expression-statement results).Changes
OptimizerIr::peephole()pass (ndc_vm/src/chunk.rs). Elides consecutiveConstant | GetGlobal | GetLocalfollowed byPopwhen neither instruction is a jump target. Runs to a fixed point so nested patterns ([Load, Load, Pop, Pop]) collapse fully.Compiler::optimize: bool(defaulttrue). The fresh-compile path (Compiler::compile) and inner function compilation always optimize.Compiler::compile_resumable— the REPL's path — sets it tofalse. The REPL relies onhalt_ipmatching the position ofHaltin the emitted chunk for resume-from-halt; peephole would shift instruction positions and invalidate that. Working around that requires either a frozen-prefix mechanism or reorderingfinish(), both of which proved tricky enough that opt-out for the REPL is the better trade.compiler_testsassertions and comments for the seven affected cases (test_statement,test_block_*,test_if_with_statement_*,test_while,test_assignment) to reflect the post-peephole opcode sequences.Performance
Across a local Advent-of-Brian-style suite of ~22 puzzles: aggregate −1.0%. Mid-sized puzzles (10–100 ms) see 5–10% wins; large puzzles dominated by stdlib calls are flat. On the internal
benches/suite the standout wins aresieve(−17%) andstring_concat(−13%) — both have hot loops with discarded expression values, which is exactly the pattern the pass targets.🤖 PR description generated by Claude.