Call compactIfPossible where it's possible in BinaryJava*

It helps to avoid retaining a lot of redundant empty SmartList's
instances
This commit is contained in:
Denis Zharkov
2017-05-25 16:39:16 +03:00
parent e2ce285ec3
commit d7cee95d95
3 changed files with 15 additions and 8 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.flattenTo import org.jetbrains.kotlin.utils.addToStdlib.flattenTo
import org.jetbrains.kotlin.utils.compactIfPossible
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
import java.text.CharacterIterator import java.text.CharacterIterator
import java.text.StringCharacterIterator import java.text.StringCharacterIterator
@@ -46,13 +47,13 @@ class BinaryClassSignatureParser(globalContext: ClassifierResolutionContext) {
return emptyList() return emptyList()
} }
val typeParameters = ContainerUtil.newSmartList<JavaTypeParameter>() val typeParameters = ContainerUtil.newArrayList<JavaTypeParameter>()
signature.next() signature.next()
while (signature.current() != '>') { while (signature.current() != '>') {
typeParameters.add(parseTypeParameter(signature, context)) typeParameters.add(parseTypeParameter(signature, context))
} }
signature.next() signature.next()
return typeParameters return typeParameters.compactIfPossible()
} }
private fun parseTypeParameter(signature: CharacterIterator, context: ClassifierResolutionContext): JavaTypeParameter { private fun parseTypeParameter(signature: CharacterIterator, context: ClassifierResolutionContext): JavaTypeParameter {
@@ -151,7 +152,7 @@ class BinaryClassSignatureParser(globalContext: ClassifierResolutionContext) {
return PlainJavaClassifierType( return PlainJavaClassifierType(
{ context.resolveByInternalName(canonicalName.toString()) }, { context.resolveByInternalName(canonicalName.toString()) },
argumentGroups.reversed().flattenTo(ContainerUtil.newSmartList()) argumentGroups.reversed().flattenTo(arrayListOf()).compactIfPossible()
) )
} }
@@ -21,6 +21,7 @@ import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.load.java.structure.* import org.jetbrains.kotlin.load.java.structure.*
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.utils.compactIfPossible
import org.jetbrains.org.objectweb.asm.MethodVisitor import org.jetbrains.org.objectweb.asm.MethodVisitor
import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
@@ -79,7 +80,7 @@ abstract class BinaryJavaMethodBase(
} }
val parameterTypes = info.valueParameterTypes val parameterTypes = info.valueParameterTypes
val parameterList = ContainerUtil.newSmartList<JavaValueParameter>() val parameterList = ContainerUtil.newArrayList<JavaValueParameter>()
val paramCount = parameterTypes.size val paramCount = parameterTypes.size
for (i in 0..paramCount - 1) { for (i in 0..paramCount - 1) {
val type = parameterTypes[i] val type = parameterTypes[i]
@@ -93,7 +94,10 @@ abstract class BinaryJavaMethodBase(
BinaryJavaConstructor(access, containingClass, parameterList, info.typeParameters) BinaryJavaConstructor(access, containingClass, parameterList, info.typeParameters)
else else
BinaryJavaMethod( BinaryJavaMethod(
access, containingClass, parameterList, info.typeParameters, Name.identifier(name), info.returnType access, containingClass,
parameterList.compactIfPossible(),
info.typeParameters,
Name.identifier(name), info.returnType
) )
val paramIgnoreCount = when { val paramIgnoreCount = when {
@@ -126,16 +130,18 @@ abstract class BinaryJavaMethodBase(
if (iterator.current() != '(') throw ClsFormatException() if (iterator.current() != '(') throw ClsFormatException()
iterator.next() iterator.next()
val paramTypes: List<JavaType> var paramTypes: List<JavaType>
if (iterator.current() == ')') { if (iterator.current() == ')') {
paramTypes = emptyList() paramTypes = emptyList()
} }
else { else {
paramTypes = ContainerUtil.newSmartList() paramTypes = ContainerUtil.newArrayList()
while (iterator.current() != ')' && iterator.current() != CharacterIterator.DONE) { while (iterator.current() != ')' && iterator.current() != CharacterIterator.DONE) {
paramTypes.add(signatureParser.parseTypeString(iterator, context)) paramTypes.add(signatureParser.parseTypeString(iterator, context))
} }
if (iterator.current() != ')') throw ClsFormatException() if (iterator.current() != ')') throw ClsFormatException()
paramTypes = (paramTypes as ArrayList).compactIfPossible()
} }
iterator.next() iterator.next()
@@ -132,7 +132,7 @@ inline fun <T> measureTimeMillisWithResult(block: () -> T) : Pair<Long, T> {
return Pair(System.currentTimeMillis() - start, result) return Pair(System.currentTimeMillis() - start, result)
} }
public fun <T> Iterable<Iterable<T>>.flattenTo(c: MutableList<T>): List<T> { fun <T, C : MutableCollection<in T>> Iterable<Iterable<T>>.flattenTo(c: C): C {
for (element in this) { for (element in this) {
c.addAll(element) c.addAll(element)
} }