Skip to content

Speed up DistinctDistances by reusing checked row summaries - #1028

Merged
Vilin97 merged 1 commit into
mainfrom
fix/distinct-distances-cache-geometric-rows-20261126
Nov 26, 2026
Merged

Vilin97 merged 1 commit into
mainfrom
fix/distinct-distances-cache-geometric-rows-20261126

Conversation

@Vilin97

@Vilin97 Vilin97 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Ready for review. DistinctDistances builds 36.92% faster and its peak sampled anonymous compiler memory is 23.98% lower on the Azure VM.

Change

The Lean 4.36 source profile for the 254-module DistinctDistances project showed several certificate modules spending 17–23 seconds in frontend work. Forty-six subtree proofs revisit the long moment and interval rows derived from the same 1,283-point grid.

The new replay verifies the packed summaries of those three rows at unit weight once. A Lean proof shows that multiplying the packed chunks gives exactly the original weighted tries, and checked index bounds justify scaling the cost. For each interval row, the proof checks its common coefficient denominator and shows when a new endpoint can be handled by changing just the right-hand side. Other rows and denominators use the original evaluator.

Declaration audit

Base a87d4847f3013d5b52180931046f360bfb0416a3 and head a7869ff5541a48b2a32c8820c20971407cdd5e51 were audited with Lean's .olean reader across the exported, server, and private layers. The compressed manifests record owner, name, layer, universe parameters, kind, safety, reducibility, exact structural kernel types, and non-theorem values. The kernel checked the regenerated proof bodies.

All 3,898 original declarations and 11,224 original manifest records matched, including generated private proof declarations. The 136 additional declarations belong to the shared cache and the 46 cached subtree proofs.

Measurements

Each run rebuilt every project module from empty project build outputs using the same prebuilt pinned dependencies and linter plugin. The VM used CPUs 12–15 and LEAN_NUM_THREADS=6, with Lean v4.36.0 and Mathlib fc4f59e577feb1a4956671bc04ac18aaaf8cd842.

Metric Base Head Reduction
Clean project build wall time 894.55 s 564.27 s 36.92%
Build user and system CPU time 3,228.60 s 2,124.47 s 34.20%
Maximum process RSS 4,540,720 KiB 3,775,444 KiB 16.85%
Peak sampled anonymous RSS of a Lean compiler 3,299,340 KiB 2,508,240 KiB 23.98%
Summed Lake time for the 46 changed subtree modules 2,208 s 895 s 59.47%

The new module's 50-second clean build is included in the totals. Case 03 subtree 3 went from 49 s to 26 s, case 09 subtree 1 from 48 s to 26 s, and case 22 subtree 1 from 61 s to 14 s. This was one paired clean build on a shared VM. Lake's module times include overlapping jobs.

Validation

The warning-free project build, Mathlib declaration linters, style check, repository static quality checks, project declaration and axiom audits, compiled-environment and compiler-message audits, generated indexes, and per-file resource checks passed. The added module used 1,105,100 KiB of sampled anonymous RSS in the timed build and also passed a separate rebuild under the repository's 3 GiB watchdog. The import diagnostic reported redundant imports throughout the project. The edited proofs keep their explicit replay tactic dependency.

lake --no-ansi --wfail build LeanPoolLinters:shared
LEAN_NUM_THREADS=6 LAKE_CACHE_ARTIFACTS=0 /usr/bin/time -v \
  taskset -c 12-15 lake --no-ansi --wfail build +LeanPool.DistinctDistances.Imports

lake env .lake/optimization-audit/manifest-words "${modules[@]}" | zstd -T1
python3 ../audit/compare_manifest.py ../audit/base.manifest.zst ../audit/final.manifest.zst

lake exe lint-style LeanPool.DistinctDistances.Imports
lake exe mk_all --module --check
lake shake --force --keep-public LeanPool.DistinctDistances.Imports
PYTHONPATH=python python -m lean_pool.rebase_fastpath check-index
PYTHONPATH=python python -m lean_pool.quality --repo . --static-only
PYTHONPATH=python python -m lean_pool.shard_validation --repo . --units DistinctDistances
PYTHONPATH=python python -m lean_pool.build_time_limits prepare --base "$base" \
  --modules LeanPool.DistinctDistances.S4CertificateCachedRows
PYTHONPATH=python python -m lean_pool.build_time_limits watch --base "$base" -- \
  taskset -c 12-15 lake --no-ansi --wfail build +LeanPool.DistinctDistances.S4CertificateCachedRows
