diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index d5490e96..13049c88 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -14,7 +14,7 @@ public final class ExtendsType extends WildcardType { * @param extendedType The extended type e.g. Integer in "? extends Integer" */ 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 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 @@ -61,7 +64,12 @@ public final class ExtendsType extends WildcardType { if(!(obj instanceof ExtendsType)) return false; + if(obj.hashCode() != this.hashCode()) + return false; + ExtendsType other = (ExtendsType) obj; + + return other.getWildcardedType().equals(wildcardedType); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index ac0862cc..a08f79cb 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -33,7 +33,6 @@ public class FiniteClosure implements IFiniteClosure { */ private Set 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. */ diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java index 94ac1cf8..31517891 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java @@ -58,7 +58,11 @@ public class FunNType extends UnifyType { UnifyType apply(Unifier unif) { // TODO this bypasses the validation of the type parameters. // 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 @@ -71,8 +75,11 @@ public class FunNType extends UnifyType { if(!(obj instanceof FunNType)) return false; - FunNType other = (FunNType) obj; + if(obj.hashCode() != this.hashCode()) + return false; + FunNType other = (FunNType) obj; + return other.getTypeParams().equals(typeParams); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java index ad02c9ab..9ffea796 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java @@ -11,12 +11,19 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; */ 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) { super(name, params); + hashCode = 31 + 17 * typeName.hashCode() + 17 * typeParams.hashCode(); } @Override @@ -31,17 +38,24 @@ public final class ReferenceType extends UnifyType { @Override 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 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); } @Override public int hashCode() { - return 31 + 17 * typeName.hashCode() + 17 * typeParams.hashCode(); + return hashCode; } @Override @@ -49,6 +63,9 @@ public final class ReferenceType extends UnifyType { if(!(obj instanceof ReferenceType)) return false; + if(obj.hashCode() != this.hashCode()) + return false; + ReferenceType other = (ReferenceType) obj; if(!other.getName().equals(typeName)) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java index 8a828156..664e1624 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/SuperType.java @@ -50,7 +50,10 @@ public final class SuperType extends WildcardType { @Override 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 @@ -67,6 +70,9 @@ public final class SuperType extends WildcardType { if(!(obj instanceof SuperType)) return false; + if(obj.hashCode() != this.hashCode()) + return false; + SuperType other = (SuperType) obj; return other.getSuperedType().equals(wildcardedType); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index 7ce251d9..f2336e96 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -39,7 +39,7 @@ public final class TypeParams implements Iterable{ */ public TypeParams(UnifyType... types) { typeParams = types; - + // Hashcode calculation is expensive and must be cached. hashCode = Arrays.hashCode(typeParams); } @@ -73,8 +73,22 @@ public final class TypeParams implements Iterable{ */ public TypeParams apply(Unifier unif) { 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); } @@ -107,8 +121,13 @@ public final class TypeParams implements Iterable{ * @throws ArrayOutOfBoundsException if i > this.size()-1. */ 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); - newparams[idx] = t; + newparams[idx] = t; return new TypeParams(newparams); } @@ -127,6 +146,9 @@ public final class TypeParams implements Iterable{ if(!(obj instanceof TypeParams)) return false; + if(obj.hashCode() != this.hashCode()) + return false; + TypeParams other = (TypeParams) obj; if(other.size() != this.size()) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index 62de0c96..672a516d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -63,6 +63,9 @@ public class UnifyPair { public boolean equals(Object obj) { if(!(obj instanceof UnifyPair)) return false; + + if(obj.hashCode() != this.hashCode()) + return false; UnifyPair other = (UnifyPair) obj; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java index 19efd88a..9b42c087 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java @@ -47,6 +47,9 @@ public abstract class WildcardType extends UnifyType { if(!(obj instanceof WildcardType)) return false; + if(obj.hashCode() != this.hashCode()) + return false; + WildcardType other = (WildcardType) obj; return other.getWildcardedType().equals(wildcardedType); }