Skip to content

Reduce IwaniecLinearSieve arithmetic elaboration cost - #772

Merged
Vilin97 merged 1 commit into
mainfrom
codex/daily-optimize-20261106T2215
Nov 7, 2026
Merged

Vilin97 merged 1 commit into
mainfrom
codex/daily-optimize-20261106T2215

Conversation

@Vilin97

@Vilin97 Vilin97 commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

IwaniecLinearSieve spends substantial elaboration time normalizing unrelated local inequalities in arithmetic proofs. This change restricts those searches and reuses existing analytic bounds. On the Azure VM, the median clean project build fell from 386.215s to 328.165s (15.03% faster) across two quiet measurements per revision.

  • Give linarith and nlinarith explicit hypothesis lists while preserving the surrounding proof structure and existing explicit lemma inputs.
  • Replace the cubic comparison in TheoremFive.crude_exponent_le with a factored product comparison; restrict its remaining exponential estimates to their relevant inequalities.
  • Reuse the existing large-ξ estimate and direct maximum bounds in TheoremFive.exists_card_small instead of reconstructing those bounds through logarithmic comparisons.
  • Compose standard IsBigO multiplication, norm and congruence results in integrable_log_over_pow; use explicit monotonicity and nonnegativity arguments in the analytic support proofs.
  • Remove the redundant private Mathlib.Algebra.Order.Star.Real import in TheoremFiveCount, as reported by lake shake.

Invariant audit: Before editing, a standalone Lean environment inspector imported all 48 project modules at private visibility and exported every declaration's full type expression DAG, universe parameters, kind, safety, mutual-declaration information, and non-theorem value/reducibility information. The final clean-build export matches all 2,575 module/name records (2,573 distinct names) exactly in those fields, including compiler-generated private helpers. All 243 definition values are identical. There are no added or removed declarations; only 312 theorem proof bodies differ. Types are compared structurally, including binder names and annotations, rather than by pretty-printed text.

Measured/audited base: 49bdad2b2b82b4b41d75a8e251ac10b2aa08fb45
Head: 94c8f24e3f70c5536e81d509ce8b6a8c45e7006e

Main advanced to 5bf7f7139f861a1e548dff28e2e747fb66d472f4 during the cycle. The selected project, its MooreBound dependency, pinned toolchain/dependency manifest, and relevant contribution/review/quality rules are unchanged from the measured base. The measured head is preserved without rebasing.

Measurements: Both revisions use the repository-pinned Lean/Mathlib 4.35.0, unchanged Lake options and compiler linters, LEAN_NUM_THREADS=1, and CPU affinity 14 on the same AMD EPYC 9V45 Azure VM. Before each run, only this project's artifacts under .lake/build/lib/lean/LeanPool/IwaniecLinearSieve* and .lake/build/ir/LeanPool/IwaniecLinearSieve* were deleted. Mathlib and unchanged dependency artifacts were retained and warmed. All 48 project modules were rebuilt using:

/usr/bin/time -v taskset -c 14 env LEAN_NUM_THREADS=1 \
  lake build LeanPool.IwaniecLinearSieve.Imports
Run Wall time (s) Maximum child RSS (KiB)
baseline2 394.22 2,056,828
optimized3 307.35 2,052,340
baseline3 378.21 2,055,812
optimized4 348.98 2,052,340

Median RSS: 2,056,320 → 2,052,340 KiB, effectively unchanged. Mean aggregate user+system CPU time: 367.460 → 317.425s (13.62% lower). The reported wall-time improvement is the ratio of the two per-revision medians. Shared VM load caused visible timing variation; these measurements do not establish the same speedup on parallel CI runners. An exploratory baseline that overlapped a diagnostic compiler on CPU 14's SMT sibling was excluded from this table.

Validation on the final source:

  • lake build LeanPool.IwaniecLinearSieve.Imports, including the normal warning-as-error and proof-size plugin checks; all timed clean builds passed.
  • lake env lean --run scripts/ci/lint-project.lean over all 48 owned modules: slow Mathlib declaration linters passed (2,572 owner-filtered declarations).
  • lake exe lint-style LeanPool.IwaniecLinearSieve.Imports and git diff --check: passed.
  • Repository quality routines scoped to all 48 selected modules: headers, forbidden text/options, file sizes, metadata/card and main-declaration resolution, generated project aggregate, compiled option/soundness audit, and allowed-axiom audit: zero errors.
  • Declaration manifest comparison repeated after the final clean build: zero invariant violations.
  • lake shake --force --keep-public LeanPool.IwaniecLinearSieve.Imports: no remaining selected-project suggestions. It still reports suggestions in unchanged MooreBound dependencies, outside this PR's scope.