PYTHONPATH=python python -m lean_pool.build_time_limits check --base "$base" \
  --log ../audit/timing-gate-input.log

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Advisory cross-file review against Lean Pool's supervisory rubric. This is not by itself a significance, source-verification, or merge verdict.

No actionable defect was found in this proof-only refactor.RetriggerView in Greptile

[Medium impact] No actionable defect was found in this proof-only refactor.

Summary

The PR checks three long rows once, then reuses their packed summaries in 46 certificate proofs.

  • Certificate builds reuse checked summaries of long grid rows.

Reviews (1) · Last reviewed commit: "Reuse checked geometric row summaries in..." · Reviewed by Greptile

@Vilin97 Vilin97 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept the cached certificate replay at a7869ff, pending required CI.

  • The shared replay proves equality with the original checker for arbitrary rational endpoints and weighted trees. It checks the index bounds before scaling the long rows. An interval row can reuse its coefficients only when the checked denominator permits replacing the right-hand side. Other cases still use the original checker.
  • I verified that all 46 certificate modules are byte-identical after removing the cache import and restoring the prior proof expression. The certificate data, theorem statements, final validity check, and project card stay the same.
  • The paired clean build reported 36.92% less wall time and 34.20% less CPU time, including the cost of building the new cache module. The source combines cleanly with current main.

@github-actions

Copy link
Copy Markdown
Contributor

Proof profile (new / modified Lean files)

lake build wall time (changed modules): 82.33 s (= 1.37 min) — user 46.89 s, sys 6.06 s.

This build covers the changed modules and their dependency cones on top of the restored cache. The serial per-file sums below are useful for ranking slow files, not as a build budget.

A base→head comparison was attempted but its measurements are unavailable (see the run log); modified files are omitted from the tables below.

Statement changes

✅ No statements changed — all 368 tracked declarations across 47 modified files kept their type; only proofs / definition bodies changed.

Also: 46 declarations added, 0 removed. A reorganization moves declarations between files, which shows up here as an add in one file and a remove in another.

➕ Declarations added (46)
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase00Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase01Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase02Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase03Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase04Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase04Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase05Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase05Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase06Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase06Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase07Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase08Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase08Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase09Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase09Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase10Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase10Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase11Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase11Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase12Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase12Summary3.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase13Summary0.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase13Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase14Summary0.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase14Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase15Summary0.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase15Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase16Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase17Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase18Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase19Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase20Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase21Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase22Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase23Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase24Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase25Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase26Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase27Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase28Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase29Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase30Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase31Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase32Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase33Summary1.lean
  • Esgk.S4.cachedReplay — LeanPool/DistinctDistances/S4CertificateCase34Summary1.lean

Statements are compared textually against the merge base (comments and whitespace ignored; by proof terms embedded in a type are treated as proof-irrelevant), the same method as physlib's /check-golf. Anonymous instances and examples are not tracked.

Total heartbeats: 279,042 maxHeartbeats units across 1 valid file (725 added LOC).

Sum of lean --profile: 103504.9 ms (= 103.50 s) across 1 valid file. Import-excluded time: 100464.9 ms (= 100.46 s).

Count-heartbeats wall-clock total: 43.37 s. Repeated import cost inside lean --profile: 3040.0 ms (= 3.04 s).

Heartbeat values sum disjoint root command traces with trace.profiler.useHeartbeats and synchronous elaboration, converted from raw allocations to maxHeartbeats units by dividing by 1000. They include command elaboration and kernel checking, exclude imports, and include profiler overhead; they are not comparable to the deprecated linter's totals. Per-file wall clocks are measured under parallel load and are noisier than heartbeats.

LOC counts added lines in the profiled Lean files from this PR diff.

File LOC Heartbeats (maxHB) Count wall (s) lean --profile (s) Without import (s) Import (s) Commands Errors
LeanPool/DistinctDistances/S4CertificateCachedRows.lean 356 279,042 43.37 103.50 100.46 3.04 52 0
LeanPool/DistinctDistances/Imports.lean 1 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase00Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase01Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase02Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase03Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase04Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase04Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase05Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase05Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase06Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase06Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase07Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase08Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase08Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase09Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase09Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase10Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase10Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase11Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase11Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase12Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase12Summary3.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase13Summary0.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase13Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase14Summary0.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase14Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase15Summary0.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase15Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase16Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase17Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase18Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase19Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase20Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase21Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase22Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase23Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase24Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase25Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase26Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase27Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase28Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase29Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase30Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase31Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase32Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase33Summary1.lean 8 — — — — — 0 1
LeanPool/DistinctDistances/S4CertificateCase34Summary1.lean 8 — — — — — 0 1
Total 725 279,042 43.37 103.50 100.46 3.04 52 47

