Add capacities to some ArrayList instances in project

The changed lists were visible in the profiler memory snapshot. It's unclear
what this will save, but each of these lists was retaining at least several
megabytes of memory
This commit is contained in:
Alexander Udalov
2015-08-06 04:56:06 +03:00
parent dce99e2fef
commit fd006c521f
8 changed files with 35 additions and 29 deletions
@@ -41,7 +41,10 @@ import org.jetbrains.kotlin.types.Variance;
import org.jetbrains.kotlin.types.checker.JetTypeChecker; import org.jetbrains.kotlin.types.checker.JetTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilPackage; import org.jetbrains.kotlin.types.typeUtil.TypeUtilPackage;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.jetbrains.kotlin.load.java.components.TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT; import static org.jetbrains.kotlin.load.java.components.TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT;
import static org.jetbrains.kotlin.load.java.components.TypeUsage.UPPER_BOUND; import static org.jetbrains.kotlin.load.java.components.TypeUsage.UPPER_BOUND;
@@ -188,8 +191,8 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
parameterDescriptors.size(), altFunDeclaration.getValueParameters().size()); parameterDescriptors.size(), altFunDeclaration.getValueParameters().size());
} }
List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>(parameterDescriptors.size());
for (int i = 0, size = parameterDescriptors.size(); i < size; i++) { for (int i = 0; i < parameterDescriptors.size(); i++) {
ValueParameterDescriptor originalParameterDescriptor = parameterDescriptors.get(i); ValueParameterDescriptor originalParameterDescriptor = parameterDescriptors.get(i);
JetParameter annotationValueParameter = altFunDeclaration.getValueParameters().get(i); JetParameter annotationValueParameter = altFunDeclaration.getValueParameters().get(i);
@@ -231,7 +234,7 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
originalParameterDescriptor.declaresDefaultValue(), originalParameterDescriptor.declaresDefaultValue(),
alternativeVarargElementType, alternativeVarargElementType,
SourceElement.NO_SOURCE SourceElement.NO_SOURCE
)); ));
} }
altValueParameters = altParamDescriptors; altValueParameters = altParamDescriptors;
@@ -243,9 +246,9 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
typeParameters.size(), altFunDeclaration.getTypeParameters().size()); typeParameters.size(), altFunDeclaration.getTypeParameters().size());
} }
altTypeParameters = new ArrayList<TypeParameterDescriptor>(); altTypeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
for (int i = 0, size = typeParameters.size(); i < size; i++) { for (int i = 0; i < typeParameters.size(); i++) {
TypeParameterDescriptor originalTypeParamDescriptor = typeParameters.get(i); TypeParameterDescriptor originalTypeParamDescriptor = typeParameters.get(i);
TypeParameterDescriptorImpl altParamDescriptor = originalToAltTypeParameters.get(originalTypeParamDescriptor); TypeParameterDescriptorImpl altParamDescriptor = originalToAltTypeParameters.get(originalTypeParamDescriptor);
@@ -216,7 +216,7 @@ public class SignaturesPropagationData {
DescriptorUtils.getFqName(containingClass); DescriptorUtils.getFqName(containingClass);
JetType resultReceiverType = null; JetType resultReceiverType = null;
List<ValueParameterDescriptor> resultParameters = Lists.newArrayList(); List<ValueParameterDescriptor> resultParameters = new ArrayList<ValueParameterDescriptor>(parameters.size());
boolean shouldBeExtension = checkIfShouldBeExtension(); boolean shouldBeExtension = checkIfShouldBeExtension();
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.psi.Call.CallType;
import org.jetbrains.kotlin.psi.debugText.DebugTextPackage; import org.jetbrains.kotlin.psi.debugText.DebugTextPackage;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue; import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -199,7 +200,7 @@ public class CallMaker {
public static Call makeCallWithExpressions(@NotNull JetElement callElement, @NotNull ReceiverValue explicitReceiver, public static Call makeCallWithExpressions(@NotNull JetElement callElement, @NotNull ReceiverValue explicitReceiver,
@Nullable ASTNode callOperationNode, @NotNull JetExpression calleeExpression, @Nullable ASTNode callOperationNode, @NotNull JetExpression calleeExpression,
@NotNull List<JetExpression> argumentExpressions, @NotNull CallType callType) { @NotNull List<JetExpression> argumentExpressions, @NotNull CallType callType) {
List<ValueArgument> arguments = Lists.newArrayList(); List<ValueArgument> arguments = new ArrayList<ValueArgument>(argumentExpressions.size());
for (JetExpression argumentExpression : argumentExpressions) { for (JetExpression argumentExpression : argumentExpressions) {
arguments.add(makeValueArgument(argumentExpression, calleeExpression)); arguments.add(makeValueArgument(argumentExpression, calleeExpression));
} }
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.createProjection import org.jetbrains.kotlin.types.typeUtil.createProjection
import org.jetbrains.kotlin.utils.toReadOnlyList
// The index in the lambda is the position of the type component: // The index in the lambda is the position of the type component:
// Example: for `A<B, C<D, E>>`, indices go as follows: `0 - A<...>, 1 - B, 2 - C<D, E>, 3 - D, 4 - E`, // Example: for `A<B, C<D, E>>`, indices go as follows: `0 - A<...>, 1 - B, 2 - C<D, E>, 3 - D, 4 - E`,
@@ -119,7 +120,7 @@ private fun JetType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers, i
private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size()) { private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size()) {
0 -> error("At least one Annotations object expected") 0 -> error("At least one Annotations object expected")
1 -> single() 1 -> single()
else -> CompositeAnnotations(this) else -> CompositeAnnotations(this.toReadOnlyList())
} }
private fun TypeComponentPosition.shouldEnhance() = this != TypeComponentPosition.INFLEXIBLE private fun TypeComponentPosition.shouldEnhance() = this != TypeComponentPosition.INFLEXIBLE
@@ -739,7 +739,7 @@ public class KotlinBuiltIns {
@NotNull List<JetType> parameterTypes, @NotNull List<JetType> parameterTypes,
@NotNull JetType returnType @NotNull JetType returnType
) { ) {
List<TypeProjection> arguments = new ArrayList<TypeProjection>(); List<TypeProjection> arguments = new ArrayList<TypeProjection>(parameterTypes.size() + (receiverType != null ? 1 : 0) + 1);
if (receiverType != null) { if (receiverType != null) {
arguments.add(defaultProjection(receiverType)); arguments.add(defaultProjection(receiverType));
} }
@@ -302,23 +302,25 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
@NotNull TypeSubstitutor substitutor @NotNull TypeSubstitutor substitutor
) { ) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(unsubstitutedValueParameters.size());
for (ValueParameterDescriptor unsubstitutedValueParameter : unsubstitutedValueParameters) { for (ValueParameterDescriptor unsubstitutedValueParameter : unsubstitutedValueParameters) {
// TODO : Lazy? // TODO : Lazy?
JetType substitutedType = substitutor.substitute(unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE); JetType substitutedType = substitutor.substitute(unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE);
JetType varargElementType = unsubstitutedValueParameter.getVarargElementType(); JetType varargElementType = unsubstitutedValueParameter.getVarargElementType();
JetType substituteVarargElementType = varargElementType == null ? null : substitutor.substitute(varargElementType, Variance.IN_VARIANCE); JetType substituteVarargElementType =
varargElementType == null ? null : substitutor.substitute(varargElementType, Variance.IN_VARIANCE);
if (substitutedType == null) return null; if (substitutedType == null) return null;
result.add(new ValueParameterDescriptorImpl( result.add(
substitutedDescriptor, new ValueParameterDescriptorImpl(
unsubstitutedValueParameter, substitutedDescriptor,
unsubstitutedValueParameter.getIndex(), unsubstitutedValueParameter,
unsubstitutedValueParameter.getAnnotations(), unsubstitutedValueParameter.getIndex(),
unsubstitutedValueParameter.getName(), unsubstitutedValueParameter.getAnnotations(),
substitutedType, unsubstitutedValueParameter.getName(),
unsubstitutedValueParameter.declaresDefaultValue(), substitutedType,
substituteVarargElementType, unsubstitutedValueParameter.declaresDefaultValue(),
SourceElement.NO_SOURCE substituteVarargElementType,
SourceElement.NO_SOURCE
) )
); );
} }
@@ -212,12 +212,11 @@ public class CommonSupertypes {
} }
List<TypeParameterDescriptor> parameters = constructor.getParameters(); List<TypeParameterDescriptor> parameters = constructor.getParameters();
List<TypeProjection> newProjections = new ArrayList<TypeProjection>(); List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
for (int i = 0, parametersSize = parameters.size(); i < parametersSize; i++) { for (TypeParameterDescriptor parameterDescriptor : parameters) {
TypeParameterDescriptor parameterDescriptor = parameters.get(i);
Set<TypeProjection> typeProjections = new HashSet<TypeProjection>(); Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
for (JetType type : types) { for (JetType type : types) {
typeProjections.add(type.getArguments().get(i)); typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
} }
newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth)); newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
} }
@@ -425,12 +425,12 @@ public class TypeUtils {
} }
@NotNull @NotNull
public static List<TypeProjection> getDefaultTypeProjections(List<TypeParameterDescriptor> parameters) { public static List<TypeProjection> getDefaultTypeProjections(@NotNull List<TypeParameterDescriptor> parameters) {
List<TypeProjection> result = new ArrayList<TypeProjection>(); List<TypeProjection> result = new ArrayList<TypeProjection>(parameters.size());
for (TypeParameterDescriptor parameterDescriptor : parameters) { for (TypeParameterDescriptor parameterDescriptor : parameters) {
result.add(new TypeProjectionImpl(parameterDescriptor.getDefaultType())); result.add(new TypeProjectionImpl(parameterDescriptor.getDefaultType()));
} }
return result; return UtilsPackage.toReadOnlyList(result);
} }
@NotNull @NotNull