Skip to content

perf(CategoryTheory): write down some universe parameters explicitly - #46147

Open
felixpernegger wants to merge 2 commits into
leanprover-community:masterfrom
felixpernegger:fixuniverseexplicit
Open

felixpernegger wants to merge 2 commits into
leanprover-community:masterfrom
felixpernegger:fixuniverseexplicit

Conversation

@felixpernegger

Copy link
Copy Markdown
Contributor

@felixpernegger

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Nov 18, 2026 •

Copy link
Copy Markdown

Benchmark results for 74407c5 against 5198ccc are in. There are significant results. @felixpernegger

  • ✅ build//instructions: -467.9G (-0.43%)

Large changes (4✅)

  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.Basic//instructions: -184.3G (-92.19%)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.ExactSequences//instructions: -181.6G (-71.02%)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.MayerVietoris//instructions: -101.5G (-40.75%)
  • and 1 hidden

Medium changes (1✅)

  • ✅ lint//instructions: -41.7G (-0.82%)

Small changes (1✅, 5🟥)

  • 6 hidden

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR summary 74407c5171

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

No declarations were harmed in the making of this PR! 🐙

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 74407c5).

  • +0 new declarations
  • −0 removed declarations

No declaration differences.


No changes to strong technical debt.
No changes to weak technical debt.

Current commit 74407c5171
Reference commit 5198cccbd0

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions Bot added the t-category-theory Category theory label Nov 18, 2026
@robin-carlier

robin-carlier commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Ouch. How did you find those? Do you have a rationale and why they help? Ideally, such annotations should not be needed: is this a problem that we could potentially minimize and bring to the attention of lean core?

(To clarify: we should definitely take this improvement but having to manually find and write these is tedious and doesn’t give us much insight on how not to repeat the pattern inadvertently)

@felixpernegger

Copy link
Copy Markdown
Contributor Author

How did you find those? Do you have a rationale and why they help?

By looking at the slowest files per line in mathlib

I don't have a great understanding into this, but afaik, one should avoid free universe levels, as lean is very ineffective plugging those in sometimes, see #40964

@robin-carlier

robin-carlier commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

But the universe levels you added are not really "free": CategoryTheory.HasExt has 3 universe levels (w, v and u) and only w (which was already specified before this PR) is free: v is the universe level for the category instance, and u is the universe level of the type.

What’s weird to me is that the problematic universe level is the third (i.e. the one you can guess from the type the fastest!): in Mathlib/CategoryTheory/Sites/SheafCohomology/Basic.lean, the abbrev H is roughly 10x slower without the universe annotation in the variable block, but the speedup is here if you write HasExt.{w', _, max (max u v) (w + 1)}.

But then if you hover on the Sheaf J AddCommGrpCat.{w} type you can see that the universe is supposed to be max (max (w + 1) u) v. If you use that universe in the variable block instead then the definition is slow again. So this is an issue with universe normalization being slow, not with "free" universe variables. It’s also very problematic that we have to know which massaging of the universe expression is the right one to get fast files...

@robin-carlier

robin-carlier commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

It looks like this is the "universe level variables like being sorted alphabetically" quirk again: still in Mathlib/CategoryTheory/Sites/SheafCohomology/Basic.lean, rename the universe w to a, and now max (max (a + 1) u) v is the fast universe while max (max u v) (a + 1), max v (max u (a +1)) and max (max u (a + 1)) v are slow.

We merged #26008, and then later on #34088, which are basically instances of this.

@felixpernegger

Copy link
Copy Markdown
Contributor Author

Thanks for the analysis :)

@robin-carlier

Copy link
Copy Markdown
Contributor

Given that you’ve said on Zulip that these are the only three files where perfomances tricks like this seem to have an effect, let’s move on with this and take the speedup.

Thanks!

maintainer merge

@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by robin-carlier.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. t-category-theory Category theory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants