From cc0f2e87d44e836b576c5a50b5cb67d96022242b Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 6 Sep 2016 16:48:21 +0300 Subject: [PATCH] Optimize java scope members computation for non-existing names Do not use memoized function if member with given name is not contained in the scope There are a lot of queries with names of non-existent functions, that leads to many effectively redundant Map nodes in MemoizedFunction and also cause additional computation that is worth to compute at once --- .../load/java/lazy/descriptors/LazyJavaScope.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaScope.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaScope.kt index 89de53f2979..e3af865b65f 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaScope.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaScope.kt @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl import org.jetbrains.kotlin.storage.NotNullLazyValue +import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.utils.Printer @@ -215,11 +216,19 @@ abstract class LazyJavaScope(protected val c: LazyJavaResolverContext) : MemberS return ResolvedValueParameters(descriptors, synthesizedNames) } - override fun getContributedFunctions(name: Name, location: LookupLocation): Collection = functions(name) + private val functionNamesLazy by c.storageManager.createLazyValue { computeFunctionNames(DescriptorKindFilter.FUNCTIONS, null) } + private val propertyNamesLazy by c.storageManager.createLazyValue { computePropertyNames(DescriptorKindFilter.VARIABLES, null) } + override fun getFunctionNames() = functionNamesLazy + override fun getVariableNames() = propertyNamesLazy protected open fun computeFunctionNames(kindFilter: DescriptorKindFilter, nameFilter: ((Name) -> Boolean)?): Set = memberIndex().getMethodNames(nameFilter ?: alwaysTrue()) + override fun getContributedFunctions(name: Name, location: LookupLocation): Collection { + if (name !in getFunctionNames()) return emptyList() + return functions(name) + } + protected abstract fun computeNonDeclaredProperties(name: Name, result: MutableCollection) protected abstract fun computePropertyNames(kindFilter: DescriptorKindFilter, nameFilter: ((Name) -> Boolean)?): Set @@ -289,7 +298,10 @@ abstract class LazyJavaScope(protected val c: LazyJavaResolverContext) : MemberS return propertyType } - override fun getContributedVariables(name: Name, location: LookupLocation): Collection = properties(name) + override fun getContributedVariables(name: Name, location: LookupLocation): Collection { + if (name !in getVariableNames()) return emptyList() + return properties(name) + } override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = allDescriptors()