New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: whnf projections during defeq #2003
Conversation
Yes, and supporting this use case was pointed out as important in the past. |
Let's discuss this issue in the next dev meeting. The file |
!bench |
!bench |
Here are the benchmark results for commit bf45a27. |
Here are the benchmark results for commit 9cc819b. |
Add a test with heartbeat limit. |
I wanted to note that I suspect I will witness these regressions in opencompl/lean-mlir --- Zulip thread: performance of equality with projections/mutual, AFAIU, our bug reports were why this commit was landed, which says:
There are some more commits in the same vein: All of which have to do with similar regressions. |
Thanks for the pointer! It would be awesome if we could get an actual regression from lean-mlir. Do you have any plans to update the Lean version in the repo though? There's been a lot of changes in the last three months, and it no longer builds. |
FWIW, all the tests in the commits you've linked to (which are the examples from the Zulip thread) still work without any noticeable slowdown. |
This PR has major performance improvements for mathlib: https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/port.20benchmark/near/320915653 |
I wanted to profile the total impact of this PR on mathlib4, but I'm seeing a TC inference failure in mathlib4 HEAD after merging this PR with master, but not before
|
@Kha Some small changes to mathlib are required. https://github.com/leanprover-community/mathlib4/compare/bump2003?expand=1 |
Shorter repro: #check (add_tsub_cancel_left) What's happening here is that nested TC synthesis behaves differently than non-nested TC synthesis. Usually TC synthesis aborts when it would need to assign a metavariable but can't (e.g. we're synthesizing an instance |
I'm happy to let you know that these changes fix a pair of timeouts in code ported from Lean 3: leanprover-community/mathlib4#1587. It's great service to have the fixing PR out before I run into the issue :D |
There is an issue where this PR doesn't help, namely if one of the sides is a projection of a metavariable, because then the projections don't reduce: This is now #2055 |
Fixes #1986. Kevin's example now takes 24ms instead of 1770ms to elaborate.
What this PR changes is that we now eagerly reduce projections during unification. When solving
foo.1 =?= bar
, we now try to reducefoo
first. And if it reduces to a constructor⟨a, b⟩
, then we continue unifyinga =?= bar
. This actually makes the code simpler, since we went out of our way to avoid reducing projections before.I am convinced that this is the right choice for type class instances. When unifying
@Group.toMul G inst1 =?= @Group.toMul G inst2
we should never try to unify theGroup G
instances (which usually involves unifying all fields) when we only need the multiplication to be equal. And we have various other mechanisms to assign type class instance metavariables besides unification (which is super brittle): nested type class resolution, as well as type class resolution in the elaborator.I can see some potential regressions with structures, for example when unifying
(a, b).1 =?= (?m).1
. But all tests still pass and mathlib4 builds with very minor changes:foo (↑f) h
then the coercion is not elaborated before elaboratingh
.rw [@foo]
works whilerw [foo]
doesn't (only an issue with the notoriously flakyCovariantClass
type class)