Browse Source

DATACMNS-1396 - 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.
pull/351/head
Mark Paluch 7 years ago committed by Oliver Gierke
parent
commit
8d06a38fdb
  1. 5
      src/main/java/org/springframework/data/convert/CustomConversions.java
  2. 9
      src/main/java/org/springframework/data/convert/DefaultTypeMapper.java

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

@ -393,7 +393,10 @@ public class CustomConversions { @@ -393,7 +393,10 @@ public class CustomConversions {
public Class<?> computeIfAbsent(Class<?> sourceType, Class<?> targetType,
Function<ConvertiblePair, Class<?>> 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);
}

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

@ -127,7 +127,14 @@ public class DefaultTypeMapper<S> implements TypeMapper<S> { @@ -127,7 +127,14 @@ public class DefaultTypeMapper<S> implements TypeMapper<S> {
*/
@Nullable
private TypeInformation<?> getFromCacheOrCreate(Alias alias) {
return typeCache.computeIfAbsent(alias, getAlias).orElse(null);
Optional<TypeInformation<?>> typeInformation = typeCache.get(alias);
if (typeInformation == null) {
typeInformation = typeCache.computeIfAbsent(alias, getAlias);
}
return typeInformation.orElse(null);
}
/*

Loading…
Cancel
Save