Skip to content

Commit b7726ff

Browse files
sdeleuzejhoeller
authored andcommitted
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 (cherry picked from commit 7ea69fb)
1 parent a52ff46 commit b7726ff

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

spring-core/src/main/java/org/springframework/core/ResolvableType.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,11 @@ public boolean equals(Object obj) {
808808

809809
@Override
810810
public int hashCode() {
811-
return ObjectUtils.nullSafeHashCode(this.type);
811+
int hashCode = ObjectUtils.nullSafeHashCode(this.type);
812+
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(getSource());
813+
hashCode = 31 * hashCode + variableResolverSourceHashCode();
814+
hashCode = 31 * hashCode + ObjectUtils.nullSafeHashCode(this.componentType);
815+
return hashCode;
812816
}
813817

814818
/**
@@ -838,6 +842,14 @@ private boolean variableResolverSourceEquals(VariableResolver other) {
838842
return ObjectUtils.nullSafeEquals(this.variableResolver.getSource(), other.getSource());
839843
}
840844

845+
private int variableResolverSourceHashCode() {
846+
int hashCode = 0;
847+
if (this.variableResolver != null) {
848+
hashCode = ObjectUtils.nullSafeHashCode(this.variableResolver.getSource());
849+
}
850+
return hashCode;
851+
}
852+
841853
private static ResolvableType[] forTypes(Type[] types, VariableResolver owner) {
842854
ResolvableType[] result = new ResolvableType[types.length];
843855
for (int i = 0; i < types.length; i++) {

0 commit comments

Comments
 (0)