Improve ResolvableType.hashCode() for better performance

Prior to this commit, when there was a lot of entries in the
ResolvableType.cache HashMap, getting a simple value could
take a lot of time due to a lot of calls to ResolvableType.equals().
ResolvableType.equals() used this.type, getSource(),
this.variableResolver.getSource() and this.componentType, but
ResolvableType.hashCode() used only this.type.

With this commit, ResolvableType.hashCode() now uses the same
fields than ResolvableType.equals().

Performance on the spring-resolvabletype-benchmark project:
 - 8000 us before this commit
 - 120 us with this commit

Issue: SPR-12122
This commit is contained in:
Sebastien Deleuze 2014-08-26 11:50:46 +02:00
parent d5c6bcb901
commit 7ea69fb96c
1 changed files with 13 additions and 1 deletions

View File

@ -808,7 +808,11 @@ public final class ResolvableType implements Serializable {
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.type);
int hashCode = ObjectUtils.nullSafeHashCode(this.type);
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(getSource());
hashCode = 31 * hashCode + variableResolverSourceHashCode();
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.componentType);
return hashCode;
}
/**
@ -838,6 +842,14 @@ public final class ResolvableType implements Serializable {
return ObjectUtils.nullSafeEquals(this.variableResolver.getSource(), other.getSource());
}
private int variableResolverSourceHashCode() {
int hashCode = 0;
if (this.variableResolver != null) {
hashCode = ObjectUtils.nullSafeHashCode(this.variableResolver.getSource());
}
return hashCode;
}
private static ResolvableType[] forTypes(Type[] types, VariableResolver owner) {
ResolvableType[] result = new ResolvableType[types.length];
for (int i = 0; i < types.length; i++) {