Peephole Optimisation
debt(d7/e3/b3/t5)
Closest to 'only careful code review or runtime testing' (d7). Manual strength reduction and hand-collapsed instruction patterns aren't flagged automatically; inspecting output via vld or objdump requires deliberate effort.
Closest to 'simple parameterised fix' (e3). Quick_fix is to stop hand-rewriting and let OPcache/LLVM peephole passes handle it — a localised pattern replacement rather than architectural change.
Closest to 'localised tax' (b3). The choice to hand-optimise affects isolated hot paths; it doesn't shape the whole system but does leave cryptic code wherever applied.
Closest to 'notable trap' (t5). The misconception that peephole competes with global optimisation (rather than running after it) is a documented gotcha, and the local-window correctness constraint trips newcomers writing rules.
Also Known As
TL;DR
Explanation
Peephole optimisation is a classic compiler technique where the optimiser slides a small window (the peephole, typically 2-5 instructions) over the generated code and rewrites recognised patterns into cheaper equivalents. Unlike global optimisations such as dead code elimination across a control flow graph, peephole passes operate locally: they do not need to understand the whole function, only the instructions currently in view. Typical rewrites include collapsing a load followed immediately by a store of the same value, replacing multiply-by-power-of-two with a shift, eliminating redundant moves after register allocation, folding jump-to-jump chains, and removing no-ops introduced by earlier passes. In PHP's Zend Engine, the OPcache optimiser performs peephole-style rewrites on the opcode array: for example, QM_ASSIGN followed by RETURN can collapse, and SEND_VAR with an unused result can be simplified. JIT backends such as LLVM and HotSpot run peephole passes both on IR and on the final machine code to clean up suboptimal sequences left by instruction selection and register allocation. Peephole passes are cheap, run late in the pipeline, and complement rather than replace higher-level optimisations like constant folding or loop unrolling. Their value is cumulative: each individual rewrite saves one or two instructions, but across millions of opcodes the reduction in instruction count and code size is significant. Writing custom peephole rules is a common extension point in compiler toolkits because the pattern-matching framework is simple and the correctness proofs are localised to the window being rewritten.
Common Misconception
Why It Matters
Common Mistakes
- Writing peephole rules that assume broader context — the window is local and must be correct in isolation.
- Running peephole passes too early, before instruction selection or register allocation introduce the patterns worth cleaning up.
- Confusing peephole optimisation with constant folding — folding is a semantic simplification, peephole is a syntactic rewrite over emitted instructions.
- Forgetting that peephole rules can conflict — the order of pattern application matters and may require iterating to a fixed point.
- Expecting large speedups from peephole alone — gains are small per rewrite but accumulate over large codebases.
Code Examples
// Hand-writing "optimised" PHP to simulate peephole rewrites:
$x = $y << 3; // Manual strength reduction instead of $y * 8
$tmp = $a; // Redundant temp
$b = $tmp; // Compiler already eliminates these
// Writing a peephole rule that peeks beyond the window:
// Rule: replace MUL by SHIFT only if later BRANCH is unconditional.
// This breaks locality — the branch is outside the peephole.
// Correctness becomes hard to prove and the rule misfires
// when the surrounding code changes.
// Let the compiler handle local rewrites:
$x = $y * 8; // Readable; backend emits shift if beneficial
$b = $a; // Direct assignment, optimiser removes temps
// Peephole rule example (pseudocode) — operates on a local window only:
// Window: [MOV r1, r2; MOV r2, r1]
// Rewrite: [MOV r1, r2] // second move is redundant
//
// Window: [JMP L1]; L1: [JMP L2]
// Rewrite: [JMP L2] // jump-to-jump collapse
//
// Inspect PHP opcodes with VLD to see what the optimiser leaves:
// php -d vld.active=1 -d opcache.enable_cli=1 script.php