Use a SLRUCache to reduce memory consumption
This commit is contained in:
@@ -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,41 +70,47 @@ 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(
|
||||||
ANALYZE_EXHAUST_FULL,
|
project,
|
||||||
new CachedValueProvider<AnalyzeExhaust>() {
|
ANALYZE_EXHAUST_FULL,
|
||||||
@Override
|
new CachedValueProvider<SLRUCache<JetFile, AnalyzeExhaust>>() {
|
||||||
public Result<AnalyzeExhaust> compute() {
|
@Nullable
|
||||||
try {
|
@Override
|
||||||
if (DumbService.isDumb(file.getProject())) {
|
public Result<SLRUCache<JetFile, AnalyzeExhaust>> compute() {
|
||||||
return new Result<AnalyzeExhaust>(
|
SLRUCache<JetFile, AnalyzeExhaust> cache = new SLRUCache<JetFile, AnalyzeExhaust>(3, 8) {
|
||||||
emptyExhaust(),
|
|
||||||
PsiModificationTracker.MODIFICATION_COUNT);
|
|
||||||
}
|
|
||||||
|
|
||||||
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<AnalyzeExhaust>(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;
|
false
|
||||||
}
|
).get(file);
|
||||||
catch (Throwable e) {
|
|
||||||
handleError(e);
|
|
||||||
return emptyExhaustWithDiagnosticOnFile(file, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
false
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user