Skip to content

Commit 745ec26

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1402 - Use best-effort caching in CustomConversions and DefaultTypeMapper.
We now apply best-effort caching instead of atomic caching for custom conversions and type mapping. This change is a workaround for a Java 8 bug in ConcurrentHashMap where the computeIfAbsent(…) operation unconditionally locks nodes even when the node is already present. The workaround is to assume the optimistic case by looking up the key and then falling back to computeIfAbsent if the key is absent. Before: TypicalEntityReaderBenchmark.simpleEntityReflectivePropertyAccessWithCustomConversionRegistry thrpt 10 6487423,969 ± 349449,326 ops/s DefaultTypeMapperBenchmark.readTyped thrpt 10 38213392,961 ± 5080789,480 ops/s DefaultTypeMapperBenchmark.readUntyped thrpt 10 47565238,929 ± 855200,560 ops/s After: TypicalEntityReaderBenchmark.simpleEntityReflectivePropertyAccessWithCustomConversionRegistry thrpt 10 7361251,834 ± 278530,209 ops/s DefaultTypeMapperBenchmark.readTyped thrpt 10 122523380,422 ± 3839365,439 ops/s DefaultTypeMapperBenchmark.readUntyped thrpt 10 181767673,793 ± 3549021,260 ops/s Original pull request: #319.
1 parent 6481cbe commit 745ec26

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/main/java/org/springframework/data/convert/CustomConversions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ public Class<?> computeIfAbsent(Class<?> sourceType, Function<ConvertiblePair, C
393393
public Class<?> computeIfAbsent(Class<?> sourceType, Class<?> targetType,
394394
Function<ConvertiblePair, Class<?>> mappingFunction) {
395395

396-
TargetTypes targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new);
396+
TargetTypes targetTypes = customReadTargetTypes.get(sourceType);
397+
if (targetTypes == null) {
398+
targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new);
399+
}
397400
return targetTypes.computeIfAbsent(targetType, mappingFunction);
398401
}
399402

src/main/java/org/springframework/data/convert/DefaultTypeMapper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ public TypeInformation<?> readType(S source) {
127127
*/
128128
@Nullable
129129
private TypeInformation<?> getFromCacheOrCreate(Alias alias) {
130-
return typeCache.computeIfAbsent(alias, getAlias).orElse(null);
130+
131+
Optional<TypeInformation<?>> typeInformation = typeCache.get(alias);
132+
133+
if (typeInformation == null) {
134+
typeInformation = typeCache.computeIfAbsent(alias, getAlias);
135+
}
136+
137+
return typeInformation.orElse(null);
131138
}
132139

133140
/*

0 commit comments

Comments
 (0)