From 8d06a38fdb22e26ca46a04d49111be534b59461c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 8 Oct 2018 10:44:37 +0200 Subject: [PATCH] DATACMNS-1396 - Use best-effort caching in CustomConversions and DefaultTypeMapper. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../springframework/data/convert/CustomConversions.java | 5 ++++- .../springframework/data/convert/DefaultTypeMapper.java | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/convert/CustomConversions.java b/src/main/java/org/springframework/data/convert/CustomConversions.java index 25023bffc..2321e9b9e 100644 --- a/src/main/java/org/springframework/data/convert/CustomConversions.java +++ b/src/main/java/org/springframework/data/convert/CustomConversions.java @@ -393,7 +393,10 @@ public class CustomConversions { public Class computeIfAbsent(Class sourceType, Class targetType, Function> mappingFunction) { - TargetTypes targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new); + TargetTypes targetTypes = customReadTargetTypes.get(sourceType); + if (targetTypes == null) { + targetTypes = customReadTargetTypes.computeIfAbsent(sourceType, TargetTypes::new); + } return targetTypes.computeIfAbsent(targetType, mappingFunction); } diff --git a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java index 2a59aea09..8065323ff 100644 --- a/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java +++ b/src/main/java/org/springframework/data/convert/DefaultTypeMapper.java @@ -127,7 +127,14 @@ public class DefaultTypeMapper implements TypeMapper { */ @Nullable private TypeInformation getFromCacheOrCreate(Alias alias) { - return typeCache.computeIfAbsent(alias, getAlias).orElse(null); + + Optional> typeInformation = typeCache.get(alias); + + if (typeInformation == null) { + typeInformation = typeCache.computeIfAbsent(alias, getAlias); + } + + return typeInformation.orElse(null); } /*