From c7b6b72e73649b97f85b3829babff462d2ef9848 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 30 Sep 2014 18:01:40 +0200 Subject: [PATCH] Optimize ResolvableType cache Prior to this commit, the ResolvableType static cache was holding a lot of duplicates for simple types. We are using too much metadata to compute the key when the class has no generic information. so setFoo(String foo) and setBar(String bar) would result in two entries in the cache because the TypeProvider is different. On a very simple application 65% of the entries in the cache were duplicate. When the type is a Class with no generic information, the ResolvableType instance is a simple wrapper around it so we might just as well not cache it at all as the cost of finding it back from the cache is higher than creating that simple wrapper. This commit adds an explicit check; if the type is a simple Class we just return a "resolved" ResolvableType instance for it. On a few test cases, this reduces the size of the cache by 85% Issue: SPR-12275 (cherry picked from commit 6f1acdd) --- .../java/org/springframework/core/ResolvableType.java | 11 ++++++++++- 1 file changed, 10 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 6948d9ab610..78c7e88f306 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1192,8 +1192,17 @@ public final class ResolvableType implements Serializable { if (type == null) { return NONE; } - // Check the cache, we may have a ResolvableType that may have already been resolved + + // Purge empty entries on access since we don't have a clean-up thread or the like. cache.purgeUnreferencedEntries(); + + // For simple Class references, build the wrapper right away - + // no expensive resolution necessary, so not worth caching... + if (type instanceof Class) { + return new ResolvableType(type, typeProvider, variableResolver, null); + } + + // Check the cache - we may have a ResolvableType which has been resolved before... ResolvableType key = new ResolvableType(type, typeProvider, variableResolver); ResolvableType resolvableType = cache.get(key); if (resolvableType == null) {