Skip to content

perf: move more rarely-updated Core.Context field into the cold subobject - #15017

Merged
Kha merged 1 commit into
masterfrom
core-context-cold-wide
Sep 4, 2026
Merged

perf: move more rarely-updated Core.Context field into the cold subobject#15017
Kha merged 1 commit into
masterfrom
core-context-cold-wide

Conversation

@Kha

@Kha Kha commented Sep 4, 2026

Copy link
Copy Markdown
Member

This PR further cuts the reference count traffic paid by the recursion-depth and withRef steps that dominate elaboration, from ten increments per reconstruction to three. mathlib4 -0.82% instructions/-1.73% task-clock/-2.03% wall-clock.

@Kha

Kha commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Sep 4, 2026

Copy link
Copy Markdown

Benchmark results for a05f18d against 5877442 are in. There are significant results. @Kha

  • build//instructions: -53.6G (-0.48%)

Medium changes (2✅)

  • elab/simp_local//instructions: -356.6M (-1.19%)
  • size/compile/.out//bytes: -14MiB (-0.51%)

Small changes (305✅, 4🟥)

  • build/module/Init.Control.Lawful.Instances//instructions: -54.8M (-0.83%)
  • build/module/Init.Core//instructions: -45.5M (-0.47%)
  • build/module/Init.Data.Array.Attach//instructions: -66.3M (-0.66%)
  • build/module/Init.Data.Array.Basic//instructions: -57.0M (-0.54%)
  • build/module/Init.Data.Array.Erase//instructions: -48.4M (-0.70%)
  • build/module/Init.Data.Array.Extract//instructions: -180.4M (-0.54%)
  • build/module/Init.Data.Array.Find//instructions: -78.0M (-0.82%)
  • build/module/Init.Data.Array.Lemmas//instructions: -330.2M (-0.64%)
  • build/module/Init.Data.Array.Lex.Lemmas//instructions: -88.0M (-0.94%)
  • build/module/Init.Data.Array.MapIdx//instructions: -55.0M (-0.64%)
  • build/module/Init.Data.Array.Monadic//instructions: -47.1M (-0.80%)
  • build/module/Init.Data.Array.QSort.Basic//instructions: -53.7M (-0.55%)
  • build/module/Init.Data.Array.Range//instructions: -20.4M (-0.52%)
  • build/module/Init.Data.Array.Zip//instructions: -32.6M (-0.72%)
  • build/module/Init.Data.BitVec.Bitblast//instructions: -301.9M (-0.62%)
  • build/module/Init.Data.ByteArray.Lemmas//instructions: -25.7M (-0.48%)
  • build/module/Init.Data.Char.Ordinal//instructions: -26.4M (-0.46%)
  • build/module/Init.Data.Dyadic.Basic//instructions: -88.0M (-0.66%)
  • build/module/Init.Data.Fin.Lemmas//instructions: -81.8M (-0.78%)
  • build/module/Init.Data.Int.DivMod.Bootstrap//instructions: -41.7M (-0.79%)
  • and 289 more

@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 Sep 4, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-09-04 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-09-04 11:36:45)

@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Sep 4, 2026
@leanprover-bot

leanprover-bot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

@Kha Kha added the downstream-force Force creation of a downstream-lean4 adaptation PR. label Sep 4, 2026
@Kha
Kha marked this pull request as ready for review September 4, 2026 12:55
@Kha Kha changed the title perf: move every rarely-updated Core.Context field into the cold subobject perf: move more rarely-updated Core.Context field into the cold subobject Sep 4, 2026
@Kha
Kha added this pull request to the merge queue Sep 4, 2026
@Kha
Kha removed this pull request from the merge queue due to a manual request Sep 4, 2026
…bobject

This PR cuts the reference count traffic paid by the recursion-depth and `withRef` steps that dominate elaboration, from ten increments per reconstruction to three. Measured against master it saves between 0.35% and 1.24% of elaboration instructions across match-, simp- and grind-heavy workloads.

`withReader` can never reuse the `Context` record, so every reconstruction pays one increment per pointer field. The original split classified fields by their number of update *sites*; this classifies them by measured update *frequency* instead. On a representative module `withIncRecDepth` runs about 1.0M times and `withRef` about 31k, while `withOptions` runs once, so `options`, `maxRecDepth`, `currNamespace`, `openDecls`, `initHeartbeats`, `maxHeartbeats` and `currMacroScope` join the cold group and only `currRecDepth` and `ref` stay hot. The `Context` constructor goes from `(0, 10, 2)` to `(0, 3, 2)`.

Rebuilding the wider cold record does make the rare paths that update those fields more expensive: `withCurrHeartbeats` goes from ten increments to fourteen. At roughly 4.9k such calls against 1.05M reconstructions, the trade is strongly positive.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Kha
Kha force-pushed the core-context-cold-wide branch from a05f18d to 82db66b Compare September 4, 2026 13:28
@Kha
Kha enabled auto-merge September 4, 2026 13:28
@Kha
Kha added this pull request to the merge queue Sep 4, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Sep 4, 2026
@downstream-lean4

Copy link
Copy Markdown

The adaptation PR for this PR is leanprover/downstream-lean4#43.

Merged via the queue into master with commit caeb31a Sep 4, 2026
19 checks passed
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 downstream-force Force creation of a downstream-lean4 adaptation PR. 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