diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java index 5409b264260..f9f0538b787 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java @@ -26,7 +26,12 @@ import com.intellij.psi.PsiElementFinder; import com.intellij.psi.PsiManager; import com.intellij.psi.PsiPackage; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.CachedValue; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; +import com.intellij.psi.util.PsiModificationTracker; import com.intellij.util.SmartList; +import com.intellij.util.containers.SLRUCache; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.codegen.NamespaceCodegen; @@ -59,6 +64,8 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade private final PsiManager psiManager; private final LightClassGenerationSupport lightClassGenerationSupport; + private final CachedValue> findClassesCache; + public JavaElementFinder( @NotNull Project project, @NotNull LightClassGenerationSupport lightClassGenerationSupport @@ -66,6 +73,25 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade this.project = project; this.psiManager = PsiManager.getInstance(project); this.lightClassGenerationSupport = lightClassGenerationSupport; + this.findClassesCache = CachedValuesManager.getManager(project).createCachedValue( + new CachedValueProvider>() { + @Nullable + @Override + public Result> compute() { + return new Result>( + new SLRUCache(30, 10) { + @NotNull + @Override + public PsiClass[] createValue(FindClassesRequest key) { + return doFindClasses(key.fqName, key.scope); + } + }, + PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT + ); + } + }, + false + ); } @Override @@ -77,6 +103,13 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade @NotNull @Override public PsiClass[] findClasses(@NotNull String qualifiedNameString, @NotNull GlobalSearchScope scope) { + SLRUCache value = findClassesCache.getValue(); + synchronized (value) { + return value.get(new FindClassesRequest(qualifiedNameString, scope)); + } + } + + private PsiClass[] doFindClasses(String qualifiedNameString, GlobalSearchScope scope) { if (!QualifiedNamesUtil.isValidJavaFqName(qualifiedNameString)) { return PsiClass.EMPTY_ARRAY; } @@ -199,5 +232,35 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade return answer.toArray(new PsiClass[answer.size()]); } + + private static class FindClassesRequest { + private final String fqName; + private final GlobalSearchScope scope; + + private FindClassesRequest(@NotNull String fqName, @NotNull GlobalSearchScope scope) { + this.fqName = fqName; + this.scope = scope; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + FindClassesRequest request = (FindClassesRequest) o; + + if (!fqName.equals(request.fqName)) return false; + if (!scope.equals(request.scope)) return false; + + return true; + } + + @Override + public int hashCode() { + int result = fqName.hashCode(); + result = 31 * result + (scope.hashCode()); + return result; + } + } }