You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Equality checking uses too much memory in 2.6.0 (compared to 2.5.4) #4044
namesperformanceSlow type checking, interaction, compilation or execution of Agda programsregression in 2.6.0Regression that first appeared in Agda 2.6.0ux: printingIssues relating to how terms are printed for display
The test below constructs two large identical trees (with dummy function types in each branch) and compare them for definitional equality. In Agda 2.5.4.1 it takes 49 seconds and a very small and constant amount of memory (42 MB), whereas in Agda 2.6.0.1 it takes 3 minutes and 42 seconds and memory usage peaks at 8 GB.
Without the A → B → C → D → parts, Agda 2.6.0.1 does not use any excessive amount of memory.
Agda 2.6.1 as of today does not do any better than Agda 2.6.0.1 (peak memory usage at 10G).
open importAgda.Builtin.Natopen importAgda.Builtin.Equalityopen importAgda.Builtin.UnitpostulateA B C D :SetdataTree:Setwhereleaf : Tree
node : (A → B → C → D → Tree) → (A → B → C → D → Tree) → Tree
tree1 : Nat → Tree
tree1 zero = leaf
tree1 (suc n) = node (λ _ _ _ _ → tree1 n) (λ _ _ _ _ → tree1 n)
tree2 : Nat → Tree
tree2 zero = leaf
tree2 (suc n) = node (λ _ _ _ _ → tree2 n) (λ _ _ _ _ → tree2 n)
test : tree1 18 ≡ tree2 18
test = refl
(If the code above is saved in Bug.agda and the GHC on your path can build both Agda 2.5.4.1 and 2.6.0.1. It might make sense to first try with --dry-run agda-2.5.4.1 and --dry-run agda-2.6.0.1.)
I am trying to find a way to fix this issue but I cannot think of a simple way to remove the quadratic behaviour. The problem is that the new machinery to avoid shadowing relies on addContext as a kind of 'lookahead' to predict which names are shadowing a given name. Ideally, this should be restricted to just the pretty-printer. However there are often many independent calls to prettyTCM, and each individual call does not have sufficient information to determine whether a variable is shadowed.
I discussed the problem offline with @nad but we did not yet come to a conclusion as to what would be a good solution. One piste we discussed was to limit the extra work in addContext to apply only when actually pretty-printing. Another one was to find a better datastructure that can store the currently quadradic shadowing map in (quasi)linear space.
The renaming strategy is underspecified, and it might make sense to change it. (Not only because it is buggy. @jespercockx, could you report the bug that you encountered?)
A partial specification:
There should be no shadowing in pretty-printed terms/contexts.
Free variables that are in scope should not be renamed.
An example: Consider λ x → (? , λ x → ?). In order to avoid shadowing this term could be pretty-printed as λ x₁ → (? , λ x → ?) or λ x → (? , λ x₁ → ?). In either case at least one variable is renamed. When printing the context from the view of the first goal the outermost x should not be renamed, and when printing the context from the view of the second goal the innermost x should not be renamed.
namesperformanceSlow type checking, interaction, compilation or execution of Agda programsregression in 2.6.0Regression that first appeared in Agda 2.6.0ux: printingIssues relating to how terms are printed for display
Activity
nad commentedon Aug 31, 2019
You can perhaps track down a problematic commit by running the following command in a fresh clone of the Agda repository:
(If the code above is saved in
Bug.agdaand the GHC on your path can build both Agda 2.5.4.1 and 2.6.0.1. It might make sense to first try with--dry-run agda-2.5.4.1and--dry-run agda-2.6.0.1.)guillaumebrunerie commentedon Sep 2, 2019
I did the bisection and the result is the following (@jespercockx):
There are only 'skip'ped commits left to test.
The first bad commit could be any of:
b42ac7e
832dffb
e060f2d
c487b8a
guillaumebrunerie commentedon Sep 2, 2019
So this is apparently related to #3446, and commit c487b8a is most likely the culprit (the other three are refactoring commits)
nad commentedon Sep 2, 2019
The following code looks suspicious to me:
agda/src/full/Agda/TypeChecking/Monad/Context.hs
Lines 191 to 198 in 957f54f
It seems as if every time this instance of
addCtxis run something is done for all names in the context. This could lead to quadratic behaviour.nad commentedon Sep 2, 2019
Commenting out lines 193-197 seems to fix the regression. (Now the example can be type-checked in roughly 50 s, using 1 MB of memory, on my machine.)
UlfNorell commentedon Sep 4, 2019
Specifically, it's this
addContextin the conversion checker that is the culprit:agda/src/full/Agda/TypeChecking/Conversion.hs
Lines 316 to 318 in 96d0dd0
Changing
nameto"_" :: Stringmakes the example check in constant memory.nad commentedon Sep 4, 2019
This kind of construction, with
suggestsandaddContext, is used in two other places as well:agda/src/full/Agda/TypeChecking/Conversion.hs
Lines 691 to 692 in 96d0dd0
agda/src/full/Agda/TypeChecking/CheckInternal.hs
Lines 182 to 183 in 96d0dd0
nad commentedon Sep 4, 2019
But I see that
suggestsreturns aString, so I guess the use of this function isn't so interesting.jespercockx commentedon Sep 24, 2019
I am trying to find a way to fix this issue but I cannot think of a simple way to remove the quadratic behaviour. The problem is that the new machinery to avoid shadowing relies on
addContextas a kind of 'lookahead' to predict which names are shadowing a given name. Ideally, this should be restricted to just the pretty-printer. However there are often many independent calls toprettyTCM, and each individual call does not have sufficient information to determine whether a variable is shadowed.I discussed the problem offline with @nad but we did not yet come to a conclusion as to what would be a good solution. One piste we discussed was to limit the extra work in
addContextto apply only when actually pretty-printing. Another one was to find a better datastructure that can store the currently quadradic shadowing map in (quasi)linear space.nad commentedon Sep 24, 2019
Some points based on our discussion:
The renaming strategy is underspecified, and it might make sense to change it. (Not only because it is buggy. @jespercockx, could you report the bug that you encountered?)
A partial specification:
An example: Consider
λ x → (? , λ x → ?). In order to avoid shadowing this term could be pretty-printed asλ x₁ → (? , λ x → ?)orλ x → (? , λ x₁ → ?). In either case at least one variable is renamed. When printing the context from the view of the first goal the outermostxshould not be renamed, and when printing the context from the view of the second goal the innermostxshould not be renamed.jespercockx commentedon Sep 25, 2019
Yes, I reported it here: #4107