Skip to content

fix(compiler): avoid expanding shared constructor layouts - #1244

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
vicmcorrea:dev/fix-constructor-layout-cache
Oct 2, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
vicmcorrea:dev/fix-constructor-layout-cache

Conversation

@vicmcorrea

@vicmcorrea vicmcorrea commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

after #1196, I checked the case where the program actually constructs the nested sums. The native constructor cache still expands shared layouts into a JSON tree before looking up the value.

The new regression has 24 nested Choice values. On 607a095, C emission crossed a 450 MiB watchdog limit. With this fix it finishes at about 107 MiB sampled RSS.

Change

Key File.consts by layout identity through the existing memo helper. Two compiler lines replaced, plus one regression with its expected output. Compiler size drops by one token.

Separate layout objects with equal structure can get separate cache entries. Static-image deduplication through fl.lits stays unchanged.

Verification

  • The regression passes checking, source execution, emitted JS under Bun and Node, and native execution with 1 and 4 threads.
  • Eight existing regressions and an alias/phantom-type control pass. All nine emitted C comparisons are byte-identical to the base.
  • bun gates/repo.ts passes 50/50. git diff --check passes. Compiler size is 63,212/64,000 ttok.
  • A focused TypeScript 7.0.2 check reports the same two existing bend.ts errors before and after, with none in comp.ts.
  • Independent code and comment reviews found no actionable issues.

Tested on macOS arm64 with Bun 1.3.11 and clang 23.1.2. Cluster, GPU and Linux checks were not run.

To reproduce native emission:

BEND_NO_TELEMETRY=1 bun bend2/main.ts tests/reg/const_shared_layout.bend -o /tmp/const_shared_layout.c

Update: emit_native (#1270)

Fixes #1270.

emit_native had the same JSON.stringify(layout) key for its specializations, so an erased nested Choice type alone blew up C emission (3.1 GB at depth 22, 4.9 GB for the new tests/reg/native_erased_layout.bend at depth 24; now ~120 MB).

The key now names each erased argument's layout object by a number (LAY_IDS, cleared with LAYS). The key joins several layouts, so it needs that number rather than the object itself. Since lay_of makes one layout per type, a specialization is now per type: two types with equal layouts get one each, with the same code, where the JSON key merged them. On the test suite that changes only tests/io/within.bend's C (105 bytes, same functions in the output).

Verification: all 1531 .bend files under tests/ exit the same as on d4d27f3; 688 of 689 emitted C files are byte-identical, and within.bend still runs correctly natively. Compile time across all 689 C programs is the same (7752.7 ms vs 7752.6 ms of compile_book). Gates on this branch merged onto main (fb6f32a): test 1528/1528, perf 117/117, repo 48/49. The repo failure is main's: since #1269, comp.ts is 64,250/64,000 ttok on main alone, and this PR adds 24. The new regression passes in the interpreter, JS and native.

nicolas-abril and others added 2 commits October 2, 2026 16:25
…ased nested sum no longer expands into a JSON tree (bendlang#1270)

emit_native keyed its specializations by JSON.stringify of each erased
argument's layout. A Choice<A> layout points both arms at one child, so the
string doubles per level: 22 levels of an erased Choice took 3 GiB to emit C.

lay_id numbers a layout by its kinds and its fields' numbers, memoized per
object, so a shared field is read once. Equal shapes still get one number,
so two types with one layout share a specialization as before. The
constructor cache uses the same number instead of the layout object, which
keeps its old sharing across equal shapes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…pe, so lay_id and its shape table go

Layouts come one per type from lay_of, so the key now names each layout
object by a number and a specialization is per type. Two types with equal
layouts get one specialization each, with the same code; on the test suite
only io/within's C changes (105 bytes), and compile time is the same.
fl.consts is keyed by the layout object again, as in the first commit.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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 emission expands an erased shared layout exponentially; depth 22 uses about 3 GiB

2 participants