Optimize memory footprint for PartialBodyResolveFilter

There is a single PartialBodyResolveFilter instance per module
and each of them were containing just the same sets of available
in project Nothing-typed functions (including duplicating String instances),
in case of Kotlin project it might sum up to 27M of heap.

The solution is to share the global sets between all modules in a project
This commit is contained in:
Denis Zharkov
2018-04-28 13:00:33 +03:00
parent 26714d56d8
commit 8e1b0c5ab7
10 changed files with 72 additions and 22 deletions
@@ -32,14 +32,15 @@ import java.util.*
class PartialBodyResolveFilter(
elementsToResolve: Collection<KtElement>,
private val declaration: KtDeclaration,
probablyNothingCallableNames: ProbablyNothingCallableNames,
forCompletion: Boolean
) : StatementFilter() {
private val statementMarks = StatementMarks()
private val nothingFunctionNames = HashSet(probablyNothingCallableNames.functionNames())
private val nothingVariableNames = HashSet(probablyNothingCallableNames.propertyNames())
private val globalProbablyNothingCallableNames = ProbablyNothingCallableNames.getInstance(declaration.project)
private val contextNothingFunctionNames = HashSet<String>()
private val contextNothingVariableNames = HashSet<String>()
override val filter: ((KtExpression) -> Boolean)? = { statementMarks.statementMark(it) != MarkLevel.NONE }
@@ -55,10 +56,10 @@ class PartialBodyResolveFilter(
val name = declaration.name
if (name != null) {
if (declaration is KtNamedFunction) {
nothingFunctionNames.add(name)
contextNothingFunctionNames.add(name)
}
else {
nothingVariableNames.add(name)
contextNothingVariableNames.add(name)
}
}
}
@@ -399,7 +400,7 @@ class PartialBodyResolveFilter(
override fun visitCallExpression(expression: KtCallExpression) {
val name = (expression.calleeExpression as? KtSimpleNameExpression)?.getReferencedName()
if (name != null && name in nothingFunctionNames) {
if (name != null && (name in globalProbablyNothingCallableNames.functionNames() || name in contextNothingFunctionNames)) {
result.add(expression)
}
super.visitCallExpression(expression)
@@ -407,7 +408,7 @@ class PartialBodyResolveFilter(
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
val name = expression.getReferencedName()
if (name in nothingVariableNames) {
if (name in globalProbablyNothingCallableNames.propertyNames() || name in contextNothingVariableNames) {
result.add(expression)
}
}
@@ -16,7 +16,14 @@
package org.jetbrains.kotlin.resolve.lazy
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
interface ProbablyNothingCallableNames {
fun functionNames(): Collection<String>
fun propertyNames(): Collection<String>
companion object {
fun getInstance(project: Project) = ServiceManager.getService(project, ProbablyNothingCallableNames::class.java)!!
}
}