From 7ea69fb96c2edb81688602adebcbfc417a9bb930 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 26 Aug 2014 11:50:46 +0200 Subject: [PATCH] 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 --- .../org/springframework/core/ResolvableType.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/core/ResolvableType.java b/spring-core/src/main/java/org/springframework/core/ResolvableType.java index 6a50b919438..6948d9ab610 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -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++) {