The PR changes only this existing project's Lean sources. Public imports, project metadata, generated indexes, toolchain pins, repository checks and waivers are unchanged. Full protected CI and the independent refactor review remain applicable. Auto-merge is disabled.

@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

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

RetriggerView in Greptile

[Medium impact] The proof-only refactor appears safe to merge; no blocking issue was found.

Summary

This refactor narrows arithmetic proof searches across 36 existing Lean modules while preserving the surrounding arguments.

  • Lean builds the sieve proofs faster without changing their statements.

Reviews (1) · Last reviewed commit: "perf(IwaniecLinearSieve): narrow arithme..." · Reviewed by Greptile

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Proof profile (new / modified Lean files)

Statement changes

✅ No statements changed — all 1,057 tracked declarations across 36 modified files kept their type; only proofs / definition bodies changed.

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.

Compile cost of modified files — base → head

Heartbeats: 6,923,980 → 5,766,888 (-1,157,092, -16.7%). Commands: 1,931 → 1,931.

Per-file wall sum: 615.5 s → 560.3 s (-9.0%). Wall clock is measured under parallel load and is noisy; heartbeats are deterministic and are the regression signal.

Files: 36 compared — 25 cheaper, 1 costlier, 10 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 trace.profiler.useHeartbeats and synchronous elaboration, converted to maxHeartbeats units. Failed measurements are not classified as cheaper or costlier. Δ lines counts added/deleted lines from this PR diff.

File Δ lines HB base HB head Δ HB Δ HB % Wall base (s) Wall head (s)
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/StrongPNT.lean +134/-116 970,950 797,881 -173,068 -17.8% 57.28 50.47
LeanPool/IwaniecLinearSieve/SieveMajorants/Lemmas.lean +109/-107 377,913 224,535 -153,379 -40.6% 44.92 29.71
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFiveAux.lean +43/-38 198,452 59,299 -139,153 -70.1% 15.74 8.47
LeanPool/IwaniecLinearSieve/LinearSieve/Corollaries.lean +73/-71 301,036 171,516 -129,520 -43.0% 34.47 20.34
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFour.lean +43/-43 263,471 149,351 -114,120 -43.3% 27.37 18.50
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFive.lean +35/-35 212,379 125,081 -87,298 -41.1% 27.21 16.98
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/MediumPNT.lean +120/-108 925,203 839,829 -85,374 -9.2% 58.51 72.18
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/ZetaBounds.lean +102/-109 1,167,205 1,106,627 -60,578 -5.2% 48.41 58.01
LeanPool/IwaniecLinearSieve/LinearSieve/Depth.lean +16/-16 79,555 41,005 -38,550 -48.5% 10.83 5.98
LeanPool/IwaniecLinearSieve/SieveFunctions/Bounds.lean +30/-30 80,598 61,485 -19,112 -23.7% 12.14 10.45
LeanPool/IwaniecLinearSieve/LinearSieve/LemmaSeven.lean +32/-32 56,363 37,482 -18,881 -33.5% 8.49 6.79
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFourAux.lean +27/-27 95,174 77,216 -17,959 -18.9% 9.70 11.07
LeanPool/IwaniecLinearSieve/SieveMajorants/Properties.lean +49/-49 83,143 65,760 -17,383 -20.9% 9.62 8.30
LeanPool/IwaniecLinearSieve/LinearSieve/LemmaEleven.lean +51/-51 152,402 137,156 -15,246 -10.0% 13.47 16.25
LeanPool/IwaniecLinearSieve/PrimeSums/Mertens.lean +29/-29 70,183 55,223 -14,960 -21.3% 9.39 9.64
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFourAsymp.lean +13/-13 109,252 98,423 -10,830 -9.9% 14.83 11.38
LeanPool/IwaniecLinearSieve/Jacobsthal/Reduction.lean +5/-5 66,583 55,867 -10,716 -16.1% 13.45 15.12
LeanPool/IwaniecLinearSieve/LinearSieve/TheoremFiveCount.lean +18/-16 114,320 104,981 -9,339 -8.2% 15.94 12.39
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/MellinCalculus.lean +21/-21 456,291 448,382 -7,909 -1.7% 30.98 30.68
LeanPool/IwaniecLinearSieve/LinearSieve/Crude.lean +11/-11 65,001 57,956 -7,045 -10.8% 8.33 8.85
LeanPool/IwaniecLinearSieve/PrimeSums/Error.lean +19/-19 46,586 39,864 -6,722 -14.4% 6.70 7.84
LeanPool/IwaniecLinearSieve/PrimeSums/Lemma13.lean +39/-39 85,062 78,660 -6,402 -7.5% 10.70 8.60
LeanPool/IwaniecLinearSieve/SieveFunctions/LemmaFive.lean +33/-33 49,444 44,728 -4,716 -9.5% 7.85 7.68
LeanPool/IwaniecLinearSieve/PrimeSums/Product.lean +25/-25 69,815 65,357 -4,459 -6.4% 8.64 8.52
LeanPool/IwaniecLinearSieve/SieveMajorants/Defs.lean +30/-30 60,165 56,914 -3,251 -5.4% 7.91 9.89
LeanPool/IwaniecLinearSieve/PrimeSums/NthPrime.lean +3/-3 47,040 48,115 +1,074 +2.3% 7.66 6.49
LeanPool/IwaniecLinearSieve/PrimeSums/SummationByParts.lean +2/-2 22,031 21,306 -726 -3.3% 5.18 4.83
LeanPool/IwaniecLinearSieve/Jacobsthal/ShiftedSieve.lean +2/-2 50,774 50,196 -578 -1.1% 12.27 15.29
LeanPool/IwaniecLinearSieve/SieveMajorants/Delay.lean +9/-9 11,984 11,641 -343 -2.9% 3.82 4.04
LeanPool/IwaniecLinearSieve/LinearSieve/Calculus.lean +13/-13 22,010 21,686 -324 -1.5% 9.11 10.93
LeanPool/IwaniecLinearSieve/SieveMajorants/Majorants.lean +17/-17 27,738 27,581 -157 -0.6% 4.66 4.52
LeanPool/IwaniecLinearSieve/SieveFunctions/Series.lean +9/-9 23,465 23,426 -39 -0.2% 3.86 3.85
LeanPool/IwaniecLinearSieve/LinearSieve/Estimate.lean +2/-2 190,773 190,751 -23 -0.0% 23.77 19.47
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/EulerMaclaurin.lean +1/-1 40,362 40,353 -9 -0.0% 5.29 5.65
LeanPool/IwaniecLinearSieve/Vendor/PrimeNumberTheoremAnd/ResidueCalcOnRectangles.lean +1/-1 293,157 293,157 +1 +0.0% 20.95 14.40
LeanPool/IwaniecLinearSieve/Rosser.lean +2/-2 38,100 38,099 -0 -0.0% 6.06 6.74
Total +1,168/-1,134 6,923,980 5,766,888 -1,157,092 -16.7% 615.5 560.3

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

@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.

{
"summary": "Technical source review at exact 94c8f24: all 354 changed written proof envelopes are covered. Independent normalization isolates 341 tactic-input changes; the remaining 13 arguments were reviewed separately, including the positive factored cubic bound, reuse of the existing large-xi/max witnesses, and BigO multiplication/norm/eventual-congruence route for the logarithmic tail integral. Required signs, thresholds, endpoint hypotheses and theorem conclusions are retained. Written headers and nonproof values are preserved, with no added or removed declarations. The sole private import removal leaves public import vectors unchanged; actual elaboration remains a hosted-CI requirement. The author's generated-declaration audit and measured speedup were not independently reproduced. Current-head protected CI and native integration remain required.",
"assessment": {
"scope": "existing IwaniecLinearSieve proof elaboration and one private import removal",
"introduces_tech_debt": false,
"maintainability": "improved",
"brittleness": "more_robust",
"risk": "low",
"assessment_one_sentence": "Explicit tactic inputs and standard analytic composition expose the needed arguments without weakening statements or replacing reusable interfaces."
},
"verdict": "approve",
"findings": []
}

@Vilin97

Vilin97 commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator Author

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

Reviewed head: 94c8f24e3f70c5536e81d509ce8b6a8c45e7006e

Verdict: ✅ approve

Across 36 modules, this refactor narrows arithmetic tactic inputs while preserving intermediate bounds and case structure. The larger rewrites expose factored comparisons and reuse existing estimates and asymptotics APIs. The supplied profile reports 16.7% fewer heartbeats overall. I found no actionable maintenance regression; local builds were not rerun because this checkout lacks the pinned dependency artifacts.

Aspect Value
Scope arithmetic tactic and analytic proof refactor
Introduces tech debt ✅ no
Maintainability ✅ improved
Brittleness ✅ more_robust
Risk ✅ low

Explicit arithmetic dependencies and standard lemma composition make these proofs easier to debug and less sensitive to unrelated context without sacrificing reusable structure.


Tokens: 4,091,453 in / 8,032 out · Tier: codex-subscription · Effort: xhigh · Billing: Codex subscription quota (no API credits) · Estimated cost: $16.4863 (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.

@Vilin97
Vilin97 added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 70ea369 Nov 7, 2026
26 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