Repository navigation
perf: initialize only the core modules leanchecker-paranoid needs - #14936
Merged
Merged
Conversation
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.
Member
Author
|
!bench |
|
Benchmark results for 00b7798 against 31aea96 are in. There are significant results. @Kha
Large changes (1✅)
Small changes (2🟥)
|
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
Closed
3 tasks done
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.
This PR shrinks
leanchecker-paranoidfrom 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_initializecallsinitialize_Lean, which transitively initializes every module of the library. That makes all ofLeanreachable frommain, so--gc-sectionscannot 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.cppvaries with this, soinitialize_minimalcompiles a second copy of it underLEAN_MINIMAL_CORE_INIT, listing instead the modules that define the exported Lean functions the C++ side calls. That list is derived from the symbolslibleancpp.aandlibleanrt.areference but do not define, which is exactly what this binary links. The archive is listed ahead oflibleancpp.aso that itslean_initializewins, the same ordering the hardened mimalloc already relies on, and it isEXCLUDE_FROM_ALLso 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 againstlibleansharedanyway.