From a0a0d9b554cc8e5da3d4afc37fe5341d5df56a5b Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 12 Aug 2016 16:31:24 +0300 Subject: [PATCH] Get rid of DeserializedMemberScope.ProtoKey class Only 'name' property matters, 'isExtension' is only needed for sorting when computing all descriptors (can be replaced with additional sort) --- .../descriptors/DeserializedMemberScope.kt | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedMemberScope.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedMemberScope.kt index 94ea061bdd4..3ccaac0dfc0 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedMemberScope.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedMemberScope.kt @@ -16,15 +16,18 @@ package org.jetbrains.kotlin.serialization.deserialization.descriptors -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor +import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.protobuf.MessageLite +import org.jetbrains.kotlin.resolve.MemberComparator import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext -import org.jetbrains.kotlin.serialization.deserialization.receiverType import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.toReadOnlyList import java.util.* @@ -36,19 +39,17 @@ abstract class DeserializedMemberScope protected constructor( typeAliasList: Collection ) : MemberScopeImpl() { - private data class ProtoKey(val name: Name, val isExtension: Boolean) - private val functionProtos = c.storageManager.createLazyValue { - groupByKey(functionList, { it.name }) { it.receiverType(c.typeTable) != null } + functionList.groupByName { it.name } } private val propertyProtos = c.storageManager.createLazyValue { - groupByKey(propertyList, { it.name }) { it.receiverType(c.typeTable) != null } + propertyList.groupByName { it.name } } private val typeAliasProtos = c.storageManager.createLazyValue { - typeAliasList.groupBy { c.nameResolver.getName(it.name) } + typeAliasList.groupByName { it.name } } protected val typeAliasNames = c.storageManager.createLazyValue { @@ -62,20 +63,12 @@ abstract class DeserializedMemberScope protected constructor( private val typeAliases = c.storageManager.createMemoizedFunction> { computeTypeAliases(it) } - private fun groupByKey( - protos: Collection, getNameIndex: (M) -> Int, isExtension: (M) -> Boolean - ): Map> { - val map = LinkedHashMap>() - for (proto in protos) { - val key = ProtoKey(c.nameResolver.getName(getNameIndex(proto)), isExtension(proto)) - map.getOrPut(key) { ArrayList(1) }.add(proto) - } - return map - } + private inline fun Collection.groupByName( + getNameIndex: (M) -> Int + ) = groupBy { c.nameResolver.getName(getNameIndex(it)) } private fun computeFunctions(name: Name): Collection { - val protos = functionProtos()[ProtoKey(name, isExtension = false)].orEmpty() + - functionProtos()[ProtoKey(name, isExtension = true)].orEmpty() + val protos = functionProtos()[name].orEmpty() val descriptors = protos.mapTo(linkedSetOf()) { c.memberDeserializer.loadFunction(it) @@ -91,8 +84,7 @@ abstract class DeserializedMemberScope protected constructor( override fun getContributedFunctions(name: Name, location: LookupLocation): Collection = functions(name) private fun computeProperties(name: Name): Collection { - val protos = propertyProtos()[ProtoKey(name, isExtension = false)].orEmpty() + - propertyProtos()[ProtoKey(name, isExtension = true)].orEmpty() + val protos = propertyProtos()[name].orEmpty() val descriptors = protos.mapTo(linkedSetOf()) { c.memberDeserializer.loadProperty(it) @@ -148,32 +140,43 @@ abstract class DeserializedMemberScope protected constructor( } private fun addFunctionsAndProperties( - result: LinkedHashSet, + result: MutableCollection, kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, location: LookupLocation ) { if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) { - val keys = propertyProtos().keys.filter { nameFilter(it.name) } - addMembers(result, keys) { getContributedVariables(it, location) } + addMembers( + propertyProtos().keys, + nameFilter, + result + ) { getContributedVariables(it, location) } } if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) { - val keys = functionProtos().keys.filter { nameFilter(it.name) } - addMembers(result, keys) { getContributedFunctions(it, location) } + addMembers( + functionProtos().keys, + nameFilter, + result + ) { getContributedFunctions(it, location) } } } - private fun addMembers( + private inline fun addMembers( + names: Collection, + nameFilter: (Name) -> Boolean, result: MutableCollection, - keys: Collection, - getMembers: (Name) -> Collection + descriptorsByName: (Name) -> Collection ) { - listOf(false, true).forEach { isExtension -> - keys.filter { it.isExtension == isExtension } - .flatMap { getMembers(it.name) } - .filterTo(result) { (it.extensionReceiverParameter != null) == isExtension } + val subResult = ArrayList() + for (name in names) { + if (nameFilter(name)) { + subResult.addAll(descriptorsByName(name)) + } } + + subResult.sortWith(MemberComparator.INSTANCE) + result.addAll(subResult) } protected fun addNonDeclaredDescriptors(result: MutableCollection, location: LookupLocation) {