Skip to content

[Performance]: getSymbolsWithAnnotation resolves symbols too eagerly. #2816

@bcorso

Description

@bcorso

I'm seeing ResolverAAImpl#getSymbolsWithAnnotation() take ~50% of the processing time in some cases (see numbers below). Note that calling getSymbolsWithAnnotation is literally the first thing a processor does before doing any work itself, so the fact that this call alone takes 50% of the processing time is substantial.

It looks like the main culprit is CollectAnnotatedSymbolsVisitor, which is used to walk the AST and find which symbols are annotated. However, this visitor requires using the Analysis API to first resolve each symbol to a KaSymbol and then wrap in KSAnnotated, which is very expensive.

It should be much cheaper to use a psi visitor, e.g. PsiRecursiveElementWalkingVisitor to find the elements that are annotated. The PsiElement representation should be much cheaper because it's purpose is just to represent the structure of the source file and avoids expensive type resolution/validation that is needed for KaSymbol.

However, one issue with a pure psi visitor approach is that it doesn't take into account type aliases, e.g. if an element is annotated with @MyAlias which is an alias of @MyAnnotation, calling resolver.getSymbolsWithAnnotation("MyAnnotation") wouldn't return symbols annotated with @MyAlias because it doesn't take into account type aliases. In order to fix this, we would need to use the Analysis API to resolve every annotation to fully expand its type aliases. However, this should still be much, much cheaper than resolving every symbol in the AST.

I've created a prototype of both the pure psi approach (without type alias support) and the "fixed" approach (with type alias support). The performance comparisons are shown below:

  • Without optimization:
    • total: 16.5s
    • getSymbolsWithAnnotation: 8.67s
  • With PsiElement optimization with type alias support:
    • total: 8.39s
    • getSymbolsWithAnnotation: 1.84s
  • With PsiElement optimization without type alias support
    • total: 6.98s
    • getSymbolsWithAnnotation: 536ms

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions