Basic PSI questions (mutability, caching)

I have an inspection that invokes some heavy PSI stuff on possibly
hundreds of files. It runs very slowly. It looks like a lot of the time
is spent in things like CompositeElement.getChildren() and
.countChildren, and also in some custom PSI methods for my language.

The files aren't modified frequently, so most of this data could be
cached. However, I don't feel that I understand some parts of PSI well
enough to make a correct cache. Is a PSI tree mutable? Is it a good idea
for a method to cache its children, or some processed version of its
getText(), in a field? If not, is PSI tree listener the best way to
build such a cache?

4 comments

com.intellij.psi.util.CachedValueProvider and friends?!

Maybe someone from JB can shed some light on this (when/where to use etc)

0

Well, in general we don't cache children since it would require too much
memory. In my exprerience (as IDEA user), getting children every time is not
expensive at all. Rather the slowness could be caused by algorithm
complexity itself. Some of the bits of PSI are rather expensive in some
cases, but
in order to explain your problem it is necessary to see the code.

Eugene.


"Keith Lea" <keith@cs.oswego.edu> wrote in message
news:dj12g6$m4k$1@is.intellij.net...

I have an inspection that invokes some heavy PSI stuff on possibly
hundreds of files. It runs very slowly. It looks like a lot of the time
is spent in things like CompositeElement.getChildren() and
.countChildren, and also in some custom PSI methods for my language.

>

The files aren't modified frequently, so most of this data could be
cached. However, I don't feel that I understand some parts of PSI well
enough to make a correct cache. Is a PSI tree mutable? Is it a good idea
for a method to cache its children, or some processed version of its
getText(), in a field? If not, is PSI tree listener the best way to
build such a cache?



0

CachedValue is used to make certain computation depend on certain objects.
Once the dependencies get out of date, the cached value is recomputed,
otherwise it is taken from previous computation.
However this mechanism, while being very powerful, is at the same time
rather expensive, so you can't use it for computing properties of each
distinct PsiElement.
An example of when it could be used is caching members in the class (with
its superclasses) till some out-of-code block modification occurs.

Eugene.

"Yann Cebron" <no_mail@jetbrains.com> wrote in message
news:4468157.1129621185893.JavaMail.itn@is.intellij.net...

com.intellij.psi.util.CachedValueProvider and friends?!

>

Maybe someone from JB can shed some light on this (when/where to use etc)



0

This doesn't answer my question, what semantics can I depend on to build such a
cache? Are PSI elements immutable? Is there some functionality for this already?

Eugene Vigdorchik (JetBrains) wrote:

Well, in general we don't cache children since it would require too much
memory. In my exprerience (as IDEA user), getting children every time is not
expensive at all. Rather the slowness could be caused by algorithm
complexity itself. Some of the bits of PSI are rather expensive in some
cases, but
in order to explain your problem it is necessary to see the code.

Eugene.


"Keith Lea" <keith@cs.oswego.edu> wrote in message
news:dj12g6$m4k$1@is.intellij.net...

>>I have an inspection that invokes some heavy PSI stuff on possibly
>>hundreds of files. It runs very slowly. It looks like a lot of the time
>>is spent in things like CompositeElement.getChildren() and
>>.countChildren, and also in some custom PSI methods for my language.
>>
>>The files aren't modified frequently, so most of this data could be
>>cached. However, I don't feel that I understand some parts of PSI well
>>enough to make a correct cache. Is a PSI tree mutable? Is it a good idea
>>for a method to cache its children, or some processed version of its
>>getText(), in a field? If not, is PSI tree listener the best way to
>>build such a cache?


0

Please sign in to leave a comment.