Skip to content

experiment: persist type class resolution cache across commands - #14316

Draft
Kha wants to merge 3 commits into
masterfrom
push-msumqyxslkpk
Draft

experiment: persist type class resolution cache across commands#14316
Kha wants to merge 3 commits into
masterfrom
push-msumqyxslkpk

Conversation

@Kha

@Kha Kha commented Jul 7, 2026

Copy link
Copy Markdown
Member

Not yet sound, to establish upper threshold

@Kha

Kha commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

!bench

@Kha

Kha commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Jul 7, 2026

Copy link
Copy Markdown

Benchmark results for d028112 against 41b2fe8 are in. There are significant results. @Kha

  • build//instructions: -205.7G (-1.84%)

Large changes (4✅)

  • build//instructions: -205.7G (-1.84%)
  • build/profile/typeclass inference//wall-clock: -25s (-16.31%)
  • elab/grind_bitvec2//instructions: -9.5G (-6.50%)
  • elab/grind_list2//instructions: -3.4G (-7.64%)

and 1 hidden

Medium changes (52✅, 3🟥)

Too many entries to display here. View the full report on radar instead.

Small changes (814✅, 6🟥)

Too many entries to display here. View the full report on radar instead.

@leanprover-radar

leanprover-radar commented Jul 7, 2026

Copy link
Copy Markdown

Benchmark results for leanprover-community/mathlib4-nightly-testing@0015b87 against leanprover-community/mathlib4-nightly-testing@de3a9cf are in. There are significant results. @Kha

  • build//instructions: -4.1T (-2.50%)

Large changes (30✅)

Too many entries to display here. View the full report on radar instead.

Medium changes (189✅)

Too many entries to display here. View the full report on radar instead.

Small changes (690✅)

Too many entries to display here. View the full report on radar instead.

@Kha
Kha force-pushed the push-msumqyxslkpk branch 2 times, most recently from 51b6cf0 to 4e89424 Compare July 8, 2026 08:11
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jul 8, 2026
@leanprover-bot

leanprover-bot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-07-07 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-07-08 09:02:34)
  • ✅ Reference manual branch lean-pr-testing-14316 has successfully built against this PR. (2026-07-09 14:12:54) View Log
  • 🟡 Reference manual branch lean-pr-testing-14316 build against this PR didn't complete normally. (2026-07-09 14:15:22) View Log
  • 💥 Reference manual branch lean-pr-testing-14316 build failed against this PR. (2026-07-10 14:44:55) View Log
  • 🟡 Reference manual branch lean-pr-testing-14316 build against this PR didn't complete normally. (2026-07-10 14:45:09) View Log
  • 💥 Reference manual branch lean-pr-testing-14316 build failed against this PR. (2026-07-11 11:23:56) View Log
  • 🟡 Reference manual branch lean-pr-testing-14316 build against this PR didn't complete normally. (2026-07-11 11:25:00) View Log
  • 💥 Reference manual branch lean-pr-testing-14316 build failed against this PR. (2026-07-23 15:47:33) View Log
  • 🟡 Reference manual branch lean-pr-testing-14316 build against this PR didn't complete normally. (2026-07-23 15:48:15) View Log
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-07-21 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-07-23 18:24:29)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-07-22 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-07-24 01:16:14)
  • ✅ Reference manual branch lean-pr-testing-14316 has successfully built against this PR. (2026-08-09 15:52:02) View Log
  • 🟡 Reference manual branch lean-pr-testing-14316 build against this PR didn't complete normally. (2026-08-09 15:53:13) View Log

@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Jul 8, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Jul 8, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Jul 8, 2026

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❌ Mathlib branch lean-pr-testing-14316 built against this PR, but testing failed. (2026-07-08 10:05:47) View Log
  • ❌ Mathlib branch lean-pr-testing-14316 built against this PR, but testing failed. (2026-07-08 13:15:47) View Log
  • ❌ Mathlib branch lean-pr-testing-14316 built against this PR, but testing failed. (2026-07-09 15:13:48) View Log
  • ✅ Mathlib branch lean-pr-testing-14316 has successfully built against this PR. (2026-07-10 15:47:56) View Log
  • ✅ Mathlib branch lean-pr-testing-14316 has successfully built against this PR. (2026-07-11 12:12:11) View Log
  • 💥 Mathlib branch lean-pr-testing-14316 build failed against this PR. (2026-07-23 16:26:38) View Log
  • 💥 Mathlib branch lean-pr-testing-14316 build failed against this PR. (2026-07-23 19:37:51) View Log
  • 💥 Mathlib branch lean-pr-testing-14316 build failed against this PR. (2026-07-23 20:55:53) View Log
  • 💥 Mathlib branch lean-pr-testing-14316 build failed against this PR. (2026-07-23 22:09:53) View Log
  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-07-22 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-07-24 01:16:13)
  • ✅ Mathlib branch lean-pr-testing-14316 has successfully built against this PR. (2026-07-24 10:19:11) View Log
  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-07-29 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-09 15:46:48)

