Skip to content

fix(comp): a module exports its own defs, so a page ships what it uses - #2224

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
caiodomingues:fix/loader-own-exports
Nov 23, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
caiodomingues:fix/loader-own-exports

Conversation

@caiodomingues

Copy link
Copy Markdown
Contributor

Problem

In module mode (the bun plugin in bend2/main.ts, and bend file.bend -o f.mjs), js_lib exports every live, non-IO def of the whole book, the defs of imported files included (keys like "lib:unused"). Then file_book compiles everything reachable from all of them, and the module exports one export default { ... } object, which bundlers cannot shake.

So a page that uses one def of a large imported file ships all of that file. The guide also says -o f.mjs emits "an ES module of its non-IO defs", but the command exports the imported file's defs too.

We hit this in a game engine written in Bend (opengameworld/engine). A module that uses one clip of a generated 42-clip animation library:

today with this change
bundle, minified 9,071 KB / 1,893 KB gzip 167 KB / 33.5 KB gzip
loader compile (what bun --hot/dev pays per change) 3.1 s 0.76 s
headless Chromium, emulated phone (CPU 4x, 1.6 Mbit/s, 150 ms), until ready 10.2 s 0.52 s

Change

-    return done_live(t) && !t.b && t.x === 0 && io_base(FL.book, t.T) === null;
+    return done_live(t) && !t.b && t.x === 0 && io_base(FL.book, t.T) === null
+      && !k.includes(":");

A module exports only its own defs. This uses the existing convention: bend.ts says ns:name is internal, and cli_verdict already reads !k.includes(":") as "a def of this file". The reachability pass that already exists then keeps only what those exports reach. The whole-program build (-o x.js, rooted at main) is unchanged.

comp.ts must stay under its 66k cap. On current main it is at 65,995 tokens (cl100k_base, the encoding ttok -m gpt-3.5-turbo uses, counted with js-tiktoken). The new condition costs 7 tokens, so if (mod) { FL = null!; } in the same function becomes if (mod) FL = null!;, a form comp.ts already uses. The result is 65,998.

Test

tests/io/bundle_own_exports follows bundle_inline. Its effect builds a page whose module imports a file with an unused def, then checks three things:

  • the build succeeds;
  • the unused def's string is not in the bundle;
  • the bundle runs and the module exports only pick.

It prints 2 on current main and 0 with this change. I ran it interpreted and compiled to JS (-o t.js, then bun). bundle_inline and bundle_page_input still print 0 in both lanes.

Behavior that changes

  1. JS can no longer reach an imported def through the default object. For example, R["lib:used"](). Those keys contain :, so dot access never reached them. A module that wants to expose an imported def re-exports it with one line: def used() -> U32: L.used(). A GitHub code search found 5 public files that import .bend modules, and none of them read such a key.
  2. An imported file's JS effect is no longer pulled in by an unused export. Today a def such as def later() -> Unit -> IO(U32) is exported, because io_base does not see it as IO. It drags its effect file into the page, and that file's top-level code runs at load. With this change, an effect file that nothing reachable uses is not loaded. Base's effs/*.js only call io_eff(...) at load time, so they are unaffected.

Evidence

The full evidence, with reproducible harnesses, is at https://github.com/caiodomingues/poc-bend-treeshake. It was gathered on v2.0.40.

  • Bend's own tests/ corpus, differential in module mode. 1,852 files, 1,237 of them compiled and loaded, 4,605 zero-argument values compared: 0 mismatches. The patch drops 288 imported-def exports.
  • Scenarios. Chains, diamonds, imported types, closures and currying, names such as Place.word, new, __proto__, default, then, __esModule, and effects: same output, except the two cases above.
  • Other environments. The same modules under Node (direct, and through this loader), esbuild, Rollup (ESM and CJS), Vite and webpack all print what released Bend prints. Every one of these bundlers then drops the unused code.
  • The engine above. Its 99 tests pass, and the clip's value is identical (same JSON hash).

An alternative, not in this PR

Keeping the default object as it is and adding a pure named export per own def (under its exact Bend name, as an ES2022 string export name; default and then excluded) breaks nothing for existing importers. It lets import { x } drop even a file's own unused defs. But it costs about 206 tokens, which puts comp.ts over the cap, and it does not speed up compiling. The patch and its tests are in the repo linked above, if that direction is preferred.

Not tested here

  • The C and GPU lanes. This test has no C twin, like bundle_inline.
  • The cluster gates (gates/test.ts, gates/perf.ts).
  • ttok itself. The count above uses the same encoding through js-tiktoken. With 2 tokens of room, the repo gate on this PR is the real check.

In module mode (the bun plugin, `bend file.bend -o f.mjs`), js_lib
exported every live def of the whole book, imported files included,
under keys like "lib:unused". Every def was a root, so a page that used
one def of a large file shipped all of it, and the guide's "an ES module
of its non-IO defs" did not hold. Imported defs now stay internal; the
existing reachability pass keeps only what the module's own defs reach.

comp.ts stays under its cap: `if (mod) FL = null!;` drops a brace block.

tests/io/bundle_own_exports builds a page whose module imports a file
with an unused def, and checks the bundle leaves it out and runs.
…ment

A module now exports only its own defs, so module_export_keys checks that
the imported dep:value stays internal while the root dep.value and the
prototype-named keys are still exported. The js_lib comment says the same
in one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Lorenzobattistela
Lorenzobattistela merged commit 6b80619 into bendlang:main Nov 23, 2026
1 check passed
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Nov 24, 2026
, bendlang#2224-bendlang#2226, bendlang#2231, bendlang#2285, bendlang#2286, bendlang#2288 and others)

One conflict, in emit_unfold: bendlang#2288 annotates an unfolded call's lets with
the argument types instantiated by the earlier arguments; bendlang#2162's deep unfold
keeps its arguments unannotated there and annotates them the same way after
folding (its type walk renamed D, beside bendlang#2288's T). Both kept.

Co-Authored-By: Claude Opus 5.5 <[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.

2 participants