Skip to content

Smaller programs: per-function sections, the linker drops what is not called - #831

Merged
ASDAlexander77 merged 1 commit into
mainfrom
binary-size
Nov 12, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
binary-size

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Why programs were big

A program that called one default-library function linked all of lib.ts, and through its references the RegExp (std::regex), Date and HTTP wrappers. tslang emitted each module as one section, so the linker could not take part of it. On wasm, --export-all rooted every default-library function as well.

Changes

  • obj.cpp: function and data sections on by default (like clang's -ffunction-sections -fdata-sections). An explicit --function-sections / --data-sections on the command line still wins.
  • exe.cpp, Windows: -opt:noicf. link.exe already drops unreferenced COMDATs (/OPT:REF is its default without /DEBUG). With per-function COMDATs it would also fold functions with identical code; I saw two TS functions get one address. Function identity has to hold, so ICF is off. It costs about 2%.
  • exe.cpp, ELF: -Wl,--gc-sections. Exported symbols, --export-dynamic-symbol=GC_* and symbols referenced by DSOs stay. For an -mm=own program, the dynamic export table, including mimalloc's malloc family, is identical with and without the flag.
  • exe.cpp, wasm: a program exports what host.mjs uses (main/__main_void/__original_main, __wasm_call_ctors, __heap_base, __tls_size, __wasm_init_tls, the table), plus its own exported functions. ExportFixPass now gives those a wasm-export-name. A library keeps --export-all.

Goes with ASDAlexander77/TypeScriptCompilerDefaultLib binary-size: wrappers are no longer dllexported from the static lib, and the release wrappers are now built with /O2 /Gy /Gw. Either PR works without the other.

Measured

--opt, a program using Map, sort, toString and toUpperCase:

before after
Windows -mm=own 831 KB 437 KB
Windows -mm=gc 772 KB 409 KB
Linux -mm=own 621 KB 177 KB
Linux -mm=gc 591 KB 235 KB
wasm32 -mm=own 196 KB 18.6 KB

What remains on Windows is mostly the static CRT (~200 KB) and mimalloc (~120 KB), both by design. The default library's share went from ~500 KB to ~32 KB.

Tests

  • Windows: ctest 6976/6976.
  • DefaultLib tests.ps1, all four models × release/debug × compile/jit: all pass, declarations match. Run against the DefaultLib branch built with this compiler.
  • wasm: run_tests.mjs tester + defaultlib suites, wasm32/wasm64 × none/rc/own, release and --debug: 0 failures.
  • Linux (WSL): DefaultLib tests.sh, all models. Only weakref_basic gc release fails, which it does on every run with the locally built libgc.a. Linux ctest was not run locally.

🤖 Generated with Claude Code

…s what is not called

A program that used one default-library function linked all of lib.ts, and through its
references the RegExp, Date and HTTP wrappers. tslang emitted each module as one section, so
no linker could take part of it.

- obj.cpp: function and data sections on by default (--function-sections / --data-sections on
  the command line still win), as clang's -ffunction-sections -fdata-sections.
- exe.cpp, Windows: -opt:noicf. link.exe drops unreferenced COMDATs by default, but would also
  fold functions with identical code into one address; two TypeScript functions must stay apart.
- exe.cpp, ELF: -Wl,--gc-sections. What a shared library exports, what the program exports for
  the libraries it loads, and what libc references (mimalloc's malloc family) stay: measured,
  the dynamic export table is identical with and without it.
- exe.cpp, wasm: a program is no longer linked with --export-all, which rooted every
  default-library function. It exports what the JS host starts it with, plus its 'export'ed
  functions, which ExportFixPass now names (wasm-export-name). Libraries keep --export-all.

Measured (--opt, a program using Map, sort, toString, toUpperCase):
  Windows -mm=own 831 KB -> 437 KB, -mm=gc 772 KB -> 409 KB (with DefaultLib's matching change)
  Linux   -mm=own 621 KB -> 177 KB, -mm=gc 591 KB -> 235 KB
  wasm32  -mm=own 196 KB -> 18.6 KB

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 3a1227a into main Nov 12, 2026
11 of 12 checks passed
@ASDAlexander77
ASDAlexander77 deleted the binary-size branch November 12, 2026 20:39
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.

1 participant