@Kha
Kha force-pushed the push-msumqyxslkpk branch from 4e89424 to 4aa3781 Compare July 8, 2026 11:21
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 8, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 8, 2026
@Kha
Kha force-pushed the push-msumqyxslkpk branch from 4aa3781 to 72d7d46 Compare July 9, 2026 13:08
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 9, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 9, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Jul 9, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 10, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 10, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 10, 2026
@leanprover-bot leanprover-bot added breaks-manual This is not necessarily a blocker for merging, but there needs to be a plan. and removed builds-manual CI has verified that the Lean Language Reference builds against this PR labels Jul 10, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Jul 10, 2026
@Kha
Kha force-pushed the push-msumqyxslkpk branch from 526691f to 6571bd0 Compare July 11, 2026 10:30
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Jul 11, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jul 11, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jul 11, 2026
@Kha

Kha commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Jul 24, 2026

Copy link
Copy Markdown

Benchmark results for 61aa947 against 2065c90 are in. There are significant results. @Kha

  • build//instructions: -127.5G (-1.09%)

Large changes (2✅)

  • elab/grind_bitvec2//instructions: -5.5G (-4.07%)
  • elab/grind_list2//instructions: -1.7G (-4.29%)

Medium changes (24✅)

  • build/module/Init.Data.BitVec.Lemmas//instructions: -1.4G (-1.12%)
  • build/module/Init.Notation//instructions: -1.1G (-7.01%) (reduced significance based on absolute threshold)
  • build/module/Lake.CLI.Main//instructions: -2.9G (-8.78%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.App//instructions: -1.3G (-3.36%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Do.Legacy//instructions: -1.0G (-2.18%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.DocString.Builtin//instructions: -2.2G (-5.45%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.DocString//instructions: -1.2G (-2.94%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.StructInst//instructions: -1.2G (-3.60%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Structure//instructions: -1.7G (-4.86%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Tactic.Basic//instructions: -1.2G (-10.34%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Tactic.BuiltinTactic//instructions: -1.2G (-6.66%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Tactic.Do.Internal.VCGen.Solve//instructions: -1.8G (-8.58%)
  • build/module/Lean.Elab.Tactic.Do.VCGen//instructions: -1.3G (-5.40%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Tactic.Try//instructions: -1.6G (-6.01%) (reduced significance based on absolute threshold)
  • build/module/Lean.Elab.Term.TermElabM//instructions: -1.2G (-4.84%) (reduced significance based on absolute threshold)
  • build/module/Lean.Meta.Tactic.Grind.Arith.CommRing.EqCnstr//instructions: -2.6G (-6.56%) (reduced significance based on absolute threshold)
  • build/module/Lean.Meta.Tactic.Grind.Arith.Cutsat.EqCnstr//instructions: -1.1G (-4.64%) (reduced significance based on absolute threshold)
  • build/module/Lean.Meta.Tactic.Grind.Types//instructions: -1.5G (-4.54%) (reduced significance based on absolute threshold)
  • build/module/Lean.PrettyPrinter.Delaborator.Builtins//instructions: -2.9G (-7.85%) (reduced significance based on absolute threshold)
  • build/profile/typeclass inference//wall-clock: -15s (-11.54%)
  • and 3 more
  • and 1 hidden

Small changes (626✅, 9🟥)

  • build/lakeprof/longest rebuild path//instructions: -11.7G (-1.92%)
  • build/module/Init.BinderPredicates//instructions: -99.6M (-4.70%) (reduced significance based on absolute threshold)
  • build/module/Init.CbvSimproc//instructions: -31.9M (-1.52%)
  • build/module/Init.Conv//instructions: -178.5M (-4.77%) (reduced significance based on absolute threshold)
  • build/module/Init.Core//instructions: -192.8M (-1.97%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.Array.Mem//instructions: -25.3M (-2.10%)
  • build/module/Init.Data.Array.Range//instructions: -33.5M (-0.82%)
  • 🟥 build/module/Init.Data.Array.Subarray.Split//instructions: +8.9M (+1.08%)
  • build/module/Init.Data.Array.Subarray//instructions: -19.5M (-1.07%)
  • build/module/Init.Data.BitVec.Basic//instructions: -37.6M (-1.00%)
  • build/module/Init.Data.BitVec.Bitblast//instructions: -480.3M (-0.90%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.ByteArray.Lemmas//instructions: -43.0M (-0.75%)
  • build/module/Init.Data.Char.Ordinal//instructions: -205.8M (-3.45%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.Dyadic.Basic//instructions: -80.3M (-0.54%)
  • build/module/Init.Data.Fin.Basic//instructions: -12.3M (-0.86%)
  • build/module/Init.Data.Fin.Lemmas//instructions: -104.5M (-0.94%)
  • build/module/Init.Data.Int.Cooper//instructions: -26.5M (-1.44%)
  • build/module/Init.Data.Int.DivMod.Basic//instructions: -13.9M (-1.25%)
  • build/module/Init.Data.Int.DivMod.Bootstrap//instructions: -75.6M (-1.39%)
  • build/module/Init.Data.Int.DivMod.Lemmas//instructions: -598.4M (-1.53%) (reduced significance based on absolute threshold)
  • and 615 more

Kha added a commit that referenced this pull request Jul 24, 2026
Kha added a commit that referenced this pull request Jul 24, 2026
@Kha Kha added the skip-tests Skip test step during PR builds label Jul 26, 2026
@Kha
Kha force-pushed the push-msumqyxslkpk branch from 61aa947 to cf34826 Compare July 26, 2026 12:52
Kha added a commit that referenced this pull request Jul 26, 2026
Kha added a commit that referenced this pull request Jul 26, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 28, 2026
Kha added a commit that referenced this pull request Jul 28, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 28, 2026
Kha added a commit that referenced this pull request Jul 28, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 29, 2026
Kha added a commit that referenced this pull request Jul 29, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 30, 2026
Kha added a commit that referenced this pull request Jul 30, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 30, 2026
Kha added a commit that referenced this pull request Jul 30, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Kha
Kha force-pushed the push-msumqyxslkpk branch from ebd5871 to c50a9e9 Compare July 30, 2026 22:14
Kha added a commit that referenced this pull request Jul 30, 2026
Kha added a commit that referenced this pull request Jul 30, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha added a commit that referenced this pull request Jul 31, 2026
Kha added a commit that referenced this pull request Jul 31, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Kha and others added 3 commits August 8, 2026 10:00
This PR makes type class resolution cache entries depend on the options they observed: a query records every result-relevant option lookup (`Lean.Meta.getRecordedOption`), and an entry is served only while its recorded lookups give the same answers, so options no longer have to invalidate the cache wholesale (nor silently fail to). Options resolved once per query, such as the definitional-equality compatibility flags and the resource limits, are part of the cache key instead.

By-name option reads are restricted while a query runs (`Lean.OptionsRestriction`): a plain read panics, so nothing can go unrecorded. Type class resolution is a closed system, so the few frameworks whose reads cannot influence a result read through `Options.findUnrestricted?`/`Lean.Option.getUnrestricted` at their accessor, each carrying its one-line argument (trace and profiler collection, diagnostics counters, and limits whose excess throws and is never cached). Contexts captured for later rendering (messages, pretty printing, profiling) drop the restriction at the boundary.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…lution cache entries

This PR makes type class resolution cache entries depend on the environment state they observed, completing the dependency tracking begun with option accesses. Extensions the search consults are classified at registration: generation-tracked extensions (instances, unification hints) are read through recording accessors that log the observed generation, and covered extensions hold declaration-keyed content whose observable changes are enforced by the write machinery rather than trusted. A read of an unclassified extension during a query panics, and `tests/elab/tc_cache_covered_claims.lean` locks that audit.

Declaration-keyed writes are guarded for value stability, and a write some recording query could have observed is appended to a change log validated by constant birth ordering: the environment assigns each constant a per-lineage birth index as it becomes observable, so a change whose target was born after an entry was recorded cannot have affected it. Reducibility attribute changes are the most common such write. `debug.synthInstance.checkCacheHits` additionally re-runs served cache hits from scratch and compares, as a differential soak check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… commands

Cache entries whose key contains no metavariables and whose value is closed (no free variables or abstracted metavariables in key or value) are additionally stored in a persistent tier that survives the current command, so identical queries in later commands are served from the cache instead of re-searched. The recorded dependencies introduced in the previous PR replace whole-cache invalidation entirely: an entry from an earlier command is only served while its recorded option lookups, extension generations, and reducibility statuses still give the same answers, so instance declarations, unification hints, and reducibility changes invalidate exactly the affected entries.

The persistent tier lives in a dedicated `Environment` field with branch-local value semantics: fills roll back with the environment (e.g. when a speculatively added instance is discarded), parallel elaboration branches never observe each other's fills, and a fill costs one structure copy. Context-sensitive results (metavariable-laden keys, free-variable-dependent entries) stay in the per-command tier; in particular, free-variable-keyed entries must not be persisted, as `FVarId`s recur across commands under fresh name generators (see the `tc_cache_persist_fvar` test). The behavioral test suite for dependency recording arrives here, as most of it is only observable across commands: decl-time vs post-hoc reducibility changes, option partitioning with coexisting entries, fine-grained unification-hint invalidation, and erased-instance scoping.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Kha
Kha force-pushed the push-msumqyxslkpk branch from 50eb538 to fc60f36 Compare August 9, 2026 15:13
Kha added a commit that referenced this pull request Aug 9, 2026
Kha added a commit that referenced this pull request Aug 9, 2026
…nth`

The `#14316` merge resolved these two files to their ref-era versions, which expect `#eval`-internal type class queries to hit entries persisted by earlier commands. Under the trust-tier persistent cache, `#eval` rolls back its environment changes including cache fills, so these queries search anew each time.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Aug 9, 2026
@leanprover-bot leanprover-bot removed the breaks-manual This is not necessarily a blocker for merging, but there needs to be a plan. label Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR downstream Request a downstream-lean4 adaptation PR. skip-tests Skip test step during PR builds toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants