Skip to content

Prevent exponential memory usage for deeply nested product types in the C backend - #844

Merged
VictorTaelin merged 2 commits into
bendlang:mainfrom
leofmarciano:main
Sep 19, 2026
Merged

VictorTaelin merged 2 commits into
bendlang:mainfrom
leofmarciano:main

Conversation

@leofmarciano

@leofmarciano leofmarciano commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #843.

The C backend previously flattened nested product types indefinitely. For a product with K fields nested to depth D, this could create layouts and generated code proportional to K^D, causing excessive compilation time and memory usage.

This change memoizes computed layouts and switches oversized inline product layouts to boxed representation. With the depth-7 BoxK reproducer, the generated C output drops from approximately 93 MB while using 4 GB of memory to approximately 80 KB while using 130 MB of memory. The compiled binary runs successfully and outputs 0.

Added a regression test covering the nested product case.

devin-ai-integration Bot and others added 2 commits September 19, 2026 04:39
Co-Authored-By: Leonardo Marciano <[email protected]>
…ct-c-backend

Box deeply nested product layouts
@VictorTaelin
VictorTaelin merged commit f2ddc68 into bendlang:main Sep 19, 2026
@VictorTaelin

Copy link
Copy Markdown
Contributor

looks good, ty

VictorTaelin added a commit that referenced this pull request Sep 19, 2026
…pes in the C backend (#844)"

With it, three C-lane tests print wrong answers: loop_peel_countdown
45475n for 77975n, parse/float_pattern 0 for 1,
parse/string_pattern_sugar False{} for True{}. Its layout cache keys on
a sugared print of the type, and boxing every layout past 16 words
changes the ABI of wide records; #843 stays open for a fix that passes
the test and perf gates.
@VictorTaelin

Copy link
Copy Markdown
Contributor

Reverted on main: with this change three C-lane tests print wrong answers (tests/compile/loop_peel_countdown 45475n instead of 77975n, tests/parse/float_pattern 0 instead of 1, tests/parse/string_pattern_sugar False{} instead of True{}); with it reverted they are right again. Two causes to look at: the layout cache keys on term_show's print of the type, which is not injective (and term_show no longer takes a key flag since 2.0.16: use term_key), and boxing every layout past 16 words changes the ABI of wide records that the runtime keeps flat. A fix for #843 is welcome if bun gates/test.ts and bun gates/perf.ts pass.

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.

VictorTaelin added a commit that referenced this pull request Sep 20, 2026
…mits once

lay_of flattened every datatype at any width, so a record nested K deep
with F fields was F^K machine words and the emitter named every one as a
local: the reporter's depth-7 file took 50 s, 19.3 GB and 97 MB of C. Now
a datatype past WIDE (256) words is a box, the way a recursive type is;
lay_of is memoized on the type's term, so a nested type costs its depth
(a fill shares its argument), not its width. The three C tests #844 broke
at 16 pass, and the shader's Tri (24 words) emits byte-identical C.

The folder turns such a chain into a DAG whose shared body the emitter
walked as a tree (8^7 leaves at depth 7, 8 s, into a static image that
holds each node once). emit_ctr now keeps a static Val per (term, layout)
in the file's consts, and a literal Nat word counts as static, so the
walk is the DAG. Depth 7 compiles in 0.06 s, 129 MB, 81 KB of C, and C,
JS and the interpreter print the same value.

tests/compile/record_deep_boxed pins a seeded 4-deep BoxK (level 3 boxed
at 512 words, level 4 flat again as eight boxes) dug through a nested
match, plus the reporter's constant chain. (#843)
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…he C backend (bendlang#844)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…pes in the C backend (bendlang#844)"

With it, three C-lane tests print wrong answers: loop_peel_countdown
45475n for 77975n, parse/float_pattern 0 for 1,
parse/string_pattern_sugar False{} for True{}. Its layout cache keys on
a sugared print of the type, and boxing every layout past 16 words
changes the ABI of wide records; bendlang#843 stays open for a fix that passes
the test and perf gates.
Phylliida pushed a commit to Phylliida/bend that referenced this pull request Sep 27, 2026
…mits once

lay_of flattened every datatype at any width, so a record nested K deep
with F fields was F^K machine words and the emitter named every one as a
local: the reporter's depth-7 file took 50 s, 19.3 GB and 97 MB of C. Now
a datatype past WIDE (256) words is a box, the way a recursive type is;
lay_of is memoized on the type's term, so a nested type costs its depth
(a fill shares its argument), not its width. The three C tests bendlang#844 broke
at 16 pass, and the shader's Tri (24 words) emits byte-identical C.

The folder turns such a chain into a DAG whose shared body the emitter
walked as a tree (8^7 leaves at depth 7, 8 s, into a static image that
holds each node once). emit_ctr now keeps a static Val per (term, layout)
in the file's consts, and a literal Nat word counts as static, so the
walk is the DAG. Depth 7 compiles in 0.06 s, 129 MB, 81 KB of C, and C,
JS and the interpreter print the same value.

tests/compile/record_deep_boxed pins a seeded 4-deep BoxK (level 3 boxed
at 512 words, level 4 flat again as eight boxes) dug through a nested
match, plus the reporter's constant chain. (bendlang#843)
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.

C backend: nested product type uses K^depth memory (974-byte file, 19.3 GB RSS)

2 participants