KT-41346 Move related code to OptimizedImplementation
This does not change any semantics, only moves the code around
This commit is contained in:
+114
-119
@@ -62,31 +62,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
classNames: () -> Collection<Name>
|
classNames: () -> Collection<Name>
|
||||||
) : MemberScopeImpl() {
|
) : MemberScopeImpl() {
|
||||||
|
|
||||||
private val impl: Implementation = OptimizedImplementation()
|
private val impl: Implementation = OptimizedImplementation(functionList, propertyList, typeAliasList)
|
||||||
|
|
||||||
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) }
|
|
||||||
|
|
||||||
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
|
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 getClassifierNames(): Set<Name>? = classifierNamesLazy
|
||||||
|
|
||||||
override fun definitelyDoesNotContainName(name: Name): Boolean {
|
override fun definitelyDoesNotContainName(name: Name): Boolean {
|
||||||
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in impl.typeAliasNames
|
return name !in impl.functionNames && name !in impl.variableNames && name !in classNames && name !in helper.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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,27 +92,9 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
return impl.getContributedFunctions(name, location)
|
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>) {
|
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? {
|
private fun getTypeAliasByName(name: Name): TypeAliasDescriptor? {
|
||||||
return impl.getTypeAliasByName(name)
|
return impl.getTypeAliasByName(name)
|
||||||
}
|
}
|
||||||
@@ -200,7 +116,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
addEnumEntryDescriptors(result, nameFilter)
|
addEnumEntryDescriptors(result, nameFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
|
impl.addFunctionsAndPropertiesTo(result, kindFilter, nameFilter, location)
|
||||||
|
|
||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
|
||||||
for (className in classNames) {
|
for (className in classNames) {
|
||||||
@@ -213,7 +129,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.TYPE_ALIASES_MASK)) {
|
||||||
for (typeAliasName in impl.typeAliasNames) {
|
for (typeAliasName in impl.typeAliasNames) {
|
||||||
if (nameFilter(typeAliasName)) {
|
if (nameFilter(typeAliasName)) {
|
||||||
result.addIfNotNull(typeAliasByName(typeAliasName))
|
result.addIfNotNull(impl.getTypeAliasByName(typeAliasName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,32 +137,6 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
return result.compact()
|
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? =
|
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
|
||||||
when {
|
when {
|
||||||
hasClass(name) -> deserializeClass(name)
|
hasClass(name) -> deserializeClass(name)
|
||||||
@@ -278,7 +168,35 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
p.println("}")
|
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 {
|
override val functionNames by c.storageManager.createLazyValue {
|
||||||
functionProtosBytes.keys + getNonDeclaredFunctionNames()
|
functionProtosBytes.keys + getNonDeclaredFunctionNames()
|
||||||
}
|
}
|
||||||
@@ -289,8 +207,68 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
|
|
||||||
override val typeAliasNames: Set<Name> get() = typeAliasBytes.keys
|
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> {
|
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
|
||||||
if (name !in getFunctionNames()) return emptyList()
|
if (name !in functionNames) return emptyList()
|
||||||
return functions(name)
|
return functions(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,7 +277,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
|
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
|
||||||
if (name !in getVariableNames()) return emptyList()
|
if (name !in variableNames) return emptyList()
|
||||||
return properties(name)
|
return properties(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,7 +289,7 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
) {
|
) {
|
||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.VARIABLES_MASK)) {
|
||||||
addMembers(
|
addMembers(
|
||||||
getVariableNames(),
|
variableNames,
|
||||||
nameFilter,
|
nameFilter,
|
||||||
result
|
result
|
||||||
) { getContributedVariables(it, location) }
|
) { getContributedVariables(it, location) }
|
||||||
@@ -319,11 +297,28 @@ abstract class DeserializedMemberScope protected constructor(
|
|||||||
|
|
||||||
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
|
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
|
||||||
addMembers(
|
addMembers(
|
||||||
getFunctionNames(),
|
functionNames,
|
||||||
nameFilter,
|
nameFilter,
|
||||||
result
|
result
|
||||||
) { getContributedFunctions(it, location) }
|
) { 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