From b7726ff48c3a0c6944c7d7ffb3aaf6a871a69d72 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 27 Aug 2014 10:35:22 +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 (cherry picked from commit 7ea69fb) --- .../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 6a50b91943..6948d9ab61 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++) {