leanprover-community / leanprover-community/mathlib4

Kernel type checking performance depends on lexicographic ordering of universe variables

Open
#26,018 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lean
Stars
4.2k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

Observation

The number of instructions required to build Mathlib/RingTheory/Kaehler/JacobiZariski.lean depends on the lexicographic order of the universe variables. The maximal observed variation is 15 * 10^10 instructions. For context, at the time of writing this issue, this variation alone would put a file in the top 100 slowest files of mathlib.

Hypothesis

When the kernel type checks declarations and there is a (syntactic) mismatch in universe levels, it seems to unfold everything even if the left and right hand side are otherwise syntactically equal. A slightly minimized example of this effect can be seen here (This minimized problem can be fixed by changing a transparency setting, but not the original one. See below for more details.)

Possible causes

Attempts at improving the performance

Change lexicographic order of universe variables

By changing the lexicographic order of universe variables in #21129, the instruction count changed by - 21 * 10^10 (- 29%), later measurements in #26008 show reduced absolute gains of - 13 * 10^10 and comparable relative gains - 26% instructions.

Make Extension.CotangentSpace less reducible

It was repeatedly raised that Extension.CotangentSpace should be a def instead of an abbrev. This was tried here (#21364) and reported here: https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/kernel.20universe.20normalization.20issue/near/497399181. The effect is negligible.

As of now, all minimization could be fixed by replacing the abbrev by def though.

Unbundle type fields from Generators and Extension
  • The bundled type fields in Algebra.Generators and Algebra.Extension caused suspicion. Subsequently, the type fields of Algebra.Generators were unbundled in #25085 with overall performance change of - 9 * 10 ^ 10 instructions, but with no effect on JacobiZariski.lean.
  • In a follow-up attempt, unbundling the remaining type variable from Algebra.Extension was made in #25191. The performance effect was catastrophic featuring a change of + 55 * 10 ^ 10 (+ 90%) instructions in JacobiZariski.lean.

Other, possibly unrelated performance issues in JacobiZariski

There are most certainly various performance issues appearing in this file and not all of them are related to universes. The file remains slow even if replacing all universe variables by the same one.

Discussions on this

Instructions for testing performance locally

Run lake env perf stat lean Mathlib/RingTheory/Kaehler/JacobiZariski.lean. The most relevant number is cpu_core/instructions which on master at the time of writing is at ~ 54 * 10^10.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running lake env perf stat lean Mathlib/RingTheory/Kaehler/JacobiZariski.lean and compare the cpu_core/instructions result under different universe-variable orderings. Read the linked Lean kernel and Zulip discussions, then identify and address the kernel type-checking behavior causing the performance variation; done means the ordering-dependent cost is reduced or eliminated.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.