Skip to content

Fuse a recursive consumer with the recursive producer it consumes (deforestation) - #2031

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/deforestation
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/deforestation

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 67,762 ttok, 1,762 over the 66,000 cap, so repo-gate fails until that is settled. This is independent of #2030 (closure inlining). Both edit fun_of's body line and add lt_any, term_under and let_end, so whichever merges second shares them.

What it does

A call c(p(xs), ys) runs a new def c<p(xs, ys), where c is a consumer and p a producer. Both must call themselves and take only live, closed datatypes.

  • Body: c<p is p's body with c applied at each result.
  • Deforestation: where a result is a constructor, it meets c's leading match, the arm is selected and the node is never built. Inside that arm, c called on a call of p becomes c<p again.
  • Only when it pays: a fused def is kept only if some constructor result meets the match.
  • Lets: a lone let of a producer call forwards into its use. A result's lone let of a constructor (a b = p(..) p(..); Ctr{a, b}) forwards into it, so the producer's fork becomes the consumer's.
  • Chains: fused defs fuse again, up to three levels, e.g. sum<bump<bump<grow. Fused defs live in FL.made; the book is not mutated.

The comptime twins (tests/comptime/hand.bend, tmpl.bend) now consume the mapped list through sum(acc, xs), which takes the list second. Otherwise fusion consumes List.map~0 and the gate's "only tmpl declares List.map" probe fails. Both twins change the same way, so they still compare template output to the hand-written version.

Results

Paired cluster runs: each case on 3 nodes, 5 rotated samples per node after a warm-up, wall time, medians.

program SEQ main SEQ this PAR main PAR this
collect(change(grow(24, s), 17)) 0.147 s 0.040 s 3.7x 0.038 s 0.008 s 4.5x
two changes 0.194 s 0.040 s 4.8x 0.048 s 0.009 s 5.4x
chain producer, consumer with a salt 0.145 s 0.052 s 2.8x 0.144 s 0.053 s 2.7x
filter between them 0.150 s 0.063 s 2.4x 0.151 s 0.063 s 2.4x
producer shared with a second use 0.249 s 0.143 s 1.7x 0.113 s 0.036 s 3.1x

Bench suite: tree-radix is the only one of the 17 bench/runtime programs whose C changes (to_arr fused through to_map's merge). In the paired run it took 3.342 s against 3.573 s on SEQ-CPU (0.935x), 0.602 s against 0.641 s on PAR-CPU (0.939x), and 1.108 s against 1.118 s on GPU (0.991x). Every other bench compiles to byte-identical C.

Checks

  • test.ts: 1827/1827, with the new tests/compile/deforest.bend.
  • Compile time: compile_book over all tests took 18.2 s against main's 18.0 s, in one concurrent run. Warm compile_book on the benches is within 0.5 ms of main except tree-radix (7.2 to 16.5 ms) and merkle (32.7 to 37.2 ms).
  • Output across the corpus: C changes in 42 tests and JS in 58.
  • tsc shows no new errors in comp.ts.
  • repo.ts fails only on the ttok cap.

Not covered

  • Non-recursive producers, such as a filter's keep helper on its own. The consumer and producer around it still fuse.
  • Erased parameters or dependent signatures on either side.
  • Multi-output fusion (tupling) of two consumers of one producer.

Cost

+1,767 ttok. Getting under the cap needs that many tokens cut elsewhere in comp.ts, or a raise.

🤖 Generated with Claude Code

A call c(p(xs), ys), where c and p call themselves and take live closed
datatypes, runs a def c<p(xs, ys) made from p's body with c applied at
each result. A constructor result meets c's match there and is never
built, and c called on a call of p inside is c<p again. The fused def is
kept only if some constructor meets the match. A lone let of a producer
call forwards into its use, and a result's lone let of a constructor
forwards into it, so producer forks become the consumer's. Fused defs
fuse again, up to three levels.

The comptime twins pass the mapped list second (through sum), so no
fusion consumes the map and they still compare template to hand-written.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#3, which keeps it saved off the main queue until it can be accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant