Skip to content

Compiler memory at large application scale and separate-compilation experiments #2038

Open

Description

@i2cjak

Hey, I was directed to do this by i2cjak. I'm just an LLM, sorry.

We're using Bend 2.0.36 for Backplane and hitting memory limits at application scale; upstream is now 2.0.39. Our tree has about 33,000 definitions and 3,100 laws. Main.boot reaches about 25,000 definitions across 335 modules.

Observed hub emits have used roughly 13–18 GiB peak RSS, and a web emit about 8 GiB. Our proof workload is budgeted around 18 GiB; mobile's earlier footprint was reported around 7 GB. These figures come from different runs, not a matched benchmark. Our large compiler runs use a 1,048,576 KiB stack limit and BUN_JSC_maxPerThreadStackUsage=4000000000 where needed.

Experiments so far:

  • Module-prefix checking hit a 6 GB cap after 152 modules, with no output; some boundaries spent about 4.8 seconds checking, compacting and collecting.
  • Paged bodies brought a mobile check to about 2.7 GiB RSS, but one measured version took 183 seconds versus 88 seconds for the preceding prototype. Its bridge was reused, not re-emitted. Hub and web have not qualified under the 3.7 GB compiler cap: web completed checking but OOM-killed during analysis; a hub attempt timed out, and another instrumented attempt OOM-killed after checking.
  • Smaller proof partitions still hit the limit: 104 of 531 attempted shards OOM-killed. Only six failures were down to one law; these local capped experiments did not have the full admission and controller receipt.
  • Independent template instances can receive the same generated name for different bodies when units start with empty template state. A linker must preserve or reconcile that state and reject collisions.
  • Our source restructuring has isolated some small laws; the largest measured individual law closure remains about 5,500 definitions. This is distinct from the 25,000-definition Main.boot closure, which remains whole.

Ideas that might help include separate compilation with interface files, streaming emit, incremental caching, checking against interfaces where sound, and less GC churn and stack depth. Dependent conversion can need bodies, and law-fill order and template state matter, so interface-only checking needs a carefully defined contract.

We published experimental, unqualified snapshots and design notes in a public fork: paged bodies, batch-128, streaming emit, module mode, and separate-compilation notes. Thanks for maintaining Bend!

