equals / hashcode optimization

This commit is contained in:
Florian Steurer 2016-04-21 22:19:48 +02:00
parent b8415b122b
commit bb8df92cba
8 changed files with 79 additions and 14 deletions

View File

@ -14,7 +14,7 @@ public final class ExtendsType extends WildcardType {
* @param extendedType The extended type e.g. Integer in "? extends Integer" * @param extendedType The extended type e.g. Integer in "? extends Integer"
*/ */
public ExtendsType(UnifyType extendedType) { public ExtendsType(UnifyType extendedType) {
super("? extends " + extendedType.getName(), extendedType); super("? extends " + extendedType.getName(), extendedType);
} }
/** /**
@ -44,7 +44,10 @@ public final class ExtendsType extends WildcardType {
@Override @Override
UnifyType apply(Unifier unif) { UnifyType apply(Unifier unif) {
return new ExtendsType(wildcardedType.apply(unif)); UnifyType newType = wildcardedType.apply(unif);
/*if(newType.hashCode() == wildcardedType.hashCode() && newType.equals(wildcardedType))
return this; // Reduced the amount of objects created*/
return new ExtendsType(newType);
} }
@Override @Override
@ -61,7 +64,12 @@ public final class ExtendsType extends WildcardType {
if(!(obj instanceof ExtendsType)) if(!(obj instanceof ExtendsType))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
ExtendsType other = (ExtendsType) obj; ExtendsType other = (ExtendsType) obj;
return other.getWildcardedType().equals(wildcardedType); return other.getWildcardedType().equals(wildcardedType);
} }

View File

