KT-41346 Introduce inner OptimizedImplementation class

Make it implement `Implementation` and move definitions
there
This commit is contained in:
Roman Golyshev
2020-09-17 17:40:03 +03:00
parent 683dd57f24
commit c8878f862d
@@ -38,8 +38,8 @@ import java.io.ByteArrayOutputStream
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
private interface Implementation { private interface Implementation {
val functionNamesLazy: Set<Name> val functionNames: Set<Name>
val variableNamesLazy: Set<Name> val variableNames: Set<Name>
val typeAliasNames: Set<Name> val typeAliasNames: Set<Name>
fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor>
@@ -60,7 +60,9 @@ abstract class DeserializedMemberScope protected constructor(
propertyList: Collection<ProtoBuf.Property>, propertyList: Collection<ProtoBuf.Property>,
typeAliasList: Collection<ProtoBuf.TypeAlias>, typeAliasList: Collection<ProtoBuf.TypeAlias>,
classNames: () -> Collection<Name> classNames: () -> Collection<Name>
) : MemberScopeImpl(), Implementation { ) : MemberScopeImpl() {
private val impl: Implementation = OptimizedImplementation()
private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray() private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray()
@@ -86,30 +88,20 @@ abstract class DeserializedMemberScope protected constructor(
private val typeAliasByName = private val typeAliasByName =
c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { createTypeAlias(it) } c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { 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<Name> get() = typeAliasBytes.keys
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() } internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
private val classifierNamesLazy by c.storageManager.createNullableLazyValue { private val classifierNamesLazy by c.storageManager.createNullableLazyValue {
val nonDeclaredNames = getNonDeclaredClassifierNames() ?: return@createNullableLazyValue null val nonDeclaredNames = getNonDeclaredClassifierNames() ?: return@createNullableLazyValue null
this.classNames + typeAliasNames + nonDeclaredNames this.classNames + impl.typeAliasNames + nonDeclaredNames
} }
override fun getFunctionNames() = functionNamesLazy override fun getFunctionNames() = impl.functionNames
override fun getVariableNames() = variableNamesLazy override fun getVariableNames() = impl.variableNames
override fun getClassifierNames(): Set<Name>? = classifierNamesLazy override fun getClassifierNames(): Set<Name>? = classifierNamesLazy
override fun definitelyDoesNotContainName(name: Name): Boolean { 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 <M : MessageLite> Collection<M>.groupByName( private inline fun <M : MessageLite> Collection<M>.groupByName(
@@ -163,8 +155,7 @@ abstract class DeserializedMemberScope protected constructor(
} }
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> { override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
if (name !in getFunctionNames()) return emptyList() return impl.getContributedFunctions(name, location)
return functions(name)
} }
private fun computeProperties(name: Name) = private fun computeProperties(name: Name) =
@@ -188,13 +179,12 @@ abstract class DeserializedMemberScope protected constructor(
return c.memberDeserializer.loadTypeAlias(proto) return c.memberDeserializer.loadTypeAlias(proto)
} }
override fun getTypeAliasByName(name: Name): TypeAliasDescriptor? { private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
return typeAliasByName(name) return impl.getTypeAliasByName(name)
} }
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> { override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
if (name !in getVariableNames()) return emptyList() return impl.getContributedVariables(name, location)
return properties(name)
} }
protected fun computeDescriptors( protected fun computeDescriptors(
@@ -221,7 +211,7 @@ abstract class DeserializedMemberScope protected constructor(
} }
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) { if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
for (typeAliasName in typeAliasNames) { for (typeAliasName in impl.typeAliasNames) {
if (nameFilter(typeAliasName)) { if (nameFilter(typeAliasName)) {
result.addIfNotNull(typeAliasByName(typeAliasName)) result.addIfNotNull(typeAliasByName(typeAliasName))
} }
@@ -231,27 +221,13 @@ abstract class DeserializedMemberScope protected constructor(
return result.compact() return result.compact()
} }
override fun addFunctionsAndPropertiesTo( private fun addFunctionsAndPropertiesTo(
result: MutableCollection<DeclarationDescriptor>, result: MutableCollection<DeclarationDescriptor>,
kindFilter: DescriptorKindFilter, kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean, nameFilter: (Name) -> Boolean,
location: LookupLocation location: LookupLocation
) { ) {
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) { impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
addMembers(
getVariableNames(),
nameFilter,
result
) { getContributedVariables(it, location) }
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
addMembers(
getFunctionNames(),
nameFilter,
result
) { getContributedFunctions(it, location) }
}
} }
private inline fun addMembers( private inline fun addMembers(
@@ -274,7 +250,7 @@ abstract class DeserializedMemberScope protected constructor(
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
when { when {
hasClass(name) -> deserializeClass(name) hasClass(name) -> deserializeClass(name)
name in typeAliasNames -> getTypeAliasByName(name) name in impl.typeAliasNames -> getTypeAliasByName(name)
else -> null else -> null
} }
@@ -301,4 +277,53 @@ abstract class DeserializedMemberScope protected constructor(
p.popIndent() p.popIndent()
p.println("}") 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<Name> get() = typeAliasBytes.keys
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
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<PropertyDescriptor> {
if (name !in getVariableNames()) return emptyList()
return properties(name)
}
override fun addFunctionsAndPropertiesTo(
result: MutableCollection<DeclarationDescriptor>,
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) }
}
}
}
} }