Repository navigation
Prevent exponential memory usage for deeply nested product types in the C backend - #844
Conversation
Co-Authored-By: Leonardo Marciano <[email protected]>
…ct-c-backend Box deeply nested product layouts
|
looks good, ty |
…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.
|
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 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. |
…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)
…he C backend (bendlang#844) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…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.
…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)
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.