mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-20 15:52:36 +00:00
KT-41346 Move related code to OptimizedImplementation
This does not change any semantics, only moves the code around
This commit is contained in:
@@ -62,31 +62,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
classNames: () -> Collection<Name>
|
||||
) : MemberScopeImpl() {
|
||||
|
||||
private val impl: Implementation = OptimizedImplementation()
|
||||
|
||||
private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray()
|
||||
|
||||
private val propertyProtosBytes = propertyList.groupByName { it.name }.packToByteArray()
|
||||
|
||||
private val typeAliasBytes =
|
||||
if (c.components.configuration.typeAliasesAllowed)
|
||||
typeAliasList.groupByName { it.name }.packToByteArray()
|
||||
else
|
||||
emptyMap()
|
||||
|
||||
private fun Map<Name, Collection<AbstractMessageLite>>.packToByteArray(): Map<Name, ByteArray> =
|
||||
mapValues { entry ->
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
entry.value.map { proto -> proto.writeDelimitedTo(byteArrayOutputStream) }
|
||||
byteArrayOutputStream.toByteArray()
|
||||
}
|
||||
|
||||
private val functions =
|
||||
c.storageManager.createMemoizedFunction<Name, Collection<SimpleFunctionDescriptor>> { computeFunctions(it) }
|
||||
private val properties =
|
||||
c.storageManager.createMemoizedFunction<Name, Collection<PropertyDescriptor>> { computeProperties(it) }
|
||||
private val typeAliasByName =
|
||||
c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { createTypeAlias(it) }
|
||||
private val impl: Implementation = OptimizedImplementation(functionList, propertyList, typeAliasList)
|
||||
|
||||
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
|
||||
|
||||
@@ -101,49 +77,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
override fun getClassifierNames(): Set<Name>? = classifierNamesLazy
|
||||
|
||||
override fun definitelyDoesNotContainName(name: Name): Boolean {
|
||||
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in impl.typeAliasNames
|
||||
}
|
||||
|
||||
private inline fun <M : MessageLite> Collection<M>.groupByName(
|
||||
getNameIndex: (M) -> Int
|
||||
) = groupBy { c.nameResolver.getName(getNameIndex(it)) }
|
||||
|
||||
private fun computeFunctions(name: Name) =
|
||||
computeDescriptors(
|
||||
name,
|
||||
functionProtosBytes,
|
||||
ProtoBuf.Function.PARSER,
|
||||
{ c.memberDeserializer.loadFunction(it).takeIf(::isDeclaredFunctionAvailable) },
|
||||
{ computeNonDeclaredFunctions(name, it) }
|
||||
)
|
||||
|
||||
private inline fun <M : MessageLite, D : DeclarationDescriptor> computeDescriptors(
|
||||
name: Name,
|
||||
bytesByName: Map<Name, ByteArray>,
|
||||
parser: Parser<M>,
|
||||
factory: (M) -> D?,
|
||||
computeNonDeclared: (MutableCollection<D>) -> Unit
|
||||
): Collection<D> =
|
||||
computeDescriptors(
|
||||
bytesByName[name]?.let {
|
||||
val inputStream = ByteArrayInputStream(it)
|
||||
generateSequence {
|
||||
parser.parseDelimitedFrom(inputStream, c.components.extensionRegistryLite)
|
||||
}.toList()
|
||||
} ?: emptyList(),
|
||||
factory,
|
||||
computeNonDeclared
|
||||
)
|
||||
|
||||
private inline fun <M : MessageLite, D : DeclarationDescriptor> computeDescriptors(
|
||||
protos: Collection<M>,
|
||||
factory: (M) -> D?,
|
||||
computeNonDeclared: (MutableCollection<D>) -> Unit
|
||||
): Collection<D> {
|
||||
val descriptors = protos.mapNotNullTo(ArrayList(protos.size), factory)
|
||||
|
||||
computeNonDeclared(descriptors)
|
||||
return descriptors.compact()
|
||||
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in helper.typeAliasNames
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,27 +92,9 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
return impl.getContributedFunctions(name, location)
|
||||
}
|
||||
|
||||
private fun computeProperties(name: Name) =
|
||||
computeDescriptors(
|
||||
name,
|
||||
propertyProtosBytes,
|
||||
ProtoBuf.Property.PARSER,
|
||||
{ c.memberDeserializer.loadProperty(it) },
|
||||
{ computeNonDeclaredProperties(name, it) }
|
||||
)
|
||||
|
||||
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
|
||||
}
|
||||
|
||||
private fun createTypeAlias(name: Name): TypeAliasDescriptor? {
|
||||
val byteArray = typeAliasBytes[name] ?: return null
|
||||
val proto =
|
||||
ProtoBuf.TypeAlias.parseDelimitedFrom(
|
||||
ByteArrayInputStream(byteArray), c.components.extensionRegistryLite
|
||||
) ?: return null
|
||||
return c.memberDeserializer.loadTypeAlias(proto)
|
||||
}
|
||||
|
||||
private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
|
||||
return impl.getTypeAliasByName(name)
|
||||
}
|
||||
@@ -200,7 +116,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
addEnumEntryDescriptors(result, nameFilter)
|
||||
}
|
||||
|
||||
addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
|
||||
impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
|
||||
|
||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
|
||||
for (className in classNames) {
|
||||
@@ -213,7 +129,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
|
||||
for (typeAliasName in impl.typeAliasNames) {
|
||||
if (nameFilter(typeAliasName)) {
|
||||
result.addIfNotNull(typeAliasByName(typeAliasName))
|
||||
result.addIfNotNull(impl.getTypeAliasByName(typeAliasName))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,32 +137,6 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
return result.compact()
|
||||
}
|
||||
|
||||
private fun addFunctionsAndPropertiesTo(
|
||||
result: MutableCollection<DeclarationDescriptor>,
|
||||
kindFilter: DescriptorKindFilter,
|
||||
nameFilter: (Name) -> Boolean,
|
||||
location: LookupLocation
|
||||
) {
|
||||
impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
|
||||
}
|
||||
|
||||
private inline fun addMembers(
|
||||
names: Collection<Name>,
|
||||
nameFilter: (Name) -> Boolean,
|
||||
result: MutableCollection<DeclarationDescriptor>,
|
||||
descriptorsByName: (Name) -> Collection<DeclarationDescriptor>
|
||||
) {
|
||||
val subResult = ArrayList<DeclarationDescriptor>()
|
||||
for (name in names) {
|
||||
if (nameFilter(name)) {
|
||||
subResult.addAll(descriptorsByName(name))
|
||||
}
|
||||
}
|
||||
|
||||
subResult.sortWith(MemberComparator.NameAndTypeMemberComparator.INSTANCE)
|
||||
result.addAll(subResult)
|
||||
}
|
||||
|
||||
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
|
||||
when {
|
||||
hasClass(name) -> deserializeClass(name)
|
||||
@@ -278,7 +168,35 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
p.println("}")
|
||||
}
|
||||
|
||||
inner class OptimizedImplementation : Implementation {
|
||||
private inner class OptimizedImplementation(
|
||||
functionList: Collection<ProtoBuf.Function>,
|
||||
propertyList: Collection<ProtoBuf.Property>,
|
||||
typeAliasList: Collection<ProtoBuf.TypeAlias>
|
||||
) : Implementation {
|
||||
private val functionProtosBytes = functionList.groupByName { it.name }.packToByteArray()
|
||||
|
||||
private val propertyProtosBytes = propertyList.groupByName { it.name }.packToByteArray()
|
||||
|
||||
private val typeAliasBytes =
|
||||
if (c.components.configuration.typeAliasesAllowed)
|
||||
typeAliasList.groupByName { it.name }.packToByteArray()
|
||||
else
|
||||
emptyMap()
|
||||
|
||||
private fun Map<Name, Collection<AbstractMessageLite>>.packToByteArray(): Map<Name, ByteArray> =
|
||||
mapValues { entry ->
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
entry.value.map { proto -> proto.writeDelimitedTo(byteArrayOutputStream) }
|
||||
byteArrayOutputStream.toByteArray()
|
||||
}
|
||||
|
||||
private val functions =
|
||||
c.storageManager.createMemoizedFunction<Name, Collection<SimpleFunctionDescriptor>> { computeFunctions(it) }
|
||||
private val properties =
|
||||
c.storageManager.createMemoizedFunction<Name, Collection<PropertyDescriptor>> { computeProperties(it) }
|
||||
private val typeAliasByName =
|
||||
c.storageManager.createMemoizedFunctionWithNullableValues<Name, TypeAliasDescriptor> { createTypeAlias(it) }
|
||||
|
||||
override val functionNames by c.storageManager.createLazyValue {
|
||||
functionProtosBytes.keys + getNonDeclaredFunctionNames()
|
||||
}
|
||||
@@ -289,8 +207,68 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
|
||||
override val typeAliasNames: Set<Name> get() = typeAliasBytes.keys
|
||||
|
||||
private inline fun <M : MessageLite> Collection<M>.groupByName(
|
||||
getNameIndex: (M) -> Int
|
||||
) = groupBy { c.nameResolver.getName(getNameIndex(it)) }
|
||||
|
||||
private fun computeFunctions(name: Name) =
|
||||
computeDescriptors(
|
||||
name,
|
||||
functionProtosBytes,
|
||||
ProtoBuf.Function.PARSER,
|
||||
{ c.memberDeserializer.loadFunction(it).takeIf(::isDeclaredFunctionAvailable) },
|
||||
{ computeNonDeclaredFunctions(name, it) }
|
||||
)
|
||||
|
||||
private inline fun <M : MessageLite, D : DeclarationDescriptor> computeDescriptors(
|
||||
name: Name,
|
||||
bytesByName: Map<Name, ByteArray>,
|
||||
parser: Parser<M>,
|
||||
factory: (M) -> D?,
|
||||
computeNonDeclared: (MutableCollection<D>) -> Unit
|
||||
): Collection<D> =
|
||||
computeDescriptors(
|
||||
bytesByName[name]?.let {
|
||||
val inputStream = ByteArrayInputStream(it)
|
||||
generateSequence {
|
||||
parser.parseDelimitedFrom(inputStream, c.components.extensionRegistryLite)
|
||||
}.toList()
|
||||
} ?: emptyList(),
|
||||
factory,
|
||||
computeNonDeclared
|
||||
)
|
||||
|
||||
private inline fun <M : MessageLite, D : DeclarationDescriptor> computeDescriptors(
|
||||
protos: Collection<M>,
|
||||
factory: (M) -> D?,
|
||||
computeNonDeclared: (MutableCollection<D>) -> Unit
|
||||
): Collection<D> {
|
||||
val descriptors = protos.mapNotNullTo(ArrayList(protos.size), factory)
|
||||
|
||||
computeNonDeclared(descriptors)
|
||||
return descriptors.compact()
|
||||
}
|
||||
|
||||
private fun computeProperties(name: Name) =
|
||||
computeDescriptors(
|
||||
name,
|
||||
propertyProtosBytes,
|
||||
ProtoBuf.Property.PARSER,
|
||||
{ c.memberDeserializer.loadProperty(it) },
|
||||
{ computeNonDeclaredProperties(name, it) }
|
||||
)
|
||||
|
||||
private fun createTypeAlias(name: Name): TypeAliasDescriptor? {
|
||||
val byteArray = typeAliasBytes[name] ?: return null
|
||||
val proto =
|
||||
ProtoBuf.TypeAlias.parseDelimitedFrom(
|
||||
ByteArrayInputStream(byteArray), c.components.extensionRegistryLite
|
||||
) ?: return null
|
||||
return c.memberDeserializer.loadTypeAlias(proto)
|
||||
}
|
||||
|
||||
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
|
||||
if (name !in getFunctionNames()) return emptyList()
|
||||
if (name !in functionNames) return emptyList()
|
||||
return functions(name)
|
||||
}
|
||||
|
||||
@@ -299,7 +277,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
}
|
||||
|
||||
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
|
||||
if (name !in getVariableNames()) return emptyList()
|
||||
if (name !in variableNames) return emptyList()
|
||||
return properties(name)
|
||||
}
|
||||
|
||||
@@ -311,7 +289,7 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
) {
|
||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
|
||||
addMembers(
|
||||
getVariableNames(),
|
||||
variableNames,
|
||||
nameFilter,
|
||||
result
|
||||
) { getContributedVariables(it, location) }
|
||||
@@ -319,11 +297,28 @@ abstract class DeserializedMemberScope protected constructor(
|
||||
|
||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
|
||||
addMembers(
|
||||
getFunctionNames(),
|
||||
functionNames,
|
||||
nameFilter,
|
||||
result
|
||||
) { getContributedFunctions(it, location) }
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun addMembers(
|
||||
names: Collection<Name>,
|
||||
nameFilter: (Name) -> Boolean,
|
||||
result: MutableCollection<DeclarationDescriptor>,
|
||||
descriptorsByName: (Name) -> Collection<DeclarationDescriptor>
|
||||
) {
|
||||
val subResult = ArrayList<DeclarationDescriptor>()
|
||||
for (name in names) {
|
||||
if (nameFilter(name)) {
|
||||
subResult.addAll(descriptorsByName(name))
|
||||
}
|
||||
}
|
||||
|
||||
subResult.sortWith(MemberComparator.NameAndTypeMemberComparator.INSTANCE)
|
||||
result.addAll(subResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user