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
C backend: nested product type uses K^depth memory (974-byte file, 19.3 GB RSS) #843
I was experimenting writing a postgres driver for bend and I reach a issue where the compiler was taking too much time and a lot of memory to compile, so I asked my AI to reproduce the issue in a smaller file.
Steps to reproduce:
bend repro.bend -o /tmp/out.c # 19.3 GB, 50 s, 97 MB .c
bend repro.bend -o /tmp/out.js # 3.3 GB, 3.6 s, 13 KB .js
bend repro.bend # 102 MB, 0.1 s
Maybe I'm doing something wrong/dumb, maybe it's a limitation/bug in the compiler, not sure to be honest, so feel free to close if this out of the scope as bug.
Confirmed on main at 15ae0c8 (bend 2.0.16), Darwin arm64, Apple clang 21.0.0, 24 GiB host. Max RSS from /usr/bin/time -l.
program
-o out.c
-o out.js
the file above
9.05 GiB, 63 s, 97.4 MB
3.04 GiB, 2 s, 13.1 KB
same file, BoxK given one extra nullary arm
10.46 GiB, 73 s, 110.9 MB
3.36 GiB, 3 s, 13.1 KB
The second row is the file above with only this changed:
type BoxK<-a: Data>isData:
Zed{}
BoxK{f0: a, f1: a, f2: a, f3: a, f4: a, f5: a, f6: a, f7: a}
So the cost is not specific to a product type; a sum type pays the same, and a fix that keyed on a single-constructor layout would not cover it.
For contrast, nesting depth on its own is cheap: a chain of 24 defs, each matching a Maybe<&2, U32> and threading the field on into a 24-field record, compiles to C in under a second at 0.14 GiB, and the curve from depth 4 to depth 24 is flat (0.11 to 0.14 GiB, 92 KB to 187 KB of C). It is the field count per level raised to the nesting depth that costs, not the nesting.
Two things were unbounded. The layout of a datatype was flat 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. A datatype past 256 words is now a heap node, the way a recursive type already was. Second, the constant folder turns your chain into a DAG, and the emitter walked that DAG as a tree; a folded static term now emits once per layout.
Your file at depth 7, on this machine:
before
after
time
50 s
0.06 s
peak RSS
19.3 GB
122 MB
emitted C
97 MB
82 KB
The ceiling is 256 words because the widest flat record we ship is a 24-word triangle in the shader demo, and 256 leaves your BoxK boxed from depth 3 up. A record that fits stays flat, so nothing in the fast path changed. 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
Revert "Prevent exponential memory usage for deeply nested product ty…
moorbrook commentedon Sep 19, 2026
Confirmed on main at 15ae0c8 (bend 2.0.16), Darwin arm64, Apple clang 21.0.0, 24 GiB host. Max RSS from
/usr/bin/time -l.-o out.c-o out.jsBoxKgiven one extra nullary armThe second row is the file above with only this changed:
So the cost is not specific to a product type; a sum type pays the same, and a fix that keyed on a single-constructor layout would not cover it.
For contrast, nesting depth on its own is cheap: a chain of 24 defs, each matching a
Maybe<&2, U32>and threading the field on into a 24-field record, compiles to C in under a second at 0.14 GiB, and the curve from depth 4 to depth 24 is flat (0.11 to 0.14 GiB, 92 KB to 187 KB of C). It is the field count per level raised to the nesting depth that costs, not the nesting.A record of records past 256 words is a node, and a folded constant e…
VictorTaelin commentedon Sep 20, 2026
Fixed in 2.0.19.
Two things were unbounded. The layout of a datatype was flat 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. A datatype past 256 words is now a heap node, the way a recursive type already was. Second, the constant folder turns your chain into a DAG, and the emitter walked that DAG as a tree; a folded static term now emits once per layout.
Your file at depth 7, on this machine:
The ceiling is 256 words because the widest flat record we ship is a 24-word triangle in the shader demo, and 256 leaves your
BoxKboxed from depth 3 up. A record that fits stays flat, so nothing in the fast path changed.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.
Revert "Prevent exponential memory usage for deeply nested product ty…
A record of records past 256 words is a node, and a folded constant e…