From 89c2aca3a8730c39e8bb7a4185330d1832bd190a Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 11 Jun 2015 23:12:15 +0300 Subject: [PATCH] Memory optimization --- .../resolve/scopes/WritableScopeImpl.kt | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt index ec0ad5257a7..4e6bb0a6a6d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/WritableScopeImpl.kt @@ -40,9 +40,9 @@ public class WritableScopeImpl(outerScope: JetScope, private val explicitlyAddedDescriptors = SmartList() - private var functionGroups: MutableMap>? = null + private var functionGroups: MutableMap? = null - private var variableOrClassDescriptors: MutableMap>? = null //TODO: implement SmartIntList + private var variableOrClassDescriptors: MutableMap? = null private var labelsToDescriptors: MutableMap>? = null @@ -130,7 +130,8 @@ public class WritableScopeImpl(outerScope: JetScope, if (variableOrClassDescriptors == null) { variableOrClassDescriptors = HashMap() } - variableOrClassDescriptors!!.getOrPut(descriptor.getName()) { SmartList() }.add(descriptorIndex) + //TODO: could not use += because of KT-8050 + variableOrClassDescriptors!![name] = variableOrClassDescriptors!![name] + descriptorIndex } @@ -157,7 +158,9 @@ public class WritableScopeImpl(outerScope: JetScope, if (functionGroups == null) { functionGroups = HashMap(1) } - functionGroups!!.getOrPut(functionDescriptor.getName()) { SmartList() }.add(descriptorIndex) + val name = functionDescriptor.getName() + //TODO: could not use += because of KT-8050 + functionGroups!![name] = functionGroups!![name] + descriptorIndex } override fun getFunctions(name: Name): Collection { @@ -205,23 +208,28 @@ public class WritableScopeImpl(outerScope: JetScope, override fun getOwnDeclaredDescriptors(): Collection = explicitlyAddedDescriptors private fun variableOrClassDescriptorByName(name: Name, descriptorLimit: Int = explicitlyAddedDescriptors.size()): DeclarationDescriptor? { - val descriptorIndices = variableOrClassDescriptors?.get(name) ?: return null - for (i in descriptorIndices.indices.reversed()) { - val descriptorIndex = descriptorIndices[i] + if (descriptorLimit == 0) return null + + var list = variableOrClassDescriptors?.get(name) + while (list != null) { + val descriptorIndex = list.head if (descriptorIndex < descriptorLimit) { return descriptorIndex.descriptorByIndex() } + list = list.tail } return null } private fun functionsByName(name: Name, descriptorLimit: Int = explicitlyAddedDescriptors.size()): List? { - val descriptorIndices = functionGroups?.get(name) ?: return null - for (i in descriptorIndices.indices.reversed()) { - val descriptorIndex = descriptorIndices[i] - if (descriptorIndex < descriptorLimit) { - return descriptorIndices.truncated(descriptorIndex + 1).map { it.descriptorByIndex() as FunctionDescriptor } + if (descriptorLimit == 0) return null + + var list = functionGroups?.get(name) + while (list != null) { + if (list.head < descriptorLimit) { + return list.toDescriptors() } + list = list.tail } return null } @@ -250,8 +258,19 @@ public class WritableScopeImpl(outerScope: JetScope, p.println("}") } + private class IntList(val head: Int, val tail: IntList?) - private fun List.truncated(newSize: Int) = if (newSize == size()) this else subList(0, newSize) + private fun IntList?.plus(value: Int) = IntList(value, this) + + private fun IntList.toDescriptors(): List { + val result = ArrayList(1) + var rest: IntList? = this + do { + result.add(rest!!.head.descriptorByIndex() as TDescriptor) + rest = rest.tail + } while (rest != null) + return result + } private inner class Snapshot(val descriptorLimit: Int) : JetScope by this@WritableScopeImpl { override fun getDescriptors(kindFilter: DescriptorKindFilter, @@ -297,6 +316,8 @@ public class WritableScopeImpl(outerScope: JetScope, override fun getOwnDeclaredDescriptors(): Collection = explicitlyAddedDescriptors.truncated(descriptorLimit) + private fun List.truncated(newSize: Int) = if (newSize == size()) this else subList(0, newSize) + override fun printScopeStructure(p: Printer) { p.println(javaClass.getSimpleName(), " {") p.pushIndent()