@ -33,7 +33,6 @@ public class FiniteClosure implements IFiniteClosure {
*/ */
private Set<UnifyPair> pairs; private Set<UnifyPair> pairs;
//TODO Prüfen: Typen ohne Kante im Graph als extra Menge im Konstruktor mitgeben?
/** /**
* Creates a new instance using the inheritance tree defined in the pairs. * Creates a new instance using the inheritance tree defined in the pairs.
*/ */

View File

@ -58,7 +58,11 @@ public class FunNType extends UnifyType {
UnifyType apply(Unifier unif) { UnifyType apply(Unifier unif) {
// TODO this bypasses the validation of the type parameters. // TODO this bypasses the validation of the type parameters.
// Wildcard types can be unified into FunNTypes. // Wildcard types can be unified into FunNTypes.
return new FunNType(typeParams.apply(unif)); TypeParams newParams = typeParams.apply(unif);
/*if(newParams.hashCode() == typeParams.hashCode() && newParams.equals(typeParams))
return this;*/
return new FunNType(newParams);
} }
@Override @Override
@ -71,8 +75,11 @@ public class FunNType extends UnifyType {
if(!(obj instanceof FunNType)) if(!(obj instanceof FunNType))
return false; return false;
FunNType other = (FunNType) obj; if(obj.hashCode() != this.hashCode())
return false;
FunNType other = (FunNType) obj;
return other.getTypeParams().equals(typeParams); return other.getTypeParams().equals(typeParams);
} }

View File

@ -11,12 +11,19 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
*/ */
public final class ReferenceType extends UnifyType { public final class ReferenceType extends UnifyType {
public ReferenceType(String name, UnifyType... typeParams) { /**
super(name, new TypeParams(typeParams)); * The buffered hashCode
*/
private final int hashCode;
public ReferenceType(String name, UnifyType... params) {
super(name, new TypeParams(params));
hashCode = 31 + 17 * typeName.hashCode() + 17 * typeParams.hashCode();
} }
public ReferenceType(String name, TypeParams params) { public ReferenceType(String name, TypeParams params) {
super(name, params); super(name, params);
hashCode = 31 + 17 * typeName.hashCode() + 17 * typeParams.hashCode();
} }
@Override @Override
@ -31,17 +38,24 @@ public final class ReferenceType extends UnifyType {
@Override @Override
UnifyType apply(Unifier unif) { UnifyType apply(Unifier unif) {
return new ReferenceType(typeName, typeParams.apply(unif)); TypeParams newParams = typeParams.apply(unif);
/*if(newParams.hashCode() == typeParams.hashCode() && newParams.equals(typeParams))
return this;*/
return new ReferenceType(new String(typeName), newParams);
} }
@Override @Override
public UnifyType setTypeParams(TypeParams newTp) { public UnifyType setTypeParams(TypeParams newTp) {
/*if(newTp.hashCode() == typeParams.hashCode() && newTp.equals(typeParams))
return this; // reduced the amount of objects created*/
return new ReferenceType(new String(typeName), newTp); return new ReferenceType(new String(typeName), newTp);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return 31 + 17 * typeName.hashCode() + 17 * typeParams.hashCode(); return hashCode;
} }
@Override @Override
@ -49,6 +63,9 @@ public final class ReferenceType extends UnifyType {
if(!(obj instanceof ReferenceType)) if(!(obj instanceof ReferenceType))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
ReferenceType other = (ReferenceType) obj; ReferenceType other = (ReferenceType) obj;
if(!other.getName().equals(typeName)) if(!other.getName().equals(typeName))

View File

@ -50,7 +50,10 @@ public final class SuperType extends WildcardType {
@Override @Override
UnifyType apply(Unifier unif) { UnifyType apply(Unifier unif) {
return new SuperType(wildcardedType.apply(unif)); UnifyType newType = wildcardedType.apply(unif);
/*if(newType.hashCode() == wildcardedType.hashCode() && newType.equals(wildcardedType))
return this; // Reduced the amount of objects created*/
return new SuperType(newType);
} }
@Override @Override
@ -67,6 +70,9 @@ public final class SuperType extends WildcardType {
if(!(obj instanceof SuperType)) if(!(obj instanceof SuperType))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
SuperType other = (SuperType) obj; SuperType other = (SuperType) obj;
return other.getSuperedType().equals(wildcardedType); return other.getSuperedType().equals(wildcardedType);
} }

View File

@ -39,7 +39,7 @@ public final class TypeParams implements Iterable<UnifyType>{
*/ */
public TypeParams(UnifyType... types) { public TypeParams(UnifyType... types) {
typeParams = types; typeParams = types;
// Hashcode calculation is expensive and must be cached. // Hashcode calculation is expensive and must be cached.
hashCode = Arrays.hashCode(typeParams); hashCode = Arrays.hashCode(typeParams);
} }
@ -73,8 +73,22 @@ public final class TypeParams implements Iterable<UnifyType>{
*/ */
public TypeParams apply(Unifier unif) { public TypeParams apply(Unifier unif) {
UnifyType[] newParams = new UnifyType[typeParams.length]; UnifyType[] newParams = new UnifyType[typeParams.length];
for(int i = 0; i < typeParams.length; i++)
newParams[i] = typeParams[i].apply(unif); // If true, a type was modified and a new typeparams object has to be created.
// Otherwise it is enough to return this object, since it is immutable
// This reduced the needed TypeParams-Instances for the lambda14-Test from
// 130.000 to 30.000 without a decrease in speed.
boolean isNew = false;
for(int i = 0; i < typeParams.length; i++) {
UnifyType newType = typeParams[i].apply(unif);
newParams[i] = newType;
//if(!isNew && (newType.hashCode() != typeParams[i].hashCode() || !newType.equals(typeParams[i])))
//isNew = true;
}
//if(!isNew)
// return this;
return new TypeParams(newParams); return new TypeParams(newParams);
} }
@ -107,8 +121,13 @@ public final class TypeParams implements Iterable<UnifyType>{
* @throws ArrayOutOfBoundsException if i > this.size()-1. * @throws ArrayOutOfBoundsException if i > this.size()-1.
*/ */
public TypeParams set(UnifyType t, int idx) { public TypeParams set(UnifyType t, int idx) {
// Reduce the creation of new objects for less memory
// Reduced the needed instances of TypeParams in the lambda14-Test from
// 150.000 to 130.000
/*if(t.hashCode() == typeParams[idx].hashCode() && t.equals(typeParams[idx]))
return this;*/
UnifyType[] newparams = Arrays.copyOf(typeParams, typeParams.length); UnifyType[] newparams = Arrays.copyOf(typeParams, typeParams.length);
newparams[idx] = t; newparams[idx] = t;
return new TypeParams(newparams); return new TypeParams(newparams);
} }
@ -127,6 +146,9 @@ public final class TypeParams implements Iterable<UnifyType>{
if(!(obj instanceof TypeParams)) if(!(obj instanceof TypeParams))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
TypeParams other = (TypeParams) obj; TypeParams other = (TypeParams) obj;
if(other.size() != this.size()) if(other.size() != this.size())

View File

@ -63,6 +63,9 @@ public class UnifyPair {
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof UnifyPair)) if(!(obj instanceof UnifyPair))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
UnifyPair other = (UnifyPair) obj; UnifyPair other = (UnifyPair) obj;

View File

@ -47,6 +47,9 @@ public abstract class WildcardType extends UnifyType {
if(!(obj instanceof WildcardType)) if(!(obj instanceof WildcardType))
return false; return false;
if(obj.hashCode() != this.hashCode())
return false;
WildcardType other = (WildcardType) obj; WildcardType other = (WildcardType) obj;
return other.getWildcardedType().equals(wildcardedType); return other.getWildcardedType().equals(wildcardedType);
} }