Skip to content

perf: initialize only the core modules leanchecker-paranoid needs - #14936

Merged
Kha merged 1 commit into
masterfrom
leanchecker-minimal-init-simple
Aug 27, 2026
Merged

Kha merged 1 commit into
masterfrom
leanchecker-minimal-init-simple

Conversation

@Kha

@Kha Kha commented Aug 27, 2026

Copy link
Copy Markdown
Member

This PR shrinks leanchecker-paranoid from 78 MB to 7 MB (18.9 MiB to 2.0 MiB compressed) by not initializing the whole Lean library in a binary that never elaborates Lean source.

lean_initialize calls initialize_Lean, which transitively initializes every module of the library. That makes all of Lean reachable from main, so --gc-sections cannot drop any of it and a statically linked binary carries the elaborator, the tactic frameworks and the language server whatever it uses.

Only src/initialize/init.cpp varies with this, so initialize_minimal compiles a second copy of it under LEAN_MINIMAL_CORE_INIT, listing instead the modules that define the exported Lean functions the C++ side calls. That list is derived from the symbols libleancpp.a and libleanrt.a reference but do not define, which is exactly what this binary links. The archive is listed ahead of libleancpp.a so that its lean_initialize wins, the same ordering the hardened mimalloc already relies on, and it is EXCLUDE_FROM_ALL so no other target pays for it.

Binaries that call into elaboration (must) still call initialize_Lean, and there would be no size advantage for them not to do so as they are all linked against libleanshared anyway.

This PR shrinks `leanchecker-paranoid` from 78 MB to 7 MB, and from 18.9 MiB to 2.0 MiB compressed, by not initializing the whole Lean library in a binary that never elaborates Lean source.

`lean_initialize` calls `initialize_Lean`, which transitively initializes every module of the library. That makes all of `Lean` reachable from `main`, so `--gc-sections` cannot drop any of it and a statically linked binary carries the elaborator, the tactic frameworks and the language server whatever it uses: dominator analysis of the symbol reachability graph puts 41.55 MB of 43.92 MB attributable `.text` behind that one call. `leanchecker` only replays declarations through the kernel and needs none of it.

Only `src/initialize/init.cpp` varies with this, so `initialize_minimal` compiles a second copy of it under `LEAN_MINIMAL_CORE_INIT`, listing instead the modules that define the exported Lean functions the C++ side calls. That list is derived from the symbols `libleancpp.a` and `libleanrt.a` reference but do not define, which is exactly what this binary links. The archive is listed ahead of `libleancpp.a` so that its `lean_initialize` wins, the same ordering the hardened mimalloc already relies on, and it is `EXCLUDE_FROM_ALL` so no other target pays for it.

It is scoped to this one binary rather than offered as a global option, because `initialize_Lean` with `builtin` set is also what registers the builtin elaborators and attributes, and the modules whose native code the elaborator reaches are decided at run time rather than by any import graph. `lake` natively initializes 1415 of 2367 modules and still executes native code from `Lean.Meta.ExprDefEq`, which is not among them. The approach is therefore sound only for a binary that never elaborates.

Behaviour is unchanged where it matters: across every module of `tests/pkg/leanchecker` this binary produces exit codes identical to `leanchecker`, so detection is unaffected. Nine of ten outputs are byte-identical; the tenth differs only in that expressions inside an error message print in raw rather than delaborated form, delaboration needing the Meta stack the binary no longer carries.
@Kha

Kha commented Aug 27, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Aug 27, 2026 •

Copy link
Copy Markdown

Benchmark results for 00b7798 against 31aea96 are in. There are significant results. @Kha

  • 🟥 build//instructions: +2.0G (+0.02%)

Large changes (1✅)

  • ✅ size/install//bytes: -71MiB (-2.19%)

Small changes (2🟥)

  • 🟥 elab/bv_stress_replicate//instructions: +358.7M (+2.19%)
  • 🟥 size/all/.cpp//lines: +67.0 (+0.23%)

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Aug 27, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 31aea96d017b53c6acf56577b20e4ef48421af9e --onto b9c9eb9a3acefaa242c1da2a31e3723e94fde3a5. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-27 09:07:02)

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 31aea96d017b53c6acf56577b20e4ef48421af9e --onto b9c9eb9a3acefaa242c1da2a31e3723e94fde3a5. You can force reference manual CI using the force-manual-ci label. (2026-08-27 09:07:04)

@Kha
Kha added this pull request to the merge queue Aug 27, 2026
Merged via the queue into master with commit e991a05 Aug 27, 2026
17 checks passed
@nomeata nomeata mentioned this pull request Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants