Repository navigation
fix(compiler): avoid expanding shared constructor layouts - #1244
Merged
nicolas-abril merged 3 commits intoOct 2, 2026
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Choicevalues. On607a095, C emission crossed a 450 MiB watchdog limit. With this fix it finishes at about 107 MiB sampled RSS.Change
Key
File.constsby layout identity through the existingmemohelper. 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.litsstays unchanged.Verification
bun gates/repo.tspasses 50/50.git diff --checkpasses. Compiler size is 63,212/64,000 ttok.bend.tserrors before and after, with none incomp.ts.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:
Update: emit_native (#1270)
Fixes #1270.
emit_nativehad the sameJSON.stringify(layout)key for its specializations, so an erased nestedChoicetype alone blew up C emission (3.1 GB at depth 22, 4.9 GB for the newtests/reg/native_erased_layout.bendat depth 24; now ~120 MB).The key now names each erased argument's layout object by a number (
LAY_IDS, cleared withLAYS). The key joins several layouts, so it needs that number rather than the object itself. Sincelay_ofmakes 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 onlytests/io/within.bend's C (105 bytes, same functions in the output).Verification: all 1531
.bendfiles undertests/exit the same as ond4d27f3; 688 of 689 emitted C files are byte-identical, andwithin.bendstill runs correctly natively. Compile time across all 689 C programs is the same (7752.7 ms vs 7752.6 ms ofcompile_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.tsis 64,250/64,000 ttok on main alone, and this PR adds 24. The new regression passes in the interpreter, JS and native.