Merge stored empty lists and sets to singe object

This commit is contained in:
Nikolay Krasko
2014-11-02 01:15:07 +03:00
parent 87d372aad5
commit 7e1546d3e7
6 changed files with 28 additions and 19 deletions
@@ -39,6 +39,7 @@ import org.jetbrains.jet.storage.MemoizedFunctionToNotNull;
import org.jetbrains.jet.storage.NotNullLazyValue; import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager; import org.jetbrains.jet.storage.StorageManager;
import org.jetbrains.jet.utils.Printer; import org.jetbrains.jet.utils.Printer;
import org.jetbrains.jet.utils.UtilsPackage;
import java.util.*; import java.util.*;
@@ -106,7 +107,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
private List<ClassDescriptor> resolveClassDescriptor(@NotNull final Name name) { private List<ClassDescriptor> resolveClassDescriptor(@NotNull final Name name) {
Collection<JetClassLikeInfo> classOrObjectDeclarations = declarationProvider.getClassOrObjectDeclarations(name); Collection<JetClassLikeInfo> classOrObjectDeclarations = declarationProvider.getClassOrObjectDeclarations(name);
return ContainerUtil.mapNotNull(classOrObjectDeclarations, new Function<JetClassLikeInfo, ClassDescriptor>() { return UtilsPackage.toReadOnlyList(ContainerUtil.mapNotNull(classOrObjectDeclarations, new Function<JetClassLikeInfo, ClassDescriptor>() {
@Override @Override
public ClassDescriptor fun(JetClassLikeInfo classLikeInfo) { public ClassDescriptor fun(JetClassLikeInfo classLikeInfo) {
// SCRIPT: Creating a script class // SCRIPT: Creating a script class
@@ -115,7 +116,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
} }
return new LazyClassDescriptor(resolveSession, thisDescriptor, name, classLikeInfo); return new LazyClassDescriptor(resolveSession, thisDescriptor, name, classLikeInfo);
} }
}); }));
} }
@Override @Override
@@ -153,7 +154,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
getNonDeclaredFunctions(name, result); getNonDeclaredFunctions(name, result);
return result; return UtilsPackage.toReadOnlySet(result);
} }
@NotNull @NotNull
@@ -189,7 +190,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
getNonDeclaredProperties(name, result); getNonDeclaredProperties(name, result);
return result; return UtilsPackage.toReadOnlySet(result);
} }
protected abstract void getNonDeclaredProperties(@NotNull Name name, @NotNull Set<VariableDescriptor> result); protected abstract void getNonDeclaredProperties(@NotNull Name name, @NotNull Set<VariableDescriptor> result);
@@ -252,8 +253,8 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
throw new IllegalArgumentException("Unsupported declaration kind: " + declaration); throw new IllegalArgumentException("Unsupported declaration kind: " + declaration);
} }
} }
result.trimToSize();
return result; return UtilsPackage.toReadOnlyList(result);
} }
@NotNull @NotNull
@@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.types.DeferredType;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.storage.NullableLazyValue; import org.jetbrains.jet.storage.NullableLazyValue;
import org.jetbrains.jet.utils.UtilsPackage;
import java.util.*; import java.util.*;
@@ -299,7 +300,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
@Override @Override
@NotNull @NotNull
protected Collection<DeclarationDescriptor> computeExtraDescriptors() { protected Collection<DeclarationDescriptor> computeExtraDescriptors() {
ArrayList<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>(); List<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>();
for (JetType supertype : thisDescriptor.getTypeConstructor().getSupertypes()) { for (JetType supertype : thisDescriptor.getTypeConstructor().getSupertypes()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) { for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) {
if (descriptor instanceof FunctionDescriptor) { if (descriptor instanceof FunctionDescriptor) {
@@ -314,8 +315,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
addDataClassMethods(result); addDataClassMethods(result);
result.trimToSize(); return UtilsPackage.toReadOnlyList(result);
return result;
} }
private void addDataClassMethods(@NotNull Collection<DeclarationDescriptor> result) { private void addDataClassMethods(@NotNull Collection<DeclarationDescriptor> result) {
@@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.resolve.lazy.data.JetScriptInfo;
import org.jetbrains.jet.lang.resolve.lazy.declarations.ClassMemberDeclarationProvider; import org.jetbrains.jet.lang.resolve.lazy.declarations.ClassMemberDeclarationProvider;
import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.storage.NotNullLazyValue; import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.utils.UtilsPackage;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
@@ -61,13 +62,13 @@ public class LazyScriptClassMemberScope extends LazyClassMemberScope {
@NotNull @NotNull
@Override @Override
protected Collection<DeclarationDescriptor> computeExtraDescriptors() { protected Collection<DeclarationDescriptor> computeExtraDescriptors() {
return KotlinPackage.plus( return UtilsPackage.toReadOnlyList(KotlinPackage.plus(
super.computeExtraDescriptors(), super.computeExtraDescriptors(),
KotlinPackage.plus( KotlinPackage.plus(
getProperties(Name.identifier(ScriptDescriptor.LAST_EXPRESSION_VALUE_FIELD_NAME)), getProperties(Name.identifier(ScriptDescriptor.LAST_EXPRESSION_VALUE_FIELD_NAME)),
getPropertiesForScriptParameters() getPropertiesForScriptParameters()
) )
); ));
} }
private Collection<VariableDescriptor> getPropertiesForScriptParameters() { private Collection<VariableDescriptor> getPropertiesForScriptParameters() {
@@ -100,7 +100,7 @@ public abstract class LazyJavaMemberScope(
} }
} }
ArrayList(functions) functions.toReadOnlyList()
} }
data class MethodSignatureData( data class MethodSignatureData(
@@ -237,8 +237,7 @@ public abstract class LazyJavaMemberScope(
computeNonDeclaredProperties(name, properties) computeNonDeclaredProperties(name, properties)
properties.trimToSize() properties.toReadOnlyList()
properties
} }
private fun resolveProperty(field: JavaField): PropertyDescriptor { private fun resolveProperty(field: JavaField): PropertyDescriptor {
@@ -303,7 +302,7 @@ public abstract class LazyJavaMemberScope(
override fun getOwnDeclaredDescriptors() = getAllDescriptors() override fun getOwnDeclaredDescriptors() = getAllDescriptors()
override fun getAllDescriptors() = _allDescriptors() override fun getAllDescriptors() = _allDescriptors()
private fun computeAllDescriptors(): MutableCollection<DeclarationDescriptor> { private fun computeAllDescriptors(): List<DeclarationDescriptor> {
val result = LinkedHashSet<DeclarationDescriptor>() val result = LinkedHashSet<DeclarationDescriptor>()
for (name in getAllClassNames()) { for (name in getAllClassNames()) {
@@ -324,7 +323,7 @@ public abstract class LazyJavaMemberScope(
addExtraDescriptors(result) addExtraDescriptors(result)
return ArrayList(result) return result.toReadOnlyList()
} }
protected open fun addExtraDescriptors(result: MutableSet<DeclarationDescriptor>) { protected open fun addExtraDescriptors(result: MutableSet<DeclarationDescriptor>) {
@@ -31,6 +31,7 @@ import org.jetbrains.jet.storage.MemoizedFunctionToNotNull;
import org.jetbrains.jet.storage.NotNullLazyValue; import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager; import org.jetbrains.jet.storage.StorageManager;
import org.jetbrains.jet.utils.Printer; import org.jetbrains.jet.utils.Printer;
import org.jetbrains.jet.utils.UtilsPackage;
import java.util.*; import java.util.*;
@@ -135,7 +136,7 @@ public abstract class DeserializedMemberScope implements JetScope {
private Collection<FunctionDescriptor> computeFunctions(@NotNull Name name) { private Collection<FunctionDescriptor> computeFunctions(@NotNull Name name) {
Collection<FunctionDescriptor> descriptors = computeMembersByName(name, FUNCTION); Collection<FunctionDescriptor> descriptors = computeMembersByName(name, FUNCTION);
computeNonDeclaredFunctions(name, descriptors); computeNonDeclaredFunctions(name, descriptors);
return new ArrayList<FunctionDescriptor>(descriptors); return UtilsPackage.toReadOnlyList(descriptors);
} }
protected void computeNonDeclaredFunctions(@NotNull Name name, @NotNull Collection<FunctionDescriptor> functions) { protected void computeNonDeclaredFunctions(@NotNull Name name, @NotNull Collection<FunctionDescriptor> functions) {
@@ -152,7 +153,7 @@ public abstract class DeserializedMemberScope implements JetScope {
Collection<PropertyDescriptor> descriptors = computeMembersByName(name, PROPERTY); Collection<PropertyDescriptor> descriptors = computeMembersByName(name, PROPERTY);
computeNonDeclaredProperties(name, descriptors); computeNonDeclaredProperties(name, descriptors);
//noinspection unchecked //noinspection unchecked
return new ArrayList<VariableDescriptor>((Collection) descriptors); return UtilsPackage.<VariableDescriptor>toReadOnlyList(descriptors);
} }
protected void computeNonDeclaredProperties(@NotNull Name name, @NotNull Collection<PropertyDescriptor> descriptors) { protected void computeNonDeclaredProperties(@NotNull Name name, @NotNull Collection<PropertyDescriptor> descriptors) {
@@ -211,7 +212,7 @@ public abstract class DeserializedMemberScope implements JetScope {
addAllClassDescriptors(result); addAllClassDescriptors(result);
return result; return UtilsPackage.toReadOnlyList(result);
} }
protected abstract void addNonDeclaredDescriptors(@NotNull Collection<DeclarationDescriptor> result); protected abstract void addNonDeclaredDescriptors(@NotNull Collection<DeclarationDescriptor> result);
@@ -20,6 +20,7 @@ import java.util.LinkedHashMap
import java.util.ArrayList import java.util.ArrayList
import java.util.HashMap import java.util.HashMap
import java.util.HashSet import java.util.HashSet
import java.util.Collections
public fun <K, V> Stream<V>.valuesToMap(key: (V) -> K): Map<K, V> { public fun <K, V> Stream<V>.valuesToMap(key: (V) -> K): Map<K, V> {
val map = LinkedHashMap<K, V>() val map = LinkedHashMap<K, V>()
@@ -94,3 +95,9 @@ public fun <K, V> newHashMapWithExpectedSize(expectedSize: Int): HashMap<K, V> {
public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> { public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
return HashSet(if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1) return HashSet(if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1)
} }
public fun <T> Collection<T>.toReadOnlyList(): List<T> =
if (isEmpty()) Collections.emptyList() else ArrayList(this)
public fun <T> Collection<T>.toReadOnlySet(): Set<T> =
if (isEmpty()) Collections.emptySet() else HashSet(this)