Repository navigation
Description
On behalf of Mathieu G. (mr-zwets), written by Claude Opus 5.5:
Problem
for and while loops test their condition at the top of every iteration, then carry the result over the body on the altstack:
BEGIN <cond> DUP TOALTSTACK IF <body> <update> ENDIF FROMALTSTACK NOT UNTIL
Beyond the condition itself, that is 5 instructions (500 op-cost) of control flow per iteration: DUP TOALTSTACK IF ... ENDIF FROMALTSTACK. For loops with small bodies it is a large share of the cost.
Proposal
Standard loop inversion, reusing the existing do-while codegen: test once on entry, then re-test at the bottom.
<cond> IF BEGIN <body> <update> <cond> NOT UNTIL ENDIF
- When the loop variable starts at a literal and is compared against a literal, the first test is known to pass and the
IF ... ENDIFguard can be dropped. - The existing peephole already turns
LESSTHAN NOTintoGREATERTHANOREQUAL, so a typical loop ends inOP_1ADD OP_DUP <n> OP_GREATERTHANOREQUAL OP_UNTIL. - No language change. Users can already get this by hand with
if (c) do { ... } while (c);.
Literal bounds are the normal case for contract loops: a fixed number of rounds, bits or bytes. For example for (int i = 0; i < 254; i++) walks the bits of a BN254 scalar. In one codebase of BN254/BLS12-381 verifier contracts, 131 of 132 for loops have a literal start and a literal bound (i < 254, i < 381, k < 25, ...), as do all 8 for loops in test/valid-contract-files. So the guard is almost always dropped.
Measured
64-iteration Horner-style loop (one byte read, two shifts and two big-int multiply-adds per iteration), BCH 2026 VM:
| bytes | op-cost | |
|---|---|---|
current for codegen |
139 | 478,062 |
| inverted, guard dropped | 133 | 433,069 (-9.4%) |
(for reference) hand-written if (c) do { ... } while (c) |
141 | 433,771 |
The saving is about 5 instructions (~500 op-cost) per iteration, so it matters most for loops with small bodies. Bytes are usually unchanged or smaller; with a runtime bound (guard kept) the condition is emitted twice and a loop can grow by a couple of bytes, in exchange for the lower per-iteration cost.
Prototype
About 70 lines in GenerateTargetTraversal.ts (emitWhile / emitForLoop), on top of our fork's compiler-optimizations-7 branch, but independent of the fork's other changes:
- an internal
loopInversion: falseoption keeps the current codegen for tests and tooling; - cashc tests: everything passes except the 15 loop-contract bytecode fixtures, which need regenerating;
- SDK tests: all pass against the prototype, including the debugging tests that execute loops (error attribution, console logging);
- one debugging test needed recalibrating: "should report an operation cost overrun" relied on the old loop's per-iteration cost to hit the op-cost limit before the hashing limit. Adding a little non-hash work to its loop body makes it pass with both the current and the inverted codegen.
Happy to open a PR against master with the regenerated fixtures if this looks good.
Activity
rkalis commentedon Dec 1, 2026
Repeating the
<cond>can result in much higher bytesize if the cond is big. Even at smaller cond, it is likely to result in a slightly bigger bytesize. However, it does always result in a significantly lower op-cost. So this seems like something to consider as part of the larger cost model rework in #474.