From d5408df35a525e4a078a3f7078944c69e36ea2ef 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 d08e62eb5..6abfece6a 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 c4c9ef7d3..b4e1b036c 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); } /*