-
Notifications
You must be signed in to change notification settings - Fork 733
perf: better cache sharing in isDefEq
#8294
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
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ef7cccb
perf: better cache sharing in `isDefEq`
JovanGerb f6881cf
use `(shareCache := true)` everywhere in `ExprDefEq.lean`
JovanGerb f43191b
better management of `Lean.Meta.Cache.defEqTrans`
JovanGerb 19c4a79
retry CI
JovanGerb 9964565
retry CI again
JovanGerb 676b0fc
avoid `@[extern]` to resolve segfault
JovanGerb 0422dcb
Revert "avoid `@[extern]` to resolve segfault"
JovanGerb be3f48e
fix: bug that was already present in `isDefEqProj`: missing `checkPoi…
JovanGerb 34a76d3
retry CI
JovanGerb 2fd2cde
retry CI
JovanGerb aacf58d
move `Term.addTermInfo id` one line up in `withRWRulesSeq`
JovanGerb 4eee7f9
Revert "move `Term.addTermInfo id` one line up in `withRWRulesSeq`"
JovanGerb 983efd6
fix: oh, `Meta.SavedState.restore` doesn't restore the cache, so I ha…
JovanGerb e1d19a5
change how to `modifyDefEqTransientCache`, and add missing `checkpoin…
JovanGerb 2930737
perf: instead of resetting the transient cache to be empty, reset it …
JovanGerb e05d9f9
perf: don't increment `numAssignments` in `instantiateMVars`
JovanGerb 85f2f78
empty commit
JovanGerb 244866d
add a test file
JovanGerb 53a4d87
`set_option maxHeartbeats 1000` in test
JovanGerb cc74f96
slightly improve caching
JovanGerb 0e96d2c
undo caching unifications that instantiate
JovanGerb 8c514b4
avoid `isDefEq` in `ExprDefEq.lean`
JovanGerb 5f05587
empty commit
JovanGerb ebb5309
Merge branch 'nightly-with-mathlib' into Jovan-defEq-cache
JovanGerb 9c0429d
empty commit
JovanGerb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import Lean | ||
| /-! | ||
| Previously, unification wouldn't be very careful with the `isDefEq` cache for terms containing metavariables. | ||
| - This is mostly problematic because erasing the cache leads to exponential slowdowns (`test1` & `test2`) | ||
| - but in some cases it leads to metavariable assignments leaking into places where they shouldn't be, | ||
| which either causes unification to fail where it should succeed (`test3`) | ||
| or to succeed where it is expected to fail. | ||
|
|
||
| -/ | ||
| set_option maxHeartbeats 1000 | ||
|
|
||
| namespace test1 | ||
| class A (n : Nat) where | ||
| x : Nat | ||
|
|
||
| instance [A n] : A (n+1) where | ||
| x := A.x n | ||
|
|
||
| theorem test [A 0] : A.x 100 = sorry := sorry | ||
|
|
||
| -- Previously, this example was exponentially slow | ||
| example [A 1] : A.x 100 = sorry := by | ||
| fail_if_success rw [@test] | ||
| sorry | ||
| end test1 | ||
|
|
||
|
|
||
| namespace test2 | ||
| @[irreducible] def A : Type := Unit | ||
|
|
||
| @[irreducible] def B : Type := Unit | ||
|
|
||
| unseal B in | ||
| @[coe] def AtoB (_a : A) : B := () | ||
|
|
||
| instance : Coe A B where coe := AtoB | ||
|
|
||
| def h {α : Type} (a b : α) : Nat → α | ||
| | 0 => a | ||
| | n + 1 => h b a n | ||
|
|
||
| def f {α : Type} (a b : α) : Nat → Prop | ||
| | 0 => a = b | ||
| | n + 1 => f (h a b n) (h b a n) n ∧ f (h a a n) (h b b n) n | ||
|
|
||
| axiom foo {p} {α : Type} (a b : α) : f a b p | ||
|
|
||
| variable (x : A) (y : B) | ||
| -- Previously, this check was exponentially slow; now it is quadratically slow | ||
| #check (foo (↑x) y : f (AtoB x) y 30) | ||
| end test2 | ||
|
|
||
|
|
||
| namespace test3 | ||
| structure A (α : Type) where | ||
| x : Type | ||
| y : α | ||
|
|
||
| structure B (α : Type) extends A α where | ||
| z : Nat | ||
|
|
||
| def A.map {α β} (f : α → β) (a : A α) : A β := ⟨a.x, f a.y⟩ | ||
|
|
||
| open Lean Meta in | ||
| elab "unfold_head" e:term : term => do | ||
| let e ← Elab.Term.elabTerm e none | ||
| unfoldDefinition e | ||
|
|
||
| -- we use `unfold_head` in order to get the raw kernel projection `·.1` instead of the projection funtcion `A.x`. | ||
| def test {α} (i : B α) : unfold_head i.toA.x := sorry | ||
|
|
||
| -- Previously, in this example the unification failed, | ||
| -- because some metavariable assignment wasn't reverted properly | ||
| -- However, it is clearly the case that `(i.toA.map f).x` is the same as `i.toA.x` | ||
| example (i : B α) (f : α → β) : (i.toA.map f).x := by | ||
| apply @test | ||
| end test3 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about the effect of just these changes in
isDefEqProjon Mathlib.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just doing this change may cause a slight slowdown, because
checkPointDefEqresets the transitive defEq cache. So the cache will be reset more often. I think it will work better as a fix done simultaneously with this the fix from this PR.But I might be wrong, so feel free to try it.