You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A literal Nat fuel unrolls the recursion on the device: the voxel DDA at 60n is 13,913 lines, 944 keeps and 2x the frame of U32.to_nat(60) #886
A recursive def called with a literal Nat fuel is unrolled by the compiler: dda(60n, ...), the DDA step of an editable voxel world, becomes one device function of 13,913 lines with 60 copies of the step and 944 term_keep calls, and no loop. The same def called with U32.to_nat(60) stays a loop (7,053 lines of C for the whole program against 20,459; 6 keeps against 950) and the frame costs half: 13 → 6 ms untouched, 26 → 17 ms with 300 edits, at 512², same checksums. def fuel() -> Nat: 60n is inlined and unrolled too. Neither guide mentions it, and the GUIDE recommends the Nat fuel as the way to write a bounded loop, so the natural spelling is the slow one on the device.
What I did
gfx/05_craft.bend in AdrielSantana/metal-bending (the program of #885): fork!(7n) over 4×4 tiles at 512², a DDA of 60 steps per ray written as the guide says:
def dda(fuel: Nat, +w: World, ...) -> U32:
match fuel:
case 0n:
col
case 1n+f:
...
dda(f, w, nvx, nvy, nvz, ...)
def run_dda(...) -> U32:
...
+c = dda(60n, w, vx, vy, vz, ...)
Bench: windowless, five frames with the camera turning 0.01 rad a frame, a total(9n) checksum per frame, "built" = 300 blocks placed in one corner; three alternating runs each, the ten checksums identical across every row. Apple M5, Bend 2.0.17.
512², Metal
untouched
300 blocks placed
C lines
term_keep in the C
dda(60n, ...)
13–14 ms
25–28 ms
20,459
950
dda(U32.to_nat(60), ...)
6 ms
17–18 ms
7,053
6
dda(fuel(), ...) with def fuel() -> Nat: 60n
as the literal
as the literal
20,459
950
In the emitted C of the literal version, run_dda is spin_25: FAR, 13,913 lines, no WL_AGAIN, 1,521 calls to the small spins (787 to the generic Bool.pick, which term_sinks the branch it drops), and 944 term_keep, four per unrolled step on the values the step passes on. In the U32.to_nat version the same def is a WL_SPIN loop with the step once.
For scale: the same pixel function written by hand in WGSL, the step as a for loop of 60, draws the identical frame (same checksums) in 1.34 ms on the same GPU through Chrome. The numbers in #885 are the literal's; with the loop they are 6 / 17 ms, and that question stands.
The question
Is the unroll of a literal fuel intended, and should the device keep the loop instead, or should the guide say that a fuel must be a runtime value on the device? Today the spelling the GUIDE teaches for a bounded loop is the one that unrolls.
Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from his M5.
Verified on 20cfa67: dda(60n, ...) now emits the same C as the U32.to_nat(60) spelling (7,053 lines, 6 term_keep, the loop kept) and the frame is the same: 6 ms untouched, 17–18 ms with the 300 blocks, the ten checksums unchanged. Thanks.
Fixed in 2.0.19, and the cause was ours, not the device's.
emit_fold is a compile-time partial evaluator, and its fuel counted unfolds, not size: 64 unfolds per segment. A literal Nat bound makes every match fuel decidable, so a 60-step loop fits inside 64 unfolds and the whole loop lands in its caller. That is why U32.to_nat(60) changed everything: at 70 the fold aborts and the loop stays a loop.
The fuel is now the size of what an unfold adds (FOLD_FUEL = 8192 term nodes). A wide body and a narrow one no longer cost the same. Measured on your own gfx/05_craft.bend: with a literal bound it emitted 22,049 lines and 950 kept terms; it now emits 9,115 lines and 6 kept terms, identical to the runtime-fuel version. Our perf gate went from 113/117 to 117/117 with the change. Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
Activity
README: the fuel-literal unroll is bendlang/bend#886
A literal loop bound no longer unrolls a wide body into its caller
AdrielSantana commentedon Sep 20, 2026
Verified on 20cfa67:
dda(60n, ...)now emits the same C as theU32.to_nat(60)spelling (7,053 lines, 6term_keep, the loop kept) and the frame is the same: 6 ms untouched, 17–18 ms with the 300 blocks, the ten checksums unchanged. Thanks.VictorTaelin commentedon Sep 20, 2026
Fixed in 2.0.19, and the cause was ours, not the device's.
emit_foldis a compile-time partial evaluator, and its fuel counted unfolds, not size: 64 unfolds per segment. A literalNatbound makes everymatch fueldecidable, so a 60-step loop fits inside 64 unfolds and the whole loop lands in its caller. That is whyU32.to_nat(60)changed everything: at 70 the fold aborts and the loop stays a loop.The fuel is now the size of what an unfold adds (
FOLD_FUEL = 8192term nodes). A wide body and a narrow one no longer cost the same. Measured on your owngfx/05_craft.bend: with a literal bound it emitted 22,049 lines and 950 kept terms; it now emits 9,115 lines and 6 kept terms, identical to the runtime-fuel version. Our perf gate went from 113/117 to 117/117 with the change.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
The fold fuel test folds a wide body from an expression position
A literal loop bound no longer unrolls a wide body into its caller
The fold fuel test folds a wide body from an expression position