Skip to content

perf: check universe defeq before unfolding plain constants - #12175

Closed
kim-em wants to merge 1 commit into
nightly-with-mathlibfrom
fix-universe-normalization-defeq
Closed

kim-em wants to merge 1 commit into
nightly-with-mathlibfrom
fix-universe-normalization-defeq

Conversation

@kim-em

@kim-em kim-em commented Jan 27, 2026 •

Copy link
Copy Markdown
Collaborator

This PR extends the kernel's lazy_delta_reduction_step optimization to handle plain constants, not just applications. Previously, when comparing two constants with the same name but different universe parameters (e.g., c.{max u v} vs c.{max v u}), the kernel would unfold both before discovering the universes are definitionally equal. This caused severe performance issues when the unfolded terms are large (600x slowdown reported in #12102).

The fix checks universe equality for plain constants before unfolding, matching the existing optimization for applications.

The test in tests/lean/run/issue12102.lean verifies:

  • Without fix: fails at 100 heartbeats (needs ~150), diagnostics show [kernel] bar ↦ 2
  • With fix: passes at 100 heartbeats (needs ~50), no kernel unfolds reported

Closes #12102

🤖 Prepared with Claude Code

This PR extends the kernel's `lazy_delta_reduction_step` optimization to handle
plain constants, not just applications. Previously, when comparing two constants
with the same name but different universe parameters (e.g., `c.{max u v}` vs
`c.{max v u}`), the kernel would unfold both before discovering the universes
are definitionally equal.

This caused severe performance issues when the unfolded terms are large. The fix
checks universe equality for plain constants before unfolding, matching the
existing optimization for applications.

Closes #12102

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@kim-em kim-em added changelog-language Language features and metaprograms awaiting-mathlib We should not merge this until we have a successful Mathlib build labels Jan 27, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Jan 27, 2026
This PR switches the PR release workflow from
`softprops/action-gh-release` to `gh release create`.

The `softprops/action-gh-release` action enumerates all releases to
check for existing ones, which fails when the repository has more than
10000 releases due to GitHub API pagination limits. The
`lean4-pr-releases` repository has accumulated over 10000 releases,
causing the PR release workflow to fail with:

```
Only the first 10000 results are available.
```

This is currently blocking all PR toolchain releases, including
#12175.

🤖 Prepared with Claude Code

---------

Co-authored-by: Claude <[email protected]>
@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 Jan 27, 2026
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jan 27, 2026
@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 Jan 27, 2026
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jan 27, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Jan 27, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Jan 27, 2026
@leanprover-bot

leanprover-bot commented Jan 27, 2026 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

@kim-em

kim-em commented Jan 27, 2026

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Jan 27, 2026 •

Copy link
Copy Markdown

Benchmark results for c0d1025 against 0e28043 are in! @kim-em

  • ✅ build//instructions: -2.0G (-0.0%)

Small changes (4✅)

  • ✅ build/module/Std.Data.DTreeMap.Internal.Zipper//instructions: -153.8M (-0.9%)
  • ✅ build/module/Std.Data.HashMap.IteratorLemmas//instructions: -49.7M (-1.8%)
  • ✅ build/module/Std.Data.TreeMap.Raw.Slice//instructions: -28.0M (-1.3%)
  • ✅ build/module/Std.Data.TreeMap.Slice//instructions: -28.9M (-1.4%)

@ghost ghost added builds-mathlib CI has verified that Mathlib builds against this PR and removed awaiting-mathlib We should not merge this until we have a successful Mathlib build labels Jan 27, 2026
@ghost

ghost commented Jan 27, 2026

Copy link
Copy Markdown

kim-em added a commit to kim-em/lean4 that referenced this pull request Jan 27, 2026
This extends the kernel fix from PR leanprover#12175 to the elaborator.

In isDefEqDeltaStep.unfoldBoth, when comparing two plain constants
with the same name but different universe parameters (e.g.,
c.{max u v} vs c.{max v u}), check if the universes are definitionally
equal before unfolding. This avoids expensive unfolding when the
terms are defeq modulo universe ordering.

On the test case from leanprover#12102, this reduces elaboration time from
3.7M heartbeats to 7.3K heartbeats (499x speedup).

Related: leanprover#12175 (kernel fix), leanprover#12102 (issue)
kim-em added a commit to kim-em/lean4 that referenced this pull request Jan 27, 2026
This extends the kernel fix from PR leanprover#12175 to the elaborator.

In isDefEqDeltaStep.unfoldBoth, when comparing two plain constants
with the same name but different universe parameters (e.g.,
c.{max u v} vs c.{max v u}), check if the universes are definitionally
equal before unfolding. This avoids expensive unfolding when the
terms are defeq modulo universe ordering.

On the test case from leanprover#12102, this reduces elaboration time from
3.7M heartbeats to 7.3K heartbeats (499x speedup).

Related: leanprover#12175 (kernel fix), leanprover#12102 (issue)
kim-em added a commit that referenced this pull request Jan 27, 2026
When comparing two projections like `bar.toA` vs `bar.toA` where `bar` has
universe `max u v` on one side and `max v u` on the other, check universe
equality early before entering the lazy_delta_proj_reduction loop.

This is a more aggressive optimization than #12175, which adds the check inside
the delta reduction loop. By checking in the projection handler itself, we can
potentially skip expensive work that happens before reaching the delta step.

Related: #12102, #12175
@kim-em

kim-em commented Jan 28, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: this PR doesn't help the real Mathlib slowdown pattern (tested with KaehlerDifferential universe ordering issue).

@kim-em kim-em closed this Jan 28, 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 changelog-language Language features and metaprograms 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