Skip to content

perf: avoid setTransparency when the transparency is already in effect - #14967

Draft
Kha wants to merge 1 commit into
masterfrom
meta-transparency-noop
Draft

perf: avoid setTransparency when the transparency is already in effect#14967
Kha wants to merge 1 commit into
masterfrom
meta-transparency-noop

Conversation

@Kha

@Kha Kha commented Aug 29, 2026

Copy link
Copy Markdown
Member

This PR speeds up elaboration by not rebuilding the MetaM context when withTransparency or withAtLeastTransparency is asked for the transparency setting that is already in effect.

Context.setTransparency rebuilds Config, ConfigWithKey and Context, so each such call allocated three objects and retraced the Context's pointer fields even when nothing changed. withTransparency did this unconditionally, and withAtLeastTransparency did it by construction on the branch where the ambient mode already suffices. Both are hot: withInferTypeConfig wraps every inferType in withAtLeastTransparency .default, as does getFunInfoAux, and .default is usually the ambient mode already.

Skipping the rebuild is observationally equivalent: ConfigWithKey.key is maintained as Config.toKey, whose low three bits are exactly transparency.toUInt64, so re-applying the current mode reproduces an identical Config and an identical key.

This PR speeds up elaboration by not rebuilding the `MetaM` context when `withTransparency` or `withAtLeastTransparency` is asked for the transparency setting that is already in effect.

`Context.setTransparency` rebuilds `Config`, `ConfigWithKey` and `Context`, so each such call allocated three objects and retraced the `Context`'s pointer fields even when nothing changed. `withTransparency` did this unconditionally, and `withAtLeastTransparency` did it by construction on the branch where the ambient mode already suffices. Both are hot: `withInferTypeConfig` wraps every `inferType` in `withAtLeastTransparency .default`, as does `getFunInfoAux`, and `.default` is usually the ambient mode already.

Skipping the rebuild is observationally equivalent: `ConfigWithKey.key` is maintained as `Config.toKey`, whose low three bits are exactly `transparency.toUInt64`, so re-applying the current mode reproduces an identical `Config` and an identical key.

Measured over 12 `elab_bench` files, 5 interleaved repetitions per variant against a same-machine baseline build: -0.33% instructions overall, with every file improving (-0.06% to -1.16%). The run-to-run noise floor for this metric is 0.10% per file and 0.00% overall.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Kha

Kha commented Aug 29, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Aug 29, 2026

Copy link
Copy Markdown

Benchmark results for af20c82 against e991a05 are in. There are significant results. @Kha

  • build//instructions: -39.2G (-0.34%)

Medium changes (2✅, 1🟥)

  • elab/big_omega//instructions: -167.4M (-0.84%)
  • 🟥 elab/bv_decide_realworld//instructions: +1.0G (+2.13%)
  • elab/cbv_system_f//instructions: -976.2M (-1.05%)

Small changes (292✅, 8🟥)

  • build/module/Init.Control.Basic//instructions: -11.6M (-0.54%)
  • build/module/Init.Control.Except//instructions: -10.4M (-0.73%)
  • build/module/Init.Control.Lawful.Instances//instructions: -55.5M (-0.82%)
  • build/module/Init.Core//instructions: -67.4M (-0.68%)
  • build/module/Init.Data.Array.Attach//instructions: -73.5M (-0.71%)
  • build/module/Init.Data.Array.Basic//instructions: -75.1M (-0.69%)
  • build/module/Init.Data.Array.BinSearch//instructions: -23.7M (-0.40%)
  • build/module/Init.Data.Array.Erase//instructions: -43.8M (-0.61%)
  • build/module/Init.Data.Array.Extract//instructions: -211.9M (-0.63%)
  • build/module/Init.Data.Array.Find//instructions: -68.1M (-0.70%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.Array.Lemmas//instructions: -358.4M (-0.68%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.Array.Lex.Lemmas//instructions: -31.9M (-0.33%)
  • build/module/Init.Data.Array.MapIdx//instructions: -65.8M (-0.75%) (reduced significance based on absolute threshold)
  • build/module/Init.Data.Array.Monadic//instructions: -48.1M (-0.79%)
  • build/module/Init.Data.Array.QSort.Basic//instructions: -55.6M (-0.55%)
  • build/module/Init.Data.Array.Range//instructions: -24.1M (-0.60%)
  • build/module/Init.Data.Array.Sort.Lemmas//instructions: -16.4M (-0.51%)
  • build/module/Init.Data.Array.Zip//instructions: -26.1M (-0.57%)
  • build/module/Init.Data.BitVec.Bitblast//instructions: -293.1M (-0.59%)
  • build/module/Init.Data.BitVec.Lemmas//instructions: -700.4M (-0.60%)
  • and 279 more
  • and 1 hidden

@github-actions github-actions Bot added toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN labels Aug 29, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Aug 29, 2026
@leanprover-bot

leanprover-bot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 29, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha

Kha commented Aug 29, 2026

Copy link
Copy Markdown
Member Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Aug 29, 2026

Copy link
Copy Markdown

Benchmark results for leanprover-community/mathlib4-nightly-testing@3da59b7 against leanprover-community/mathlib4-nightly-testing@78f2465 are in. There are significant results. @Kha

  • build//instructions: -379.6G (-0.26%)

Large changes (1✅)

  • 1 hidden

Small changes (7✅, 7🟥)

  • build/module/Batteries.Data.Fin.Coding//instructions: -96.0M (-0.79%)
  • build/module/Batteries.Data.Nat.Bisect//instructions: -49.5M (-0.41%)
  • build/module/Batteries.Recycling.RBTree.WF//instructions: -303.1M (-0.98%)
  • 🟥 build/module/Batteries.Tactic.Trans//instructions: +63.8M (+1.00%)
  • build/module/Mathlib.Algebra.Lie.Basis.Prod//instructions: -285.9M (-0.60%)
  • build/module/Mathlib.Geometry.Manifold.Bordism//instructions: -124.3M (-1.04%)
  • build/module/Mathlib.MeasureTheory.Integral.Regular//instructions: -123.5M (-1.80%)
  • build/module/Mathlib.NumberTheory.DirichletCharacter.GaussSum//instructions: -161.7M (-1.82%)
  • 🟥 build/module/Mathlib.Tactic.ClickSuggestions//instructions: +79.9M (+1.40%)
  • 🟥 build/module/Mathlib.Tactic.Order.CollectFacts//instructions: +255.6M (+2.72%)
  • 🟥 build/module/Mathlib.Tactic.Positivity.Basic//instructions: +749.7M (+1.74%)
  • 🟥 build/module/Mathlib.Tactic.Tauto//instructions: +216.3M (+2.43%)
  • 🟥 build/module/Mathlib.Tactic.Widget.LibraryRewrite//instructions: +155.8M (+1.02%)
  • 🟥 build/module/Qq.Macro//instructions: +194.6M (+1.06%)

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 mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN 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