Skip to content

Equality checking uses too much memory in 2.6.0 (compared to 2.5.4) #4044

Closed

Description

@guillaumebrunerie

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 import Agda.Builtin.Nat
open import Agda.Builtin.Equality
open import Agda.Builtin.Unit

postulate
  A B C D : Set

data Tree : Set where
  leaf : 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

Activity

  1. nad commented on Aug 31, 2019

    @nad
    Collaborator

    You can perhaps track down a problematic commit by running the following command in a fresh clone of the Agda repository:

    agda-bisect --good v2.5.4.1 --bad v2.6.0.1 --log log --timeout 80 --no-default-cabal-options Bug.agda
    

    (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.)

  2. added
    performance
    regression on master
    on Aug 31, 2019
  3. added this to the 2.6.1 milestone on Aug 31, 2019
  4. guillaumebrunerie commented on Sep 2, 2019

    @guillaumebrunerie
    MemberAuthor

    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

  5. guillaumebrunerie commented on Sep 2, 2019

    @guillaumebrunerie
    MemberAuthor

    So this is apparently related to #3446, and commit c487b8a is most likely the culprit (the other three are refactoring commits)

  6. nad commented on Sep 2, 2019

    @nad
    Collaborator

    The following code looks suspicious to me:

    instance MonadAddContext TCM where
    addCtx x a ret = do
    when (not $ isNoName x) $ do
    registerForShadowing x
    ys <- getContextNames
    forM_ ys $ \y ->
    when (not (isNoName y) && sameRoot x y) $ tellShadowing x y
    defaultAddCtx x a ret

    It seems as if every time this instance of addCtx is run something is done for all names in the context. This could lead to quadratic behaviour.

  7. nad commented on Sep 2, 2019

    @nad
    Collaborator

    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.)

  8. UlfNorell commented on Sep 4, 2019

    @UlfNorell
    Member

    Specifically, it's this addContext in the conversion checker that is the culprit:

    equalFun _ (Pi dom@Dom{domInfo = info} b) m n | not $ domFinite dom = do
    let name = suggests [ Suggestion b , Suggestion m , Suggestion n ]
    addContext (name, dom) $ compareTerm cmp (absBody b) m' n'

    Changing name to "_" :: String makes the example check in constant memory.

  9. nad commented on Sep 4, 2019

    @nad
    Collaborator

    This kind of construction, with suggests and addContext, is used in two other places as well:

    let name = suggests [ Suggestion b1 , Suggestion b2 ]
    addContext (name, dom) $ cont

    let name = suggests [ Suggestion vb , Suggestion b ]
    addContext (name, a) $ do

  10. nad commented on Sep 4, 2019

    @nad
    Collaborator

    But I see that suggests returns a String, so I guess the use of this function isn't so interesting.

  11. jespercockx commented on Sep 24, 2019

    @jespercockx
    Member

    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.

  12. nad commented on Sep 24, 2019

    @nad
    Collaborator

    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:

      • 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.

  13. jespercockx commented on Sep 25, 2019

    @jespercockx
    Member

    @jespercockx, could you report the bug that you encountered?

    Yes, I reported it here: #4107

  14. added
    regression in 2.6.0
    ux: printing
    and removed
    regression on master
    on Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

namesperformanceregression in 2.6.0ux: printing

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions