Skip to content

Peak Memory Explooooosion (bend 2.0.40 requires too much memory) #2076

Closed

Description

@dmbarbour

What you did

Used bend 2.0.40 for the first time. Immediately crashed memory.

Reproduction:

git clone https://github.com/dmbarbour/glam-in-bend
cd glam-in-bend && git checkout 9922172
cd spike && bend kernel.bend -o kernel.c

What happened

Peak memory measured in a cgroup of its own (memory.peak).

  • bend 2.0.39: peak 1.9 GB, 18 s.
  • bend 2.0.40: killed at a 6 GB cap after 20 s, and at a 7 GB cap. With
    BUN_JSC_forceRAMSize=1000000000 it finishes at 7.6 GB, 38 s.

The emitted C is about the same: 3.57 MB against 3.91 MB, and 2,332
spin_ functions against 2,327. --check-only takes 0.9 GB. The JS
target is unaffected: on a smaller program, 1.2 GB against 1.1 GB.
Programs that call into a smaller part of the same code grow faster than
their output: for 0.4 to 1.1 MB of C, 1.9 to 3.0 GB under 2.0.40 against
1.0 to 1.2 GB under 2.0.39.

The likely cause is in comp.ts. In 2.0.39, FL.memo held the
opens, uses, folds and spines caches, and memo_gc() cleared
them for each definition the emit loop visited. In 2.0.40 those caches
live on FL, along with the new ground and ty_opens, and only
FL.folds is cleared, in unit_emit. The caches are Maps keyed by
term objects (all but ty_opens), so they keep every unit's terms
alive. I emitted the C for a program of 1.1 MB of output, then forced a
full GC (Bun.gc(true)):

2.0.39 2.0.40
heap used 459 MB 1,754 MB
ground entries (cleared) 2.28M
opens entries (cleared) 1.02M
spines entries (cleared) 266K
uses entries (cleared) 251K

To test it, I patched the bundle inside the release binary to clear the
caches alongside folds:

function unit_emit(u) {
  FL.folds.clear();
  [FL.opens, FL.uses, FL.spines, FL.ground, FL.ty_opens].forEach((m) => m.clear());

I ran the patched bundle under the binary's own Bun (BUN_BE_BUN=1).
The C it emits is byte-identical to the release's. Peak memory:

  • the kernel: 2.8 to 3.0 GB in three of four runs (the fourth 6.2 GB),
    30 s;
  • the 1.1 MB program: 3.0 GB down to 1.8 GB.

A WeakMap, as FL.keys already is, might do as well.

What remains above 2.0.39 (1.9 GB, 18 s) I haven't traced. The units
that fill the caches most are a template-heavy rewrite
(krules:rewrite_id~0 to ~3): 165K opens and 324K ground entries
in one unit.

One more difference between the releases: 2.0.40's binary embeds Bun
1.3.14, while 2.0.39's embedded 1.4.0.

(Issue co-authored by Opus.)

bend version

bend 2.0.(39,40) - transition

uname -sm

Linux x86_64 (Intel i7-6700, 16 GB)

clang --version (the first line)

Debian clang version 19.1.7 (3+b1)

Activity

  1. added
    bug
    on Nov 6, 2026
  2. changed the title [-]Peak Memory Explooooosion[/-] [+]Peak Memory Explooooosion (bend 2.0.40 requires too much memory)[/+] on Nov 6, 2026
  3. aivv73 commented on Nov 7, 2026

    @aivv73

    Note

    🤖 gpt-6.1-sol responding on behalf of aivv73

    The bundled Bun version also changes whether C emission completes for our project-sized input. This may be useful evidence for the runtime change noted above, although I have not established a connection to the memo-cache retention described in this issue.

    I ran the same unmodified Bend 2.0.40 source (624dd9f4607cd29b0871a9b96d413bfb11e92f52) against the same input using two embedded Bun runtimes:

    • Bun 1.3.14 from the official Linux x64 Bend 2.0.40 archive: exit 1, Error: the machine stack overflowed (a deep recursion, or a literal too large to expand).
    • Bun 1.4.4 from a build of that exact Bend source: exit 0, C output produced.

    Both runs executed bend2/main.ts directly through BUN_BE_BUN=1, so the comparison does not depend on differences between the two bundled compiler programs:

    # BEND_RELEASE is the official 2.0.40 executable.
    # BEND_REBUILT is the same Bend release source built with Bun 1.4.4.
    # BEND_SRC is the unchanged checkout at the revision above.
    BUN_BE_BUN=1 "$BEND_RELEASE" --version  # 1.3.14
    BUN_BE_BUN=1 "$BEND_REBUILT" --version  # 1.4.4
    
    # Run both from the same Entangle working directory.
    BUN_BE_BUN=1 "$BEND_RELEASE" "$BEND_SRC/bend2/main.ts" \
      tools/verify-mesh-window.bend -o /tmp/window-1314.c
    BUN_BE_BUN=1 "$BEND_REBUILT" "$BEND_SRC/bend2/main.ts" \
      tools/verify-mesh-window.bend -o /tmp/window-144.c

    The input is Entangle's window verification driver, including its imports. It uses our Bend 2.0.40 Vulkan compatibility patch. This is not yet a reduced, self-contained reproducer.

    Environment: Linux x86_64, Clang 23.1.4. The failing command emits C and does not invoke Clang. I have not collected memory measurements for this comparison or identified the underlying cause.

  4. self-assigned this
    on Nov 9, 2026
  5. nicolas-abril commented on Nov 10, 2026

    @nicolas-abril
    Collaborator

    Fixed on main by #2107 and #2108. The fix ships in the next release.

    The kernel and the other spike/ programs. glam-in-bend at 9922172, macOS arm64, Bun 1.3.14 for every version, one run at a time:

    2.0.39 2.0.40 main
    kernel.bend 6.4–6.9 s, 4.4–4.5 GB 12.8 s, 8.5 GB 3.5–4.1 s, 2.7–3.0 GB
    test_levels.bend 4.8–5.1 s, 3.3–3.5 GB 2.2 s, 2.1–2.2 GB
    runmode.bend 5.2–5.5 s, 3.2–3.8 GB 2.9–3.0 s, 2.5–2.7 GB

    The other ten runnable spike/ programs are also at or below 2.0.39 on main. These are peak RSS figures, not cgroup memory.peak, so they compare the releases with each other, not with the numbers above.

    Bun is not the cause of the blowup. The same compiler source behaves the same under both release Buns (the one 2.0.39 embeds, 1.4.0, and the one 2.0.40 embeds, 1.3.14). 2.0.40's source peaks at about 8.4 GB on the kernel under either Bun, and main's at 2.7–3.9 GB. 2.0.40 got Bun 1.3.14 because the release embeds the Bun of the machine that builds it, and nothing pins a version.

    The Entangle driver (the comment above): Entangle at cde97524, with bend-vulkan@a144f7d plus the retained patch, built tools/verify-mesh-window.bend -o x.c. The 2.0.40 release reproduces "the machine stack overflowed" after 14.7 s at 6.0 GB. The real error is RangeError: Out of memory from Map.set:

    • The limit: Bun 1.3.14 caps a Map at 25,165,824 entries, whatever the free memory, and Bun 1.4.0 caps it at 50,331,648.
    • What reached it: 2.0.40's FL.ground cache, which is never cleared, grew past the 1.3.14 limit on this driver. Bun 1.4.x has twice the room, which is why it passed there.
    • The message: the CLI reports every RangeError as a stack overflow (A RangeError that is not a stack overflow is reported as one #2120), so a larger stack could not help.

    On main the caches are dropped between units, and FL.ground peaks at 1.3M entries. The driver compiles in about 3.2 s at 3.1–3.5 GB under both Bun 1.3.14 and 1.4.0, with identical C, so pinning Bun 1.4.4 won't be needed once the next release is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions