Repository navigation
Smaller programs: per-function sections, the linker drops what is not called - #831
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-allrooted every default-library function as well.Changes
-ffunction-sections -fdata-sections). An explicit--function-sections/--data-sectionson the command line still wins.-opt:noicf. link.exe already drops unreferenced COMDATs (/OPT:REFis 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%.-Wl,--gc-sections. Exported symbols,--export-dynamic-symbol=GC_*and symbols referenced by DSOs stay. For an-mm=ownprogram, the dynamic export table, including mimalloc's malloc family, is identical with and without the flag.host.mjsuses (main/__main_void/__original_main,__wasm_call_ctors,__heap_base,__tls_size,__wasm_init_tls, the table), plus its ownexported functions. ExportFixPass now gives those awasm-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:-mm=own-mm=gc-mm=own-mm=gc-mm=ownWhat 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
tests.ps1, all four models × release/debug × compile/jit: all pass, declarations match. Run against the DefaultLib branch built with this compiler.run_tests.mjstester + defaultlib suites, wasm32/wasm64 × none/rc/own, release and--debug: 0 failures.tests.sh, all models. Onlyweakref_basicgc release fails, which it does on every run with the locally built libgc.a. Linux ctest was not run locally.🤖 Generated with Claude Code