Speedcode
Loop unrolling, taken to its conclusion
Replacing a loop with thousands of straight-line instructions to strip out the cost of looping. On the C64 it was the technique that broke raster-split and DYCP records — and it is usually generated, not written.
Speedcode is loop unrolling with the brakes off: instead of looping over an operation, you emit the operation thousands of times in a row, so the processor never pays for the loop.
The idea is that instead of a loop, you “unroll” the inner part of the loop that actually does something, and thereby strip away the administrative costs of the loop logic.
On a 6510 the administrative cost is real — a decrement and a branch on every iteration, against a store that might be three cycles.
It was invented to win competitions
⚠ The origin is competitive, not engineering. Cruzer/CML’s article says so directly:
It was probably first done to get the most rastersplits on the same line, and then later utilized to break DYCP records.
Both of those are scoreboard categories. The record tables bear it out — “most plots at 50 fps” runs from Bones’s The Larch 3 at 600 sine-dots in December 1989, through Nato’s Wriggler at 672 and Oxyron’s Natural Wonders at 960, to Camelot’s Pimp My Snail at 1,536 in 2011. Twenty-two years of the same argument.
Nobody writes it
The technique’s own reference article is mostly about not writing it by hand. Its sections:
- The slave method
- Making the computer code for you
- Scripting/Macros
- Runtime Code Generators
- Optimizing the Speedcode Generator
- What to do if running short on memory?
Five of the six are about generating the code. Speedcode is a compilation target, produced by a script or by a generator running on the C64 itself.
What it costs
The last section is the catch. Unrolling trades memory for cycles, and a 64K machine runs out of memory long before it runs out of ambition — which is why the article ends by asking what to do “if running short on memory”.
See also
- Self-modifying code — the neighbouring trade
- Stable raster
- Size coding — the opposite discipline