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

@ -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);
}

View File

@ -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.
*/

View File

@ -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,6 +75,9 @@ public class FunNType extends UnifyType {
if(!(obj instanceof FunNType))
return false;
if(obj.hashCode() != this.hashCode())
return false;
FunNType other = (FunNType) obj;
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 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))

View File

@ -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);
}

View File

@ -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,6 +121,11 @@ 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;
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())

View File

@ -64,6 +64,9 @@ public class UnifyPair {
if(!(obj instanceof UnifyPair))
return false;
if(obj.hashCode() != this.hashCode())
return false;
UnifyPair other = (UnifyPair) obj;
return other.getPairOp() == pairOp

View File

@ -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);
}