build: enable safe ICF to shrink the binary#34478
Merged
Merged
Conversation
littledivy
approved these changes
May 29, 2026
2 tasks
jdalton
added a commit
to SocketDev/socket-btm
that referenced
this pull request
Jun 1, 2026
Apply the link-time Identical Code Folding optimization from [deno#34478](denoland/deno#34478) where it actually fits node-smol's toolchain. ICF folds byte-identical functions at link time. Deno measured ~4.4% on a Rust binary via lld --icf=safe; the technique is linker-level, so it ports to node-smol's C++ output. Mapping it to the real per-platform linkers: - Windows (MSVC link.exe): set OptimizeReferences + EnableCOMDATFolding (/OPT:REF + /OPT:ICF) on the Release VCLinkerTool. Release implies these already; making them explicit keeps the behavior durable, matching the durability fix Deno made for its MSVC path. Debug keeps incremental linking untouched. - macOS (ld64/ld-prime): already folds in release; no flag needed. - Linux (bfd ld): node-smol never selects gold or lld, and bfd has no ICF. Getting it there needs a linker switch, deferred pending a measurement that justifies the toolchain risk. Assessment + measurement recipe documented in packages/node-smol-builder/docs/binary-size-icf.md.
zanieb
added a commit
to astral-sh/uv
that referenced
this pull request
Jun 9, 2026
Inspired by denoland/deno#34478 Use `--icf=safe` for optimized macOS release artifacts, via Rust's bundled Mach-O LLD. Measurements showed useful size wins on macOS; Linux did not show a meaningful artifact-size improvement and are excluded here. See #19615 (comment)
littledivy
pushed a commit
to crowlKats/deno
that referenced
this pull request
Jun 10, 2026
…#34478) Enables **safe identical code folding (ICF)** at link time across platforms. Rust emits a large number of identical machine-code functions (monomorphized `drop` glue, `Debug` impls, closures, generic instantiations); folding the identical copies shrinks the binary and lowers resident memory. ### What - **macOS (aarch64 + x86_64)** and **Linux (x86_64 + aarch64)**: `lld --icf=safe` - mac via `.cargo/config.toml`; Linux via the CI `RUSTFLAGS`/`RUSTDOCFLAGS` in `ci.ts` (CI sets `RUSTFLAGS`, which overrides `config.toml`, and guarantees `lld`) - **Windows (MSVC)**: `/OPT:ICF`, scoped to non-debug builds (rustc already enables `/OPT:REF,ICF` for optimized builds; set explicitly to make it durable) `--icf=safe` only folds functions whose addresses are *not* significant (via address-significance tables), so function-pointer identity is preserved — unlike `--icf=all`. ### Numbers (measured on `aarch64-apple-darwin`, `release` profile) | metric | before | after | Δ | |---|---|---|---| | stripped binary (`strip -x -S`) | 84.83 MB | 78.84 MB | **−5.99 MB (−7.0%)** | | `__TEXT` segment | 80.6 MB | 74.7 MB | −6.2 MB | | idle RSS (`Deno.serve` idle, median of 6) | ~60.6 MB | ~54.6 MB | **−6.0 MB (~10%)** | Idle RSS measured via `Deno.memoryUsage().rss` after a 1s settle; spreads were tight (±0.3 MB), so the delta is well clear of noise. ### Notes - Numbers above are arm64 macOS. The same mechanism applies on the other platforms; exact savings will vary (fat-LTO/CGU=1 release already folds some duplicates, but ICF still folds more — the win held and even grew vs. `release-lite` in testing).
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.
Enables safe identical code folding (ICF) at link time. Rust emits a large number of identical machine-code functions (monomorphized
dropglue,Debugimpls, closures, generic instantiations); folding the identical copies shrinks the binary.What
lld --icf=safe.cargo/config.toml; Linux via the CIRUSTFLAGS/RUSTDOCFLAGSinci.ts/OPT:ICF, non-debug only (rustc already enables/OPT:REF,ICFfor optimized builds; set explicitly to make it durable)--icf=safeonly folds functions whose addresses are not significant (via address-significance tables), so function-pointer identity is preserved.Numbers (aarch64-apple-darwin,
release, stripped, fullcargo build)__textDeterministic (static section sizes), measured via a real full build — the way the binary ships.
Note on idle RSS
ICF's idle-RSS effect is ~1 MB (within noise); this change is justified by binary size, not RSS. (An earlier revision of this description claimed larger RSS and size wins — both were measurement artifacts: incremental-build layout noise for RSS, and a
cargo rustcrelink folding__const~2.3MB more than a full build for size. The numbers above are the corrected, full-build values.)