Closed jcommelin closed 1 month ago
To add some information, the issue is that some typeclass searches are very costly in mathlib, typically the ones that are very generic and therefore lead to a gigantic search, while other instances are much more narrow and so much more likely to fail quickly when they fail (or to succeed quickly). The manual fixes that gave a 2% speedup in mathlib changed the search order for some instances, starting with the narrow ones instead of the generic ones.
The mathlib PR https://github.com/leanprover-community/mathlib4/pull/16646 gives a 2% speedup of mathlib, coming from a 6% speedup of TC synth. This is achieved by changing the TC synth order of certain instances, using mechanisms that were likely not intended as user-facing API, and deviating from the documented behaviour of TC synth. (The PR was merged into mathlib, but later reverted, in order to allow for more research on this topic.)
The following MWE displays behaviour that we believe is analogous to what happens with mathlib's algebraic hierarchy. (Code snippet adapted from https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Ways.20to.20speed.20up.20Mathlib/near/469291144)
The beginning of the trace looks like
When Lean applies
C.toB
it first attempts to find an instance forA 20000
, and times out while doing so. So it never gets to look for an instance ofC
, which does not exist.Now the trace looks like
where Lean immediately bails on
C.toB
and continues withinst1
.