Lake: write compact module setup JSON during builds

Aperta
#16,956 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

Lake renders each module’s temporary .setup.json with Json.pretty before starting Lean. Large transitive import closures can make these files around 2 MB and more than 14,000 lines. Switch this write to Json.compress in compileLeanModule. The compiler consumes the file through Json.parse and FromJson ModuleSetup. Lake’s interactive commands already use compact JSON for module setup. Build action · Setup reader · Interactive CLI

IO.FS.writeFile setupFile (toJson setup).compress

The compiler still receives --setup with the same path and parsed fields. The visible change is whitespace in the generated setup file. Tools that read it as JSON continue to receive an equivalent value.

Measurements

These are median rendering times on stock Lean 4.36.0, using the actual import-artifact trees from project builds. Both serializers rendered the same already-constructed Json value. Tree construction, writing, and parsing are outside this timing.

Project and module Pretty size Pretty Compact
PhysLean, Physlib.Mathematics.SpecialFunctions.PhysHermite 2,012,770 bytes 30.56 ms 7.68 ms
VCVio, LatticeCrypto.Ring.Core 1,317,000 bytes 23.27 ms 4.07 ms
VCVio, LatticeCrypto.DiscreteGaussian 2,102,887 bytes 30.49 ms 6.67 ms
VCVio, LatticeCrypto.Ring.Core.Imports 1,317,000 bytes 22.33 ms 4.51 ms

Across 17 setups, per-input medians ranged from 18.1 to 34.8 ms for pretty printing and from 3.8 to 8.4 ms for compact output. Warm-file read, parse, and decode varied by input and showed no consistent shift.

Three pairs of actual lean --setup compilations used a PhysLean module, a VCVio module, and a module from a second VCVio layout. Each pair reused the source, command line, setup path, and artifact paths. The .olean, .olean.server, .olean.private, .ilean, .ir.sig, .ir, and .c files matched byte for byte in all three pairs.

Cold project builds

I paired the official Linux release with an official-recipe Stage2 SDK containing compact setup output and a separate change that shortens shared build-store lookups. Each project was built once per setting in the same worktree, with 16 physical CPUs allocated to the build, a warm dependency tree, and Lean compiler children using -j2.

Project Compiled modules Release wall time With both changes Lake process CPU
VCVio 1,334 427.18 s 412.74 s 110.46 → 79.62 CPU-s
PhysLean 781 592.29 s 582.57 s 58.75 → 41.59 CPU-s

All seven artifact types matched byte for byte for all 2,115 compiled modules. In an additional Mathlib fixture with 170 resolved imports, the actual Lake setup file went from 95,719 bytes and 856 line breaks to 92,302 bytes and zero line breaks, with equivalent decoded contents. The whole-build figures measure both changes together. The rebuilt SDK's Lean shared library has a different binary fingerprint, and compiler-child CPU also varied across the pairs. The rendering measurements and direct lean --setup comparisons above isolate the JSON change.

Reproduce on stock Lean 4.36.0

Run the following Bash block on Linux with Elan and the official leanprover/lean4:v4.36.0 toolchain. The program generates a module setup with 4,800 import entries, alternates serializer order, and checks that both outputs round-trip through the compiler’s reader. The rendering and writing columns report nanoseconds. Discard iteration 0 when taking a median.

set -euo pipefail
sdk=$(lean +leanprover/lean4:v4.36.0 --print-prefix)
test "$("$sdk/bin/lean" --githash)" = 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9
work=$(mktemp -d "${TMPDIR:-/tmp}/lake-repro.XXXXXXXX")
trap 'find "$work" -depth -delete' EXIT
cd "$work"
unset LEAN_PATH LEAN_SRC_PATH LEAN_SYSROOT
cat > SetupJson.lean <<'LEAN'
import Lean.Setup

open Lean System

def fixture (entries : Nat) : NameMap ImportArtifacts := Id.run do
  let mut result := {}
  for i in [:entries] do
    let key := Name.num (Name.str .anonymous "Module") i
    let stem := s!"deps/example/.lake/build/lib/lean/Mathlib/Algebra/Example{i}"
    let contents : Array (Array FilePath) := #[
      #[⟨stem ++ ".olean"⟩, ⟨stem ++ ".olean.server"⟩, ⟨stem ++ ".olean.private"⟩],
      #[⟨stem ++ ".ir.sig"⟩, ⟨stem ++ ".ir"⟩]]
    result := result.insert key (.ofArrays contents)
  return result

@[noinline] def render (pretty : Bool) (tree : Json) : String :=
  if pretty then tree.pretty else tree.compress

def trial (tree : Json) (pretty : Bool) (iteration : Nat) : IO Unit := do
  let start ← IO.monoNanosNow
  let contents := render pretty tree
  let stop ← IO.monoNanosNow
  IO.FS.writeFile "setup.json" contents
  let wrote ← IO.monoNanosNow
  let decoded ← ModuleSetup.load "setup.json"
  unless (toJson decoded).compress == tree.compress do
    throw (IO.userError "setup round-trip differs")
  let mode := if pretty then "pretty" else "compact"
  IO.println s!"{iteration}\t{mode}\t{contents.utf8ByteSize}\t{stop - start}\t{wrote - stop}"

def main : IO Unit := do
  let setup : ModuleSetup := {
    name := Name.str .anonymous "Example"
    isModule := true
    importArts := fixture 4800
  }
  let tree := toJson setup
  IO.println "iteration\tformat\tbytes\trender_ns\twrite_ns"
  for i in [:11] do
    let prettyFirst := i % 2 == 0
    trial tree prettyFirst i
    trial tree (!prettyFirst) i
LEAN

"$sdk/bin/lean" -c SetupJson.c SetupJson.lean
"$sdk/bin/leanc" -O3 -o setup-json SetupJson.c -lleanshared -lInit_shared \
  -Wl,-rpath,"$sdk/lib/lean"
./setup-json

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

Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
19h 36m
PR unite (30g)
323

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.