Skip to content

Commit c7b6b72

Browse files
snicolljhoeller
authored andcommitted
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)
1 parent 1df8133 commit c7b6b72

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,17 @@ static ResolvableType forType(Type type, TypeProvider typeProvider, VariableReso
11921192
if (type == null) {
11931193
return NONE;
11941194
}
1195-
// Check the cache, we may have a ResolvableType that may have already been resolved
1195+
1196+
// Purge empty entries on access since we don't have a clean-up thread or the like.
11961197
cache.purgeUnreferencedEntries();
1198+
1199+
// For simple Class references, build the wrapper right away -
1200+
// no expensive resolution necessary, so not worth caching...
1201+
if (type instanceof Class<?>) {
1202+
return new ResolvableType(type, typeProvider, variableResolver, null);
1203+
}
1204+
1205+
// Check the cache - we may have a ResolvableType which has been resolved before...
11971206
ResolvableType key = new ResolvableType(type, typeProvider, variableResolver);
11981207
ResolvableType resolvableType = cache.get(key);
11991208
if (resolvableType == null) {

0 commit comments

Comments
 (0)