Lake: accumulate wide module import traces without quadratic prefix copies

Open
#16,957 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

Lake's fetchImportInfo folds a Job.zipWith over a module's imports. Each
preceding job can still own its result when the next job adds two entries to
the import trace. In that case Array.push copies the existing prefix. For a
module with many direct imports the total copy work can grow quadratically.

Keep the two growing import traces' inputs in private reverse lists during the
fold. Update the hash in the original left-to-right order, take the same
maximum mtime, and keep the existing caption and nested traces. Convert each
list into an array in import order when the completed value is published. The
same job combinators still handle errors, cancellation, logs and job-state
traces.

Reproduction

The following Bash fixture uses only Lean and Lake. Run it from either a stock
Lean 4.36.0 installation or an SDK built at the same revision with the patch.
The first build primes the artifacts. Time subsequent no-op builds using
the same files, toolchain path, thread count and machine.

set -eu
dir=$(mktemp -d)
trap 'cd /; rm -rf -- "$dir"' EXIT
cd "$dir"
export LEAN_NUM_THREADS=8
cat >lakefile.toml <<'TOML'
name = "wide"
defaultTargets = ["Wide"]

[[lean_lib]]
name = "Wide"
TOML
mkdir Wide
printf 'module\n' >Wide.lean
for i in $(seq 1 2048); do
  printf 'public import Wide.M%s\n' "$i" >>Wide.lean
  printf 'module\n\npublic def m%s : Nat := %s\n' "$i" "$i" >"Wide/M$i.lean"
done
lake --offline --no-cache build
time -p lake --offline --no-cache build

Validation

On Lean 4.36.0, a stock warm build of the Mathlib umbrella (8,691 direct import
lines) recorded 25,441 non-exclusive trace array pushes with a prefix of at
least 512 elements. The calls processed 189,727,488 existing reference slots and
used 2.259 CPU seconds. This count includes both the import-info traces and the
job-state trace. The change here addresses the import-info traces.

A differential test compared all seven trace fields and direct artifacts
against the original fold over 4,096 import entries. It covered modules and
legacy sources, repeated providers, mixed visibility, meta and import-all
flags, and non-monotonic mtimes.

An end-to-end test against the stock Lean 4.36.0 release used a matched
Stage2 Release build containing this patch and the CRLF fast path. It used a
small project importing the full Mathlib umbrella at
fc4f59e577feb1a4956671bc04ac18aaaf8cd842, with dependencies prebuilt. Each
row below reports the means from a stock–candidate–candidate–stock sequence
on the same Linux x86-64 host with eight Lake threads.

Workload Metric Stock Combined candidate
Warm no-op Elapsed time 6.571 s 5.030 s (23.5% lower)
Warm no-op Lake CPU time 13.380 s 11.437 s (14.5% lower)
One-line edit Time to first compiler fork 6.523 s 4.927 s (24.5% lower)
One-line edit Elapsed time 9.455 s 7.813 s (17.4% lower)
Cold root module Elapsed time 9.104 s 7.880 s (13.5% lower)

These timings measure the combined candidate. Both SDKs generated
byte-identical traces and all seven compiler outputs for the root module and
for a separate rebuild of the Mathlib umbrella. The regenerated Mathlib
dependency hash also matched that of the prebuilt entry. A real mixed-import
fixture produced the same artifacts, and duplicate providers with distinct
definitions produced the same error, provider order, hashes, and exit code.

(Confirmed still present on master at 3158840c in src/lake/Lake/Build/Module.lean:477.)

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.