equals / hashcode optimization
This commit is contained in:
parent
b8415b122b
commit
bb8df92cba
@ -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);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,6 @@ public class FiniteClosure implements IFiniteClosure {
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public final class TypeParams implements Iterable<UnifyType>{
|
||||
*/
|
||||
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<UnifyType>{
|
||||
*/
|
||||
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<UnifyType>{
|
||||
* @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<UnifyType>{
|
||||
if(!(obj instanceof TypeParams))
|
||||
return false;
|
||||
|
||||
if(obj.hashCode() != this.hashCode())
|
||||
return false;
|
||||
|
||||
TypeParams other = (TypeParams) obj;
|
||||
|
||||
if(other.size() != this.size())
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user