Optimize deserialized 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

Also drop unused 'location' parameter
This commit is contained in:
Denis Zharkov
2016-09-06 15:23:42 +03:00
parent b1edb01dd4
commit f0ba09ae40
3 changed files with 38 additions and 18 deletions
@@ -251,19 +251,19 @@ class DeserializedClassDescriptor(
})
}
override fun getNonDeclaredFunctionNames(location: LookupLocation): Set<Name> {
override fun getNonDeclaredFunctionNames(): Set<Name> {
return classDescriptor.typeConstructor.supertypes.flatMapTo(LinkedHashSet()) {
it.memberScope.getContributedDescriptors().filterIsInstance<SimpleFunctionDescriptor>().map { it.name }
} + c.components.additionalClassPartsProvider.getFunctionsNames(this@DeserializedClassDescriptor)
it.memberScope.getFunctionNames()
}.apply { addAll(c.components.additionalClassPartsProvider.getFunctionsNames(this@DeserializedClassDescriptor)) }
}
override fun getNonDeclaredVariableNames(location: LookupLocation): Set<Name> {
override fun getNonDeclaredVariableNames(): Set<Name> {
return classDescriptor.typeConstructor.supertypes.flatMapTo(LinkedHashSet()) {
it.memberScope.getContributedDescriptors().filterIsInstance<PropertyDescriptor>().map { it.name }
it.memberScope.getVariableNames()
}
}
override fun getNonDeclaredTypeAliasNames(location: LookupLocation): Set<Name> {
override fun getNonDeclaredTypeAliasNames(): Set<Name> {
return classDescriptor.typeConstructor.supertypes.flatMapTo(LinkedHashSet()) {
it.memberScope.getContributedDescriptors().filterIsInstance<TypeAliasDescriptor>().map { it.name }
}
@@ -339,7 +339,7 @@ class DeserializedClassDescriptor(
fun findEnumEntry(name: Name): ClassDescriptor? = enumEntryByName(name)
private fun computeEnumMemberNames(): Collection<Name> {
private fun computeEnumMemberNames(): Set<Name> {
// NOTE: order of enum entry members should be irrelevant
// because enum entries are effectively invisible to user (as classes)
val result = HashSet<Name>()
@@ -64,6 +64,17 @@ abstract class DeserializedMemberScope protected constructor(
private val typeAliases =
c.storageManager.createMemoizedFunction<Name, Collection<TypeAliasDescriptor>> { computeTypeAliases(it) }
private val functionNamesLazy by c.storageManager.createLazyValue {
functionProtos.keys + getNonDeclaredFunctionNames()
}
private val variableNamesLazy by c.storageManager.createLazyValue {
propertyProtos.keys + getNonDeclaredVariableNames()
}
override fun getFunctionNames() = functionNamesLazy
override fun getVariableNames() = variableNamesLazy
private inline fun <M : MessageLite> Collection<M>.groupByName(
getNameIndex: (M) -> Int
) = groupBy { c.nameResolver.getName(getNameIndex(it)) }
@@ -93,7 +104,10 @@ abstract class DeserializedMemberScope protected constructor(
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<SimpleFunctionDescriptor>) {
}
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = functions(name)
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
if (name !in getFunctionNames()) return emptyList()
return functions(name)
}
private fun computeProperties(name: Name) =
computeDescriptors(
@@ -113,9 +127,15 @@ abstract class DeserializedMemberScope protected constructor(
{ c.memberDeserializer.loadTypeAlias(it) },
{ })
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> = properties(name)
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
if (name !in getVariableNames()) return emptyList()
return properties(name)
}
protected fun getContributedTypeAliases(name: Name): Collection<TypeAliasDescriptor> = typeAliases(name)
protected fun getContributedTypeAliases(name: Name): Collection<TypeAliasDescriptor> {
if (name !in typeAliasNames) return emptyList()
return typeAliases(name)
}
protected abstract fun addClassifierDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean)
@@ -184,13 +204,13 @@ abstract class DeserializedMemberScope protected constructor(
}
protected fun addNonDeclaredDescriptors(result: MutableCollection<DeclarationDescriptor>, location: LookupLocation) {
result.addAll(getNonDeclaredFunctionNames(location).flatMap { getContributedFunctions(it, location) })
result.addAll(getNonDeclaredVariableNames(location).flatMap { getContributedVariables(it, location) })
result.addAll(getNonDeclaredFunctionNames().flatMap { getContributedFunctions(it, location) })
result.addAll(getNonDeclaredVariableNames().flatMap { getContributedVariables(it, location) })
}
protected abstract fun getNonDeclaredFunctionNames(location: LookupLocation): Set<Name>
protected abstract fun getNonDeclaredVariableNames(location: LookupLocation): Set<Name>
protected abstract fun getNonDeclaredTypeAliasNames(location: LookupLocation): Set<Name>
protected abstract fun getNonDeclaredFunctionNames(): Set<Name>
protected abstract fun getNonDeclaredVariableNames(): Set<Name>
protected abstract fun getNonDeclaredTypeAliasNames(): Set<Name>
protected abstract fun addEnumEntryDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean)
@@ -72,9 +72,9 @@ open class DeserializedPackageMemberScope(
}
}
override fun getNonDeclaredFunctionNames(location: LookupLocation): Set<Name> = emptySet()
override fun getNonDeclaredVariableNames(location: LookupLocation): Set<Name> = emptySet()
override fun getNonDeclaredTypeAliasNames(location: LookupLocation): Set<Name> = emptySet()
override fun getNonDeclaredFunctionNames(): Set<Name> = emptySet()
override fun getNonDeclaredVariableNames(): Set<Name> = emptySet()
override fun getNonDeclaredTypeAliasNames(): Set<Name> = emptySet()
override fun addEnumEntryDescriptors(result: MutableCollection<DeclarationDescriptor>, nameFilter: (Name) -> Boolean) {
// Do nothing