Repository navigation
Reduce MetricCodes proof elaboration cost - #691
Conversation
|
Advisory cross-file review against Lean Pool's supervisory rubric. This is not by itself a significance, source-verification, or merge verdict. [Low impact] The proof refactor appears mergeable; no actionable issue was found in the changed code. SummaryThis PR reduces proof elaboration work across 14
Reviews (1) · Last reviewed commit: "Optimize MetricCodes proof elaboration" · Reviewed by Greptile |
Proof profile (new / modified Lean files)Statement changes✅ No statements changed — all 5,368 tracked declarations across 14 modified files kept their type; only proofs / definition bodies changed. Also: 5 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 (5)
Statements are compared textually against the merge base (comments and whitespace ignored; Compile cost of modified files — base → headHeartbeats: 11,019,346 → 10,301,125 (-718,222, -6.5%). Commands: 8,202 → 8,209. Per-file wall sum: 2076.4 s → 1141.4 s (-45.0%). Wall clock is measured under parallel load and is noisy; heartbeats are deterministic and are the regression signal. Files: 11 compared — 8 cheaper, 0 costlier, 3 within noise (threshold: |Δ heartbeats| ≥ 1,000 and ≥ 1%). Both sides are elaborated against the merge-base build of the pool — sound when only proofs and definition bodies change; a file whose statements changed may fail there and is flagged ⚠. Heartbeats come from disjoint root command traces with
New files — absolute profileTotal heartbeats: — maxHeartbeats units across 0 valid files (91 added LOC). Sum of Count-heartbeats wall-clock total: 0.00 s. Repeated import cost inside Heartbeat values sum disjoint root command traces with LOC counts added lines in the profiled Lean files from this PR diff.
Aggregate phase totals
Advisory only — never blocks merge. Full log uploaded as the |
|
/review Exact head: |
Vilin97
left a comment
There was a problem hiding this comment.
{
"summary": "This MetricCodes refactor replaces broad arithmetic search and duplicated proofs with explicit premises, existing named theorems, and shared adjoint and finite-sum arguments. The spanning-range helper and injective linear-map cancellation make the longer arguments easier to follow while retaining the existing written contracts. The reported 3.811% median wall-time reduction is noisy; the advisory profile has 11 valid file comparisons and does not establish a whole-project speedup.",
"assessment": {
"scope": "Proof elaboration refactor in MetricCodes",
"introduces_tech_debt": false,
"maintainability": "improved",
"brittleness": "more_robust",
"risk": "low",
"assessment_one_sentence": "The refactor reduces repeated proof work and unnecessary automation without an actionable maintenance regression."
},
"verdict": "approve",
"findings": []
}
🤖 LLM review — refactor (
|
| Aspect | Value |
|---|---|
| Scope | proof elaboration optimization and helper extraction |
| Introduces tech debt | ✅ no |
| Maintainability | ✅ improved |
| Brittleness | ✅ more_robust |
| Risk | ✅ low |
The refactor makes proof dependencies more explicit and removes duplicated arguments without introducing actionable maintenance debt.
Tokens: 10,324,684 in / 21,859 out · Tier: codex-subscription · Effort: xhigh · Billing: Codex subscription quota (no API credits) · Estimated cost: $41.6266 (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.
CI was not green for this commit at review time; this review does not replace CI.
MetricCodes has expensive arithmetic, calculus, and tensor/linear-map elaboration spread across roughly 112,000 lines and 17 modules. This refactor reduces proof search and repeated elaboration while preserving every original declaration. The observed three-build median wall time is 584.36 → 562.09 seconds (3.811% reduction).
MetricCodes was selected from the large completed-project profiles because its long derivative/arithmetic proofs and repeated adjoint/tensor arguments offered concrete remaining opportunities. Fresh synchronous diagnostic command profiles identified the spectral-margin derivative and several 5–9 second linear-map proofs. Earlier MetricCodes optimizations were already merged before this baseline; this cycle changes exactly this one project.
Base:
b25d6804654bbfd206eb31cdbd6360d1f63d5cd1. Head:216782909c1daee9b3659094e01b6d94d121c27f.The branch was prepared in an isolated worktree. Immediately before committing, current main was fetched at
2aaa7220788257635be774b855528c4f0be6f3bd; it had no intervening changes to this project, repository policies, the toolchain, or dependency pins. The branch was not rebased to chase main. The existing repository pins are Lean4.35.0-rc3and Mathlibc55e6e786f49471c72fbddbec5415808896aec1e.Changes
lake shake --force --keep-public LeanPool.MetricCodes.Importsaudit found no useful dependency-closure reduction that preserved all original private/generated declarations.There are 14 changed Lean files, 900 insertions, and 1,323 deletions. Only the selected project's proof files changed. The project card and generated module indexes require no updates.
Invariant audit
Before editing, an immutable compiled base snapshot and declaration manifest were recorded. Lean environment inspection enumerates selected modules'
constNames, including private and compiler-generated declarations. For non-theorems, the audit compares fullConstantInfowith Lean structural equality, including elaborated bodies, types, universe parameters, safety, and inductive/recursor metadata. For theorems, it compares the exactConstantValand mutual-declaration list, excluding only proof bodies. Compact hashes are an index, not the equality test.The final independent cold build was audited again: 10,381 original declarations, 10,396 head declarations, 15 additions, zero changed or removed original declarations. Module ownership and the complete compact manifests also agree for every original. All 51 immutable base compiled-artifact checksums remain unchanged. The additions comprise five optimization helper theorems and ten generated theorem auxiliaries. Existing declaration statements, definitions, names, universes, and safety are preserved; source review also confirms existing notation and declaration attributes were not edited.
Measurements
Three complete clean-project builds per revision, with identical warm pinned dependencies, ordinary repository Lake settings, and no concurrent compiler/profiler jobs from this optimization task. All 17 project modules were freshly rebuilt on every run. Build order: base/head, head/base, base/head. The benchmark used independent build directories and deleted selected-project compiled artifacts before each run.
/usr/bin/time -f 'wall_seconds=%e peak_rss_kib=%M user_seconds=%U system_seconds=%S' \ lake --wfail build LeanPool.MetricCodes.ImportsMedian wall time: 584.36 → 562.09 seconds (3.811% reduction). Median peak single-process RSS: 2,144,632 → 2,126,500 KiB (0.845% reduction). Median user CPU: 934.59 → 889.45 seconds (4.830% reduction).
The requested 10% wall-time or RSS target was not met. Optimization continued through the six-hour alternative stopping condition (2026-10-19 21:15:09 through 2026-10-20 03:15:09 UTC). The measurements do not establish the requested 10% improvement.
Diagnostic proof elaboration examples, separate from the wall-time result: the weighted differential square-zero command decreased from 51,945 to 2,018 heartbeats; the root exterior bracket square command decreased from 110,077 to 45,938 heartbeats. These local figures do not establish a whole-project percentage.
Supplementary scheduling control
After all six ordinary native builds completed, eight proof-heavy changed modules were compiled in adjacent base/head pairs with
lake env lean --threads=4 --setup ... --json -o ... -i ... -c ... SOURCE, alternating pair order by module. Each compiler invocation was timed independently with GNU time. This is one diagnostic pair per selected module, with warm pinned dependencies and fresh outputs; its summed times cover only this subset and are not a whole-project build result.Subset summed wall: 389.35 → 377.42 seconds (3.064% reduction). Subset summed user CPU: 574.44 → 526.06 seconds (8.422% reduction). These figures are not extrapolated to the remaining project modules.
Validation
All local selected-project checks passed:
The quality/trust and generated-index checks used an isolated project-only mirror with the unchanged official checkers, repository configuration, selected source, original project card, and audited compiled artifacts. The Mathlib/Batteries declaration linters covered all 17 modules. These are scoped local checks; repository-wide CI and independent review remain required. No CI, lint configuration, waivers, or other projects were modified.
Limits
This is a shared Azure VM, so elapsed-time scheduling varies between runs; all three paired results are reported. A 02:37:35 UTC snapshot showed a one-minute load average of 38.32 on 16 logical CPUs. The native wall-time median is an observation, not a statistically isolated estimate of the effect. GNU time's peak RSS is the maximum resident set of an individual process, not aggregate memory across concurrent Lake jobs. Explicit equation-lemma references may need adjustment in a future definition refactor. The audit intentionally treats every pre-existing private/generated declaration as part of the invariant, which prevented several otherwise valid import/unfolding changes.
Exact Lean environment comparison used for the invariant audit
Build the base and head project separately using the pinned toolchain, save this as
Audit.lean, then from the head worktree runlake env lean --run /absolute/path/Audit.lean --compare /absolute/base/worktree/.lake/build/lib/lean. Passing an output filename instead of--compare ...records the compact declaration manifest. The original compiled snapshot was preserved before edits; the final comparison also uses freshly rebuilt head artifacts.