From db947f5497220af9873c5ab455be58fdf405cfe2 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 14 Mar 2013 21:04:13 +0400 Subject: [PATCH] Use a SLRUCache to reduce memory consumption --- .../project/AnalyzerFacadeWithCache.java | 70 +++++++++++-------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java index 8b862bbab34..6373c093ce8 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java +++ b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java @@ -29,7 +29,9 @@ import com.intellij.psi.util.CachedValueProvider; import com.intellij.psi.util.CachedValuesManager; import com.intellij.psi.util.PsiModificationTracker; import com.intellij.util.Function; +import com.intellij.util.containers.SLRUCache; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.lang.ModuleConfiguration; import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; @@ -50,7 +52,7 @@ public final class AnalyzerFacadeWithCache { private static final Logger LOG = Logger.getInstance("org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache"); - private final static Key> ANALYZE_EXHAUST_FULL = Key.create("ANALYZE_EXHAUST_FULL"); + private final static Key>> ANALYZE_EXHAUST_FULL = Key.create("ANALYZE_EXHAUST_FULL"); private static final Object lock = new Object(); public static final Function> SINGLE_DECLARATION_PROVIDER = new Function>() { @@ -68,41 +70,47 @@ public final class AnalyzerFacadeWithCache { */ // TODO: Also need to pass several files when user have multi-file environment @NotNull - public static AnalyzeExhaust analyzeFileWithCache(@NotNull final JetFile file) { + public static AnalyzeExhaust analyzeFileWithCache(@NotNull JetFile file) { // Need lock, because parallel threads can start evaluation of compute() simultaneously synchronized (lock) { - return CachedValuesManager.getManager(file.getProject()).getCachedValue( - file, - ANALYZE_EXHAUST_FULL, - new CachedValueProvider() { - @Override - public Result compute() { - try { - if (DumbService.isDumb(file.getProject())) { - return new Result( - emptyExhaust(), - PsiModificationTracker.MODIFICATION_COUNT); - } + Project project = file.getProject(); + return CachedValuesManager.getManager(project).getCachedValue( + project, + ANALYZE_EXHAUST_FULL, + new CachedValueProvider>() { + @Nullable + @Override + public Result> compute() { + SLRUCache cache = new SLRUCache(3, 8) { - ApplicationUtils.warnTimeConsuming(LOG); + @NotNull + @Override + public AnalyzeExhaust createValue(JetFile file) { + try { + if (DumbService.isDumb(file.getProject())) { + return emptyExhaust(); + } - AnalyzeExhaust analyzeExhaustHeaders = analyzeHeadersWithCacheOnFile(file); + ApplicationUtils.warnTimeConsuming(LOG); - AnalyzeExhaust exhaust = analyzeBodies(analyzeExhaustHeaders, file); + AnalyzeExhaust analyzeExhaustHeaders = analyzeHeadersWithCacheOnFile(file); - return new Result(exhaust, PsiModificationTracker.MODIFICATION_COUNT); + return analyzeBodies(analyzeExhaustHeaders, file); + } + catch (ProcessCanceledException e) { + throw e; + } + catch (Throwable e) { + handleError(e); + return emptyExhaustWithDiagnosticOnFile(file, e); + } + } + }; + return Result.create(cache, PsiModificationTracker.MODIFICATION_COUNT); } - catch (ProcessCanceledException e) { - throw e; - } - catch (Throwable e) { - handleError(e); - return emptyExhaustWithDiagnosticOnFile(file, e); - } - } - }, - false - ); + }, + false + ).get(file); } } @@ -132,7 +140,7 @@ public final class AnalyzerFacadeWithCache { } @NotNull - private static CachedValueProvider.Result emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) { + private static AnalyzeExhaust emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) { BindingTraceContext bindingTraceContext = new BindingTraceContext(); bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e)); AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e); @@ -141,7 +149,7 @@ public final class AnalyzerFacadeWithCache { PsiModificationTracker tracker = PsiManager.getInstance(file.getProject()).getModificationTracker(); ((PsiModificationTrackerImpl) tracker).incOutOfCodeBlockModificationCounter(); - return new CachedValueProvider.Result(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); + return analyzeExhaust; } private static void handleError(@NotNull Throwable e) {