Use a SLRUCache to reduce memory consumption

This commit is contained in:
Andrey Breslav
2013-03-14 21:04:13 +04:00
parent 784e501f7b
commit db947f5497
@@ -29,7 +29,9 @@ import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager; import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker; import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.util.Function; import com.intellij.util.Function;
import com.intellij.util.containers.SLRUCache;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.ModuleConfiguration; import org.jetbrains.jet.lang.ModuleConfiguration;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; 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 static final Logger LOG = Logger.getInstance("org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache");
private final static Key<CachedValue<AnalyzeExhaust>> ANALYZE_EXHAUST_FULL = Key.create("ANALYZE_EXHAUST_FULL"); private final static Key<CachedValue<SLRUCache<JetFile, AnalyzeExhaust>>> ANALYZE_EXHAUST_FULL = Key.create("ANALYZE_EXHAUST_FULL");
private static final Object lock = new Object(); private static final Object lock = new Object();
public static final Function<JetFile, Collection<JetFile>> SINGLE_DECLARATION_PROVIDER = new Function<JetFile, Collection<JetFile>>() { public static final Function<JetFile, Collection<JetFile>> SINGLE_DECLARATION_PROVIDER = new Function<JetFile, Collection<JetFile>>() {
@@ -68,29 +70,32 @@ public final class AnalyzerFacadeWithCache {
*/ */
// TODO: Also need to pass several files when user have multi-file environment // TODO: Also need to pass several files when user have multi-file environment
@NotNull @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 // Need lock, because parallel threads can start evaluation of compute() simultaneously
synchronized (lock) { synchronized (lock) {
return CachedValuesManager.getManager(file.getProject()).getCachedValue( Project project = file.getProject();
file, return CachedValuesManager.getManager(project).getCachedValue(
project,
ANALYZE_EXHAUST_FULL, ANALYZE_EXHAUST_FULL,
new CachedValueProvider<AnalyzeExhaust>() { new CachedValueProvider<SLRUCache<JetFile, AnalyzeExhaust>>() {
@Nullable
@Override @Override
public Result<AnalyzeExhaust> compute() { public Result<SLRUCache<JetFile, AnalyzeExhaust>> compute() {
SLRUCache<JetFile, AnalyzeExhaust> cache = new SLRUCache<JetFile, AnalyzeExhaust>(3, 8) {
@NotNull
@Override
public AnalyzeExhaust createValue(JetFile file) {
try { try {
if (DumbService.isDumb(file.getProject())) { if (DumbService.isDumb(file.getProject())) {
return new Result<AnalyzeExhaust>( return emptyExhaust();
emptyExhaust(),
PsiModificationTracker.MODIFICATION_COUNT);
} }
ApplicationUtils.warnTimeConsuming(LOG); ApplicationUtils.warnTimeConsuming(LOG);
AnalyzeExhaust analyzeExhaustHeaders = analyzeHeadersWithCacheOnFile(file); AnalyzeExhaust analyzeExhaustHeaders = analyzeHeadersWithCacheOnFile(file);
AnalyzeExhaust exhaust = analyzeBodies(analyzeExhaustHeaders, file); return analyzeBodies(analyzeExhaustHeaders, file);
return new Result<AnalyzeExhaust>(exhaust, PsiModificationTracker.MODIFICATION_COUNT);
} }
catch (ProcessCanceledException e) { catch (ProcessCanceledException e) {
throw e; throw e;
@@ -100,9 +105,12 @@ public final class AnalyzerFacadeWithCache {
return emptyExhaustWithDiagnosticOnFile(file, e); return emptyExhaustWithDiagnosticOnFile(file, e);
} }
} }
};
return Result.create(cache, PsiModificationTracker.MODIFICATION_COUNT);
}
}, },
false false
); ).get(file);
} }
} }
@@ -132,7 +140,7 @@ public final class AnalyzerFacadeWithCache {
} }
@NotNull @NotNull
private static CachedValueProvider.Result<AnalyzeExhaust> emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) { private static AnalyzeExhaust emptyExhaustWithDiagnosticOnFile(JetFile file, Throwable e) {
BindingTraceContext bindingTraceContext = new BindingTraceContext(); BindingTraceContext bindingTraceContext = new BindingTraceContext();
bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e)); bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e));
AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e); AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e);
@@ -141,7 +149,7 @@ public final class AnalyzerFacadeWithCache {
PsiModificationTracker tracker = PsiManager.getInstance(file.getProject()).getModificationTracker(); PsiModificationTracker tracker = PsiManager.getInstance(file.getProject()).getModificationTracker();
((PsiModificationTrackerImpl) tracker).incOutOfCodeBlockModificationCounter(); ((PsiModificationTrackerImpl) tracker).incOutOfCodeBlockModificationCounter();
return new CachedValueProvider.Result<AnalyzeExhaust>(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); return analyzeExhaust;
} }
private static void handleError(@NotNull Throwable e) { private static void handleError(@NotNull Throwable e) {