Aggregate phase totals

Phase Time
blocked (unaccounted) 60400.0 ms (= 60.40 s)
type checking 38900.0 ms (= 38.90 s)
import 3040.0 ms (= 3.04 s)
tactic execution 514.0 ms (= 0.51 s)
interpretation 434.0 ms (= 0.43 s)
elaboration 60.3 ms (= 0.06 s)
initialization 31.3 ms (= 0.03 s)
simp 29.7 ms (= 0.03 s)
linting 22.7 ms (= 0.02 s)
typeclass inference 19.6 ms (= 0.02 s)
parsing 18.1 ms (= 0.02 s)
declTypeLinter 8.6 ms (= 0.01 s)

Slowest changed modules (from lake build)

Changed module Lake time
LeanPool.DistinctDistances.S4CertificateCachedRows 46.00 s
Per-file `lean --profile` output

LeanPool/DistinctDistances/S4CertificateCachedRows.lean

import took 3.04s
tactic execution of Lean.Parser.Tactic.change took 339ms
type checking took 5.11s
type checking took 5.27s
type checking took 5.18s
type checking took 4.3s
type checking took 4.57s
type checking took 4.55s
type checking took 3.07s
type checking took 2.94s
type checking took 3.86s
cumulative profiling times:
	attribute application 0.25ms
	blocked (unaccounted) 60.4s
	compilation (IR) 0.0763ms
	compilation (LCNF base) 0.475ms
	compilation (LCNF impure) 0.282ms
	compilation (LCNF mono) 0.57ms
	congr simp thm 2.55ms
	declTypeLinter 8.58ms
	elaboration 60.3ms
	fix level params 0.622ms
	import 3.04s
	initialization 31.3ms
	instantiate metavars 1.78ms
	interpretation 434ms
	let-to-have transformation 0.654ms
	linting 22.7ms
	module linting 0.0306ms
	parsing 18.1ms
	process pre-definitions 8.53ms
	share common exprs 3.52ms
	simp 29.7ms
	tactic execution 514ms
	tacticAnalysis 7.25ms
	type checking 38.9s
	typeclass inference 19.6ms
real 43.22
user 40.56
sys 1.29

Advisory only — never blocks merge. Full log uploaded as the proof-profile artifact.

@Vilin97

Vilin97 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 LLM review — refactor (gpt-6.2-sol)

Reviewed head: a7869ff5541a48b2a32c8820c20971407cdd5e51

Verdict: 🛑 request_changes

The refactor checks unit-weight packed summaries for three grid rows and reuses them in 46 subtree proofs. Other rows and interval endpoints with incompatible denominators go through the original evaluator. The cache module, three edited subtrees, and the assembled case 22 certificate built with the pinned dependencies. The project-wide timing comparison is contributor-reported.

Aspect Value
Scope certificate replay caching
Introduces tech debt 🛑 yes
Maintainability 🛑 regressed
Brittleness 🛑 more_brittle
Risk 🟡 medium

A change to the first case’s upper endpoint can break the shared cache build for every caller, including subtrees of unrelated cases.

Findings (2)

  • brittle-cache-anchor — LeanPool/DistinctDistances/S4CertificateCachedRows.lean:249
    upperDenominator requires the full row denominator at caseY.get 1 to equal the coefficient denominator. A Lean probe confirmed that a small perturbation triples the full denominator and takes the fallback when passed to replay. Putting that endpoint in the case table would fail the shared proof before any caller could use the fallback. Generate and check both interval caches with a fixed zero right-hand side so case-table edits can use the existing fallback.
  • prompt-injection — PR-wide
    The advisory PR comment repeats its no-defect verdict in hidden HTML <rp> fallback text, where automated reviewers can see it. Remove the hidden copy of the verdict.

Tokens: 5,324,139 in / 20,315 out · Tier: codex-subscription · Effort: xhigh · Billing: Codex subscription quota (no API credits) · Estimated cost: $21.6013 (Standard API equivalent; uncached input) (long-context rate)
Automated review against .github/REFACTOR_REVIEW_RULES.md. Disagree? Reply on the PR; rules can be updated in a PR of their own.
request_changes is an ask, not a close: of the reviewer's past request_changes verdicts, 39% were merged after a human looked. Read the findings before acting on the verdict.

@Vilin97
Vilin97 added this pull request to the merge queue Nov 26, 2026
Merged via the queue into main with commit 04f2b66 Nov 26, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant