Memory optimization. Do not put sets into caches

This commit is contained in:
Andrey Breslav
2014-11-05 08:30:32 +02:00
parent 7e1546d3e7
commit 1b8c7aaf3d
3 changed files with 22 additions and 24 deletions
@@ -51,8 +51,8 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
private final MemoizedFunctionToNotNull<Name, List<ClassDescriptor>> classDescriptors;
private final MemoizedFunctionToNotNull<Name, Set<FunctionDescriptor>> functionDescriptors;
private final MemoizedFunctionToNotNull<Name, Set<VariableDescriptor>> propertyDescriptors;
private final MemoizedFunctionToNotNull<Name, Collection<FunctionDescriptor>> functionDescriptors;
private final MemoizedFunctionToNotNull<Name, Collection<VariableDescriptor>> propertyDescriptors;
private final NotNullLazyValue<Collection<DeclarationDescriptor>> descriptorsFromDeclaredElements;
private final NotNullLazyValue<Collection<DeclarationDescriptor>> extraDescriptors;
@@ -76,15 +76,15 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
}
});
this.functionDescriptors = storageManager.createMemoizedFunction(new Function1<Name, Set<FunctionDescriptor>>() {
this.functionDescriptors = storageManager.createMemoizedFunction(new Function1<Name, Collection<FunctionDescriptor>>() {
@Override
public Set<FunctionDescriptor> invoke(Name name) {
public Collection<FunctionDescriptor> invoke(Name name) {
return doGetFunctions(name);
}
});
this.propertyDescriptors = storageManager.createMemoizedFunction(new Function1<Name, Set<VariableDescriptor>>() {
this.propertyDescriptors = storageManager.createMemoizedFunction(new Function1<Name, Collection<VariableDescriptor>>() {
@Override
public Set<VariableDescriptor> invoke(Name name) {
public Collection<VariableDescriptor> invoke(Name name) {
return doGetProperties(name);
}
});
@@ -131,12 +131,12 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
@NotNull
@Override
public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
return functionDescriptors.invoke(name);
}
@NotNull
private Set<FunctionDescriptor> doGetFunctions(@NotNull Name name) {
private Collection<FunctionDescriptor> doGetFunctions(@NotNull Name name) {
Set<FunctionDescriptor> result = Sets.newLinkedHashSet();
Collection<JetNamedFunction> declarations = declarationProvider.getFunctionDeclarations(name);
@@ -154,7 +154,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
getNonDeclaredFunctions(name, result);
return UtilsPackage.toReadOnlySet(result);
return UtilsPackage.toReadOnlyList(result);
}
@NotNull
@@ -164,12 +164,12 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
@NotNull
@Override
public Set<VariableDescriptor> getProperties(@NotNull Name name) {
public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
return propertyDescriptors.invoke(name);
}
@NotNull
public Set<VariableDescriptor> doGetProperties(@NotNull Name name) {
public Collection<VariableDescriptor> doGetProperties(@NotNull Name name) {
Set<VariableDescriptor> result = Sets.newLinkedHashSet();
Collection<JetProperty> declarations = declarationProvider.getPropertyDeclarations(name);
@@ -190,7 +190,7 @@ public abstract class AbstractLazyMemberScope<D extends DeclarationDescriptor, D
getNonDeclaredProperties(name, result);
return UtilsPackage.toReadOnlySet(result);
return UtilsPackage.toReadOnlyList(result);
}
protected abstract void getNonDeclaredProperties(@NotNull Name name, @NotNull Set<VariableDescriptor> result);
@@ -135,9 +135,9 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
@NotNull
@Override
public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
// TODO: this should be handled by lazy function descriptors
Set<FunctionDescriptor> functions = super.getFunctions(name);
Collection<FunctionDescriptor> functions = super.getFunctions(name);
resolveUnknownVisibilitiesForMembers(functions);
return functions;
}
@@ -170,7 +170,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
if (parameter.getType().isError()) continue;
if (!primaryConstructorParameters.get(parameter.getIndex()).hasValOrVarNode()) continue;
Set<VariableDescriptor> properties = getProperties(parameter.getName());
Collection<VariableDescriptor> properties = getProperties(parameter.getName());
if (properties.isEmpty()) continue;
assert properties.size() == 1 :
@@ -205,14 +205,14 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
@NotNull
@Override
@SuppressWarnings("unchecked")
public Set<VariableDescriptor> getProperties(@NotNull Name name) {
public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
// TODO: this should be handled by lazy property descriptors
Set<VariableDescriptor> properties = super.getProperties(name);
resolveUnknownVisibilitiesForMembers((Set) properties);
Collection<VariableDescriptor> properties = super.getProperties(name);
resolveUnknownVisibilitiesForMembers((Collection) properties);
return properties;
}
private void resolveUnknownVisibilitiesForMembers(@NotNull Set<? extends CallableMemberDescriptor> descriptors) {
private void resolveUnknownVisibilitiesForMembers(@NotNull Collection<? extends CallableMemberDescriptor> descriptors) {
for (CallableMemberDescriptor descriptor : descriptors) {
if (descriptor.getKind() != FAKE_OVERRIDE && descriptor.getKind() != DELEGATION) {
OverridingUtil.resolveUnknownVisibilityForMember(descriptor, OverrideResolver.createCannotInferVisibilityReporter(trace));
@@ -328,7 +328,7 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
int n = 1;
while (true) {
Name componentName = DataClassUtilsPackage.createComponentName(n);
Set<FunctionDescriptor> functions = getFunctions(componentName);
Collection<FunctionDescriptor> functions = getFunctions(componentName);
if (functions.isEmpty()) break;
result.addAll(functions);
@@ -21,6 +21,7 @@ import java.util.ArrayList
import java.util.HashMap
import java.util.HashSet
import java.util.Collections
import java.util.LinkedHashSet
public fun <K, V> Stream<V>.valuesToMap(key: (V) -> K): Map<K, V> {
val map = LinkedHashMap<K, V>()
@@ -97,7 +98,4 @@ public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
}
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)
if (isEmpty()) Collections.emptyList() else ArrayList(this)