IT lexicon › Programming › Peephole optimization

Peephole optimization

Programming På svenska → Updated: 2026-11-05

An optimization that looks at a small window of adjacent instructions and swaps them for shorter or faster equivalents.

William McKeeman described the technique in a short 1965 paper in Communications of the ACM. It is deliberately short-sighted: the optimizer doesn't understand the program, it just recognises patterns. A store followed by a load from the same address loses the load, mov eax, 0 becomes xor eax, eax, and a multiply by eight becomes a shift.

Because it runs late, close to machine code, it mops up the clumsy sequences earlier passes leave behind. Henry Massalin's 1987 superoptimizer pushed the idea to its limit by exhaustively searching for the shortest instruction sequence for a given function — and occasionally found tricks no human would have written.

← Back to the lexicon