From c8878f862d993d285592e7280241f4e1ef083407 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Thu, 17 Sep 2020 17:40:03 +0300 Subject: [PATCH] KT-41346 Introduce inner `OptimizedImplementation` class Make it implement `Implementation` and move definitions there --- .../descriptors/DeserializedMemberScope.kt | 107 +++++++++++------- 1 file changed, 66 insertions(+), 41 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 6f40d32d0ff..a2a1a8a716f 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 @@ -38,8 +38,8 @@ import java.io.ByteArrayOutputStream import kotlin.collections.ArrayList private interface Implementation { - val functionNamesLazy: Set - val variableNamesLazy: Set + val functionNames: Set + val variableNames: Set val typeAliasNames: Set fun getContributedFunctions(name: Name, location: LookupLocation): Collection @@ -60,7 +60,9 @@ abstract class DeserializedMemberScope protected constructor( propertyList: Collection, typeAliasList: Collection, classNames: () -> Collection -) : MemberScopeImpl(), Implementation { +) : MemberScopeImpl() { + + private val impl: Implementation = OptimizedImplementation() private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray() @@ -86,30 +88,20 @@ abstract class DeserializedMemberScope protected constructor( private val typeAliasByName = c.storageManager.createMemoizedFunctionWithNullableValues { createTypeAlias(it) } - override val functionNamesLazy by c.storageManager.createLazyValue { - functionProtosBytes.keys + getNonDeclaredFunctionNames() - } - - override val variableNamesLazy by c.storageManager.createLazyValue { - propertyProtosBytes.keys + getNonDeclaredVariableNames() - } - - override val typeAliasNames: Set get() = typeAliasBytes.keys - internal val classNames by c.storageManager.createLazyValue { classNames().toSet() } private val classifierNamesLazy by c.storageManager.createNullableLazyValue { val nonDeclaredNames = getNonDeclaredClassifierNames() ?: return@createNullableLazyValue null - this.classNames + typeAliasNames + nonDeclaredNames + this.classNames + impl.typeAliasNames + nonDeclaredNames } - override fun getFunctionNames() = functionNamesLazy - override fun getVariableNames() = variableNamesLazy + override fun getFunctionNames() = impl.functionNames + override fun getVariableNames() = impl.variableNames override fun getClassifierNames(): Set? = classifierNamesLazy override fun definitelyDoesNotContainName(name: Name): Boolean { - return name !in functionNamesLazy && name !in variableNamesLazy && name !in classNames && name !in typeAliasNames + return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in impl.typeAliasNames } private inline fun Collection.groupByName( @@ -163,8 +155,7 @@ abstract class DeserializedMemberScope protected constructor( } override fun getContributedFunctions(name: Name, location: LookupLocation): Collection { - if (name !in getFunctionNames()) return emptyList() - return functions(name) + return impl.getContributedFunctions(name, location) } private fun computeProperties(name: Name) = @@ -188,13 +179,12 @@ abstract class DeserializedMemberScope protected constructor( return c.memberDeserializer.loadTypeAlias(proto) } - override fun getTypeAliasByName(name: Name): TypeAliasDescriptor? { - return typeAliasByName(name) + private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? { + return impl.getTypeAliasByName(name) } override fun getContributedVariables(name: Name, location: LookupLocation): Collection { - if (name !in getVariableNames()) return emptyList() - return properties(name) + return impl.getContributedVariables(name, location) } protected fun computeDescriptors( @@ -221,7 +211,7 @@ abstract class DeserializedMemberScope protected constructor( } if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) { - for (typeAliasName in typeAliasNames) { + for (typeAliasName in impl.typeAliasNames) { if (nameFilter(typeAliasName)) { result.addIfNotNull(typeAliasByName(typeAliasName)) } @@ -231,27 +221,13 @@ abstract class DeserializedMemberScope protected constructor( return result.compact() } - override fun addFunctionsAndPropertiesTo( + private fun addFunctionsAndPropertiesTo( result: MutableCollection, kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, location: LookupLocation ) { - if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) { - addMembers( - getVariableNames(), - nameFilter, - result - ) { getContributedVariables(it, location) } - } - - if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) { - addMembers( - getFunctionNames(), - nameFilter, - result - ) { getContributedFunctions(it, location) } - } + impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location) } private inline fun addMembers( @@ -274,7 +250,7 @@ abstract class DeserializedMemberScope protected constructor( override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = when { hasClass(name) -> deserializeClass(name) - name in typeAliasNames -> getTypeAliasByName(name) + name in impl.typeAliasNames -> getTypeAliasByName(name) else -> null } @@ -301,4 +277,53 @@ abstract class DeserializedMemberScope protected constructor( p.popIndent() p.println("}") } + + inner class OptimizedImplementation : Implementation { + override val functionNames by c.storageManager.createLazyValue { + functionProtosBytes.keys + getNonDeclaredFunctionNames() + } + + override val variableNames by c.storageManager.createLazyValue { + propertyProtosBytes.keys + getNonDeclaredVariableNames() + } + + override val typeAliasNames: Set get() = typeAliasBytes.keys + + override fun getContributedFunctions(name: Name, location: LookupLocation): Collection { + if (name !in getFunctionNames()) return emptyList() + return functions(name) + } + + override fun getTypeAliasByName(name: Name): TypeAliasDescriptor? { + return typeAliasByName(name) + } + + override fun getContributedVariables(name: Name, location: LookupLocation): Collection { + if (name !in getVariableNames()) return emptyList() + return properties(name) + } + + override fun addFunctionsAndPropertiesTo( + result: MutableCollection, + kindFilter: DescriptorKindFilter, + nameFilter: (Name) -> Boolean, + location: LookupLocation + ) { + if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) { + addMembers( + getVariableNames(), + nameFilter, + result + ) { getContributedVariables(it, location) } + } + + if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) { + addMembers( + getFunctionNames(), + nameFilter, + result + ) { getContributedFunctions(it, location) } + } + } + } }