leanchecker: search path is rescanned for every target, and memory grows with the number of threads
Prerequisites
- [x] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
- [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
- [x] Test your test case against the latest nightly release (4.38.0-nightly-2026-11-20, also v4.37.0-rc1).
- [ ] When reporting a perceived soundness bug: Does it affect
lake check --paranoid? N/A: not a soundness report.
Description
Two things in leanchecker that only hurt on big projects.
First, every target rescans the whole search path. In checkOlean the scan is inside the loop over targets:
for target in targets do
let mut found := false
for path in (← SearchPath.findAllWithExt sp "olean") do
if let some m := (← searchModuleNameOfFileName path sp) then
Second, every matching module is started as a task (IO.asTask (replayFromImports m)), and each extra worker thread adds roughly one copy of the import closure to resident memory. More threads also make the run slower.
Context
I hit this replaying a 175,187-module project on v4.33.1 (https://alpo.ge/lean.tar.gz.part01 ... part14). A single target took 55 s there, against about 3 s for the replay itself, and with 16 threads on a 123 GiB machine every large prefix was OOM-killed within a few minutes. One module per invocation is not practical either, because each invocation pays for the scan.
Steps to Reproduce
- Build a project with no dependencies: a chain of 2,000 trivial modules, 20 modules on top of it, and 20 modules that
import Lean.
mkdir repro && cd repro
echo 'leanprover/lean4:nightly' > lean-toolchain
cat > lakefile.toml <<'EOF'
name = "repro"
defaultTargets = ["Chain", "Leaf", "Core"]
[[lean_lib]]
name = "Chain"
[[lean_lib]]
name = "Leaf"
[[lean_lib]]
name = "Core"
EOF
mkdir Chain Leaf Core
printf 'def c0001 : Nat := 1\n' > Chain/M0001.lean
for k in $(seq 2 2000); do
printf 'import Chain.M%04d\n\ndef c%04d : Nat := c%04d + 1\n' $((k-1)) $k $((k-1)) > $(printf 'Chain/M%04d.lean' $k)
done
echo 'import Chain.M2000' > Chain.lean
for i in $(seq -w 1 20); do
printf 'import Chain.M2000\n\ntheorem leaf_%s : c2000 = c2000 := rfl\n' $i > Leaf/L$i.lean
echo "import Leaf.L$i" >> Leaf.lean
printf 'import Lean\n\ntheorem core_%s : (%d : Nat) + 0 = %d := rfl\n' $i $((10#$i)) $((10#$i)) > Core/C$i.lean
echo "import Core.C$i" >> Core.lean
done
lake build
- Replay the same 21 modules as one target, then as 21 targets:
export LEAN_NUM_THREADS=1
time lake env leanchecker Leaf
time lake env leanchecker Leaf Leaf.L01 Leaf.L02 ... Leaf.L20
- Replay the 21 modules that
import Leanwith different thread counts:
for k in 1 2 4 8 16; do LEAN_NUM_THREADS=$k lake env /usr/bin/time -v leanchecker Core; done
Expected behavior: the cost of finding targets does not depend on how many there are, and memory is not multiplied by the thread count.
Actual behavior (nightly, 16 vCPU / 61 GiB, 4,850 .olean files in the search path):
| Step 2 | wall | readlink calls (strace -f -c) |
|---|---|---|
| 1 target | 2.5 s | 228,583 |
| 21 targets | 6.3 s | 4,800,243 |
| Step 3, threads | max RSS | wall |
|---|---|---|
| 1 | 1.0 GiB | 5.5 s |
| 2 | 1.5 GiB | 5.0 s |
| 4 | 4.1 GiB | 5.0 s |
| 8 | 9.5 GiB | 10.4 s |
| 16 | 19.7 GiB | 14.7 s |
The toolchain's compiled library is 1.78 GiB, so that is about 1.2 GiB per extra thread. v4.37.0-rc1 gives the same numbers.
Versions
Lean 4.38.0-nightly-2026-11-20 (commit 82bff16a3463) and 4.37.0-rc1, Ubuntu 24.04 x86-64. The large project was on v4.33.1.
Additional Information
My guess at the memory growth: a compiled module can only be mapped once per process, so every task after the first holds private copies of its imports. Separate processes do not have the problem (about 1.3 GiB each in my runs). With Mathlib as the closure on v4.37.0-rc1 it is 5.2 GiB at 1 thread and 45 GiB at 8.
What would help: scanning once before the loop, a limit on concurrent replays (or worker processes), and a way to pass an explicit list of modules. I worked around it with a small driver that calls replayFromImports on a module list, one process per CPU.
Related: #14450, leanprover/lean4checker#75, leanprover/lean4checker#82.
Impact
Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.