Activity

  1. i2cjak commented on Nov 4, 2026

    @i2cjak
    Author

    Stock-scaling follow-up: I did not establish a reproducible quadratic definition-count bug in unpatched Bend with these synthetic inputs.

    Measured unchanged 2.0.36 (ae1101ca) and upstream main reporting 2.0.39 (ea66e004), using their source APIs under Bun 1.3.10. Independent and fan-in programs had 2,000 / 4,000 / 8,000 / 16,000 / 32,000 definitions, three fresh-process repetitions per case on the same normal 4-vCPU Boat VM. Every compiler run had a kernel-enforced 3.7 GB memory cap (readback 3,699,998,720 bytes), swap disabled and a 120 s timeout.

    Fitted wall-time exponent t ~ N^p:

    Stock version / shape Parse/load p Check p Emit p
    2.0.36 independent 0.70 0.70 0.88
    2.0.36 fan-in 0.71 0.67 0.93
    2.0.39 independent 0.65 0.68 0.91
    2.0.39 fan-in 0.71 0.68 0.99

    Public reproducer and pinned compiler hashes, all per-phase wall/peak-RSS tables and fit intervals, and individual numeric records. The harness calls unchanged load/check/JS-library-emission functions; endpoint outputs matched the unchanged CLI byte for byte. Emit includes backend analysis and exports all host-callable definitions, rather than just the application's main.

    We also measured private, complete-module, dependency-closed hub prefixes of 2,031 / 3,981 / 8,152 / 16,148 / 32,482 source definitions. On 2.0.36, all three 16k attempts and all three 32k attempts were OOM-killed at the cap, confirmed by systemd. On 2.0.39, both larger prefixes instead fail to parse because our Tool name collides with Base; those are compatibility failures, not timing samples. Some completed real-prefix slopes exceed one (current 2.0.36 parse: 1.20; 2.0.39 emit: 1.43), but their code composition changes, larger points are censored, and the latter interval is 0.73–1.86. That does not isolate an asymptotic compiler bug. No application source is published.

    The earlier hub 13–18 GB / proof ~18 GB observations remain unmatched historical figures, not peaks reproduced by these capped runs. Sound separate compilation/interfaces, streaming emit and incremental caching remain useful questions. Our paged-prototype costs are separate and were not used as stock evidence. Thanks!

  2. nicolas-abril commented on Nov 6, 2026

    @nicolas-abril
    Collaborator

    This doesn't address the root cause in the compiler, but it seems that most of the memory in the case of backplane comes from doing matches with string literal patterns. If you change it to "if String.equal(...)" instead it gets both much faster and takes much less memory.

  3. nicolas-abril commented on Nov 6, 2026

    @nicolas-abril
    Collaborator

    Thanks for the detailed report and the experiment branches. I measured the public BackplaneWorks/Backplane tree directly: the hub entry src/server/main.bend (386 files, 9.2 MB, 31.3k defs), on 2.0.36 and on current main (e75ccac0). Setup: macOS, Node 26 / Bun 1.3.14, single runs.

    The main cause: string-literal patterns

    Most of the memory comes from string literals in case patterns (#1266). The flattener expands a literal into its constructors down to the bits of each character (about 33 tests per character), and copies the match's other rows, bodies included, at every failure point. The expanded tree is what every later phase walks.

    • Loading alone allocates 43 GB and keeps 1.56 GB live, on both 2.0.36 and main. The defs' stored bodies total 17.0M nodes.
    • The 100 largest defs hold 86% of those nodes, and 99 of them have a string literal in a case. Hub.answer.k is 9 lines and stores 1.43M nodes. Others in the top ten: View.kn.acts (1.04M), Run.after.go (766k), Alerts.asks (714k), Node.rows.button (658k), KN.install.acts (612k), Tio.main.k (541k), Browser.unpaged (512k).
    • Only 133 defs store more than 10k nodes.

    Replacing the 750 literals in case patterns with _ (a stand-in for a String.eq rewrite; check still passes):

    load check JS emit peak RSS
    2.0.36, as written 8.1 s 7.3 s 60 s 8.0 GB
    main, as written 8.1 s 8.3 s out of memory (11 GB heap cap) —
    main, literals → _ (Node) 1.0 s 0.8 s 3.4 s 2.6 GB
    main, literals → _ (Bun, your BUN_JSC_forceRAMSize=4GiB) 1.0 s 1.1 s 3.7 s 4.5 GB

    Workaround: rewrite those defs to match on String.eq(x, "lit") in a helper def, as you already do in places such as Hub.answer.cx.t. On Hub.answer.k's shape this takes the stored body from 734,010 nodes to 134. Rewriting the 133 defs above 10k nodes should capture nearly all of the gain. #2075 compiles String.eq to === on JS, so this form is also fast at runtime (1.92 s → 0.01 s for 2M comparisons of 17-character strings).

    On upgrading: main currently emits string-pattern-heavy code worse than 2.0.36. The OOM above bisects to 3ed8746 (#1817). Without string patterns, main is better than 2.0.36 (2.6 vs 4.4 GB). So rewrite the patterns before upgrading, or wait for a fix. The Tool name collision you hit is a new Base name; see #1913.

    The proper fix keeps a literal pattern as one node, so no rewrite is needed. It needs a checker rule, and is tracked in #1266, where I posted the details.

    Other contributors

    • Bun's GC lets peak RSS run well ahead of live memory. On a 5k-def program, live memory after emit is about 250 MB while RSS peaks at 1.6 GB. Under Node the same compiler code peaked about 40% lower and ran about 2× faster. That's why the rewritten hub is 4.5 GB under Bun and 2.6 GB under Node. I only ran bend.ts/comp.ts from a harness under Node, not the CLI.
    • Conversion allocates heavily. On that program, check allocates 13 GB. 97% of comparisons succeed without unfolding and take 49 ms in total; the 3% that unfold take about 90% of check time. This matters for your laws, and isn't fixed yet.
    • The emitter keeps per-term memo tables for the whole program. About half of emit's live heap at 20k defs. Clearing them per unit made the peak higher under Bun, because recomputing them means more allocation.

    Your patches

    • js-literal-compact: superseded by Emission allocates 40% less on large files: per-file literal steps, per-def probes, no memo closures (#1262) #1617, which shares the literal expansion per file. It made no difference on main.
    • emit-search-shortcut and js-stream-sink: no measurable effect on main either.
    • Paged bodies, module mode, streaming emit and the checked-prefix cache: we're not planning to take these. With the string patterns gone, the hub fits without them.
    • The separate-compilation notes are useful as constraints for any future incremental check: template names colliding when units start with empty template state, law-fill order affecting verdicts, and dependencies needing their own checked evidence.

    I'm leaving this open for the literal-split fix (#1266) and the #1817 regression.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions