Skip to content

fix(compiler): avoid expanding shared display layouts - #1196

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
vicmcorrea:dev/fix-shared-layout-keys
Oct 1, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
vicmcorrea:dev/fix-shared-layout-keys

Conversation

@vicmcorrea

@vicmcorrea vicmcorrea commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

the JSON cache key from #1166 copies shared child layouts into a tree. That can turn a small type into a huge string before the compiler even checks the cache.

The regression below just returns Ready{42}, but compiling it exceeds 800 MiB because another alternative in its result type contains nested sums.

Scope

Use layout identity and the normalized type as show_main's cache key, through the existing memo helper. One net compiler line and a 16-line regression. The layout distinction from #1166 stays intact.

Tradeoffs

Separate layout objects with equal structure may get separate descriptors. This avoids expanding the graph or adding a structural interner.

Blast radius

Display descriptors for pure-main results in JS and native builds. IO mains bypass this cache.

Verification

On macOS arm64, Bun 1.3.11 and clang 23.1.2. The reproduction command is:

bun bend2/main.ts tests/reg/show_shared_layout.bend -o /tmp/show_shared_layout.js
  • Compiling the new regression to JS on eb8f58b crossed an 800 MiB sampled-RSS limit and the watchdog stopped it. With this fix, the same command completed in 0.10 s at 113 MiB sampled RSS.
  • The regression passes checking, interpreter, emitted JS and native execution, printing Ready{42}.
  • Ten existing tests and one mixed-type control pass, including all three fix(compiler): key display descriptors by layout #1166 regressions. Their emitted C and JS are byte-identical to main. The unprintable-main diagnostic also matches.
  • bun gates/repo.ts: 49/49. git diff --check passes. Compiler size is 63,778/64,000 tokens, 13 more than main.

Cluster, GPU and benchmark gates were not run.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 1, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Hey @nicolas-abril , I was reviewing this PR and dug into the tradeoff since you moved #1166 to the JSON key on purpose. Two data points:

  • Across all 143 pure-main files in tests/demos/benches, the JSON key never merged two nodes that the identity map would keep apart. Layouts are already interned per type by lay_of, so the only case where it fires is an Array element's open layout matching a flat value, and it saves one descriptor row.

  • The cost: JSON.stringify expands shared layouts as a tree, so nested sums double the key per level. This PR's regression builds a 1.5 GB key. Memory goes 79 MiB → 225 MiB from n=12 to 18 and gets OOM-killed at 24

But I see you reverted a very similar change on the #1166 mentioned, i'm trying to get the rationale behind it.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

As far as I remember I thought the nested Map wasn't needed for the same output so i cut it for simplicity

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.

3 participants