From f527fe251c8aea51ce26f69d35fae88fb3a68251 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 19 Jun 2013 15:27:44 +0400 Subject: [PATCH] Replace a mutable map with a memoized function (Premature optimization removed: trove's map replaced by something that will be boxing primitives) --- .../serialization/TypeDeserializer.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java b/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java index 248ce840a0b..72fd9ee9358 100644 --- a/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java +++ b/compiler/frontend/serialization/src/org/jetbrains/jet/descriptors/serialization/TypeDeserializer.java @@ -24,8 +24,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValue; -import org.jetbrains.jet.lang.resolve.lazy.storage.NotNullLazyValueImpl; +import org.jetbrains.jet.lang.resolve.lazy.storage.*; import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; @@ -52,8 +51,11 @@ public class TypeDeserializer { private final NameResolver nameResolver; private final ClassResolver classResolver; private final TypeDeserializer parent; + + // never written to after constructor returns private final TIntObjectHashMap typeParameterDescriptors = new TIntObjectHashMap(); - private final TIntObjectHashMap classDescriptors = new TIntObjectHashMap(); + + private final MemoizedFunctionToNullable classDescriptors; private final String debugName; @@ -80,6 +82,13 @@ public class TypeDeserializer { for (DeserializedTypeParameterDescriptor typeParameterDescriptor : typeParameterResolver.getTypeParameters(this)) { typeParameterDescriptors.put(typeParameterDescriptor.getProtoId(), typeParameterDescriptor); } + + this.classDescriptors = new MemoizedFunctionToNullableImpl() { + @Override + protected ClassDescriptor doCompute(@NotNull Integer fqNameIndex) { + return computeClassDescriptor(fqNameIndex); + } + }; } @NotNull @@ -117,7 +126,7 @@ public class TypeDeserializer { private TypeConstructor typeConstructor(@NotNull ProtoBuf.Type.Constructor proto) { switch (proto.getKind()) { case CLASS: - ClassDescriptor classDescriptor = getClassDescriptor(proto.getId()); + ClassDescriptor classDescriptor = classDescriptors.fun(proto.getId()); if (classDescriptor == null) return null; return classDescriptor.getTypeConstructor(); @@ -134,16 +143,9 @@ public class TypeDeserializer { } @Nullable - private ClassDescriptor getClassDescriptor(int fqNameIndex) { - ClassDescriptor classDescriptor = classDescriptors.get(fqNameIndex); - if (classDescriptor != null) { - return classDescriptor; - } - + private ClassDescriptor computeClassDescriptor(int fqNameIndex) { ClassId classId = nameResolver.getClassId(fqNameIndex); - ClassDescriptor descriptor = classResolver.findClass(classId); - classDescriptors.put(fqNameIndex, descriptor); - return descriptor; + return classResolver.findClass(classId); } private List typeArguments(List protos) {