Skip to content

perf: check universe defeq before unfolding in elaborator - #12186

Closed
kim-em wants to merge 1 commit into
leanprover:masterfrom
kim-em:kim/elab-universe-defeq-fix
Closed

kim-em wants to merge 1 commit into
leanprover:masterfrom
kim-em:kim/elab-universe-defeq-fix

Conversation

@kim-em

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

Copy link
Copy Markdown
Collaborator

This extends the kernel fix from #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.

Performance

On the test case from #12102:

Metric Master #12175 This PR
max u v elab 5,570 5,775 6,015
max v u elab 3,677,625 3,710,609 7,373
max u v kernel 97 97 98
max v u kernel 114,901 129 114,902

This reduces elaboration time for the slow case from 3.7M to 7.3K heartbeats (499x speedup).

Note: The kernel is still slow because this PR only fixes the elaborator. Combined with #12175, both elaboration and kernel would be fast.

Related: #12175 (kernel fix), #12102 (issue)

🤖 Prepared with Claude Code

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
kim-em force-pushed the kim/elab-universe-defeq-fix branch from b35e845 to 81aee17 Compare January 27, 2026 10:49
@kim-em
kim-em changed the base branch from master to nightly-with-mathlib January 27, 2026 10:49
@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 81aee17 against 42a0e92 are in! @kim-em

  • ✅ build//instructions: -147.8M (-0.0%)

Small changes (1🟥)

  • 🟥 build/module/Lean.Meta.ExprDefEq//instructions: +68.1M (+0.3%)

@kim-em
kim-em changed the base branch from nightly-with-mathlib to master January 27, 2026 10:54
@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
@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-01-27 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-01-27 11:55:07)

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
@ghost ghost added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Jan 27, 2026
@ghost

ghost commented Jan 27, 2026

Copy link
Copy Markdown

Mathlib CI status (docs):

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 isDefEqProjDelta loop.

This is a more targeted optimization than #12186, 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, #12186
@kim-em
kim-em marked this pull request as draft January 27, 2026 21:41
@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

breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan 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