diff options
author | Karsten Thoms | 2013-09-27 09:13:08 +0000 |
---|---|---|
committer | Karsten Thoms | 2013-09-27 09:13:08 +0000 |
commit | f7cb3c086e5a6e5994791f39f5e7bcfe7b9fe04e (patch) | |
tree | 0283edadf8c1c77acef29e49909ea367e1c0f916 /plugins/org.eclipse.xtend | |
parent | 5460980778143ca4aaa1229cfce157c8e19b7ec9 (diff) | |
download | org.eclipse.xpand-f7cb3c086e5a6e5994791f39f5e7bcfe7b9fe04e.tar.gz org.eclipse.xpand-f7cb3c086e5a6e5994791f39f5e7bcfe7b9fe04e.tar.xz org.eclipse.xpand-f7cb3c086e5a6e5994791f39f5e7bcfe7b9fe04e.zip |
[418154] Improved performance of operationsCache
Introduced Signature class to avoid having a List<Type> as type argument for Pair. This Pair performed badly since equals() and hashCode() is computed over a list.
Diffstat (limited to 'plugins/org.eclipse.xtend')
-rw-r--r-- | plugins/org.eclipse.xtend/src/org/eclipse/xtend/typesystem/AbstractTypeImpl.java | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/plugins/org.eclipse.xtend/src/org/eclipse/xtend/typesystem/AbstractTypeImpl.java b/plugins/org.eclipse.xtend/src/org/eclipse/xtend/typesystem/AbstractTypeImpl.java index 82937e7e..faced7fe 100644 --- a/plugins/org.eclipse.xtend/src/org/eclipse/xtend/typesystem/AbstractTypeImpl.java +++ b/plugins/org.eclipse.xtend/src/org/eclipse/xtend/typesystem/AbstractTypeImpl.java @@ -12,14 +12,12 @@ package org.eclipse.xtend.typesystem; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.internal.xtend.type.baseimpl.PolymorphicResolver; import org.eclipse.internal.xtend.util.Cache; -import org.eclipse.internal.xtend.util.Pair; import org.eclipse.xtend.expression.TypeSystem; public abstract class AbstractTypeImpl implements Type { @@ -52,8 +50,7 @@ public abstract class AbstractTypeImpl implements Type { */ public final Set<Callable> getAllFeatures() { if (allFeatures == null) { - allFeatures = new HashSet<Callable>(); - allFeatures.addAll(Arrays.asList(getContributedFeatures())); + allFeatures = new HashSet<Callable>(Arrays.asList(getContributedFeatures())); for (Type type : getSuperTypes()) { if (type != null) { allFeatures.addAll(type.getAllFeatures()); @@ -102,19 +99,73 @@ public abstract class AbstractTypeImpl implements Type { } } - private static final List<Type> NO_TYPES = Collections.emptyList(); + private static class Signature { + private static final Type[] EMPTY = new Type[0]; - private final Cache<Pair<String, List<Type>>, Operation> operationsCache = new Cache<Pair<String, List<Type>>, Operation>() { + protected final String name; + protected final Type[] parameterTypes; + + Signature(final String name, final Type[] parameterTypes) { + this.name = name; + this.parameterTypes = parameterTypes == null ? EMPTY : parameterTypes; + } + + @Override + public int hashCode() { + int result = name == null ? 0 : name.hashCode(); + for (Type type : parameterTypes) { + result *= 31; + if (type != null) { + result += type.hashCode(); + } + } + return result; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + + if (!(obj instanceof Signature)) { + return false; + } + + Signature signature = (Signature) obj; + String otherName = signature.name; + if ((name != otherName) && (name != null) && !name.equals(otherName)) { + return false; + } + + int length = parameterTypes.length; + Type[] otherParameterTypes = signature.parameterTypes; + if (length != otherParameterTypes.length) { + return false; + } + + for (int i = 0; i < length; ++i) { + Type type = parameterTypes[i]; + Type otherType = otherParameterTypes[i]; + if ((type != otherType) && (type != null) && !type.equals(otherType)) { + return false; + } + } + + return true; + } + } + + private final Cache<Signature, Operation> operationsCache = new Cache<Signature, Operation>() { @Override - protected Operation createNew(final Pair<String, List<Type>> arg0) { - return PolymorphicResolver.getOperation(getAllFeatures(), arg0.getFirst(), AbstractTypeImpl.this, arg0.getSecond()); + protected Operation createNew(final Signature signature) { + return PolymorphicResolver.getOperation(getAllFeatures(), signature.name, AbstractTypeImpl.this, Arrays.asList(signature.parameterTypes)); } }; public Operation getOperation(final String name, final Type[] parameterTypes) { - return operationsCache.get(new Pair<String, List<Type>>(name, (parameterTypes != null) && (parameterTypes.length > 0) ? Arrays - .asList(parameterTypes) : NO_TYPES)); + return operationsCache.get(new Signature(name, parameterTypes)); } public Set<? extends StaticProperty> getAllStaticProperties() { |