JVM IR: clear BindingContext after psi2ir

This helps to reduce peak memory in lowerings/codegen by a lot.

A more robust approach would be to have a separate BindingContext for
each file, and clear each of them after running psi2ir on it. This would
also lower peak memory usage in psi2ir.

Provide a fallback workaround compiler argument
-Xir-do-not-clear-binding-context just in case BindingContext is in fact
used somewhere and it's not caught by tests.
This commit is contained in:
Alexander Udalov
2020-10-21 22:26:55 +02:00
parent bc76f1fec3
commit 382a0bd298
11 changed files with 66 additions and 29 deletions
@@ -51,7 +51,8 @@ import java.util.regex.Pattern
data class DiagnosticsRenderingConfiguration(
val platform: String?,
val withNewInference: Boolean,
val languageVersionSettings: LanguageVersionSettings?
val languageVersionSettings: LanguageVersionSettings?,
val skipDebugInfoDiagnostics: Boolean = false,
)
object CheckerTestUtil {
@@ -130,18 +131,20 @@ object CheckerTestUtil {
diagnostics.add(ActualDiagnostic(SyntaxErrorDiagnostic(errorElement), configuration.platform, configuration.withNewInference))
}
diagnostics.addAll(
getDebugInfoDiagnostics(
root,
bindingContext,
markDynamicCalls,
dynamicCallDescriptors,
configuration,
dataFlowValueFactory,
moduleDescriptor,
diagnosedRanges
if (!configuration.skipDebugInfoDiagnostics) {
diagnostics.addAll(
getDebugInfoDiagnostics(
root,
bindingContext,
markDynamicCalls,
dynamicCallDescriptors,
configuration,
dataFlowValueFactory,
moduleDescriptor,
diagnosedRanges
)
)
)
}
return diagnostics
}
@@ -37,11 +37,9 @@ public class BindingTraceContext implements BindingTrace {
/* package */ final static boolean TRACK_WITH_STACK_TRACES = true;
private final MutableSlicedMap map;
@Nullable private final MutableDiagnosticsWithSuppression mutableDiagnostics;
@NotNull private final BindingTraceFilter filter;
private final BindingContext bindingContext = new BindingContext() {
private final MutableDiagnosticsWithSuppression mutableDiagnostics;
private final BindingContext bindingContext = new CleanableBindingContext() {
@NotNull
@Override
public Diagnostics getDiagnostics() {
@@ -76,6 +74,11 @@ public class BindingTraceContext implements BindingTrace {
public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) {
BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics);
}
@Override
public void clear() {
map.clear();
}
};
public BindingTraceContext() {
@@ -87,7 +90,6 @@ public class BindingTraceContext implements BindingTrace {
}
public BindingTraceContext(BindingTraceFilter filter, boolean allowSliceRewrite) {
//noinspection ConstantConditions
this(TRACK_REWRITES && !allowSliceRewrite ? new TrackingSlicedMap(TRACK_WITH_STACK_TRACES) : new SlicedMapImpl(allowSliceRewrite), filter);
}
@@ -97,7 +99,6 @@ public class BindingTraceContext implements BindingTrace {
this.mutableDiagnostics = !filter.getIgnoreDiagnostics()
? new MutableDiagnosticsWithSuppression(bindingContext, Diagnostics.Companion.getEMPTY())
: null;
this.filter = filter;
}
@TestOnly
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve
interface CleanableBindingContext : BindingContext {
/**
* Removes all recorded data except diagnostics.
*/
fun clear()
}