Skip to content

Commit

Permalink
Reduce contention in TypeNameIdResolver. Fixes [FasterXML#2556]
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingFabian committed Dec 1, 2019
1 parent c1250b7 commit 8643600
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.jsontype.impl;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.BeanDescription;
Expand Down Expand Up @@ -28,8 +29,8 @@ protected TypeNameIdResolver(MapperConfig<?> config, JavaType baseType,
{
super(baseType, config.getTypeFactory());
_config = config;
_typeToId = typeToId;
_idToType = idToType;
_typeToId = new ConcurrentHashMap<String, String>(typeToId);
_idToType = new ConcurrentHashMap<String, JavaType>(idToType);
}

public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
Expand Down Expand Up @@ -91,10 +92,7 @@ protected String idFromClass(Class<?> clazz)
// NOTE: although we may need to let `TypeModifier` change actual type to use
// for id, we can use original type as key for more efficient lookup:
final String key = clazz.getName();
String name;
synchronized (_typeToId) {
name = _typeToId.get(key);
}
String name = _typeToId.get(key);

if (name == null) {
// 29-Nov-2019, tatu: As per test in `TestTypeModifierNameResolution` somehow
Expand All @@ -110,9 +108,7 @@ protected String idFromClass(Class<?> clazz)
// And if still not found, let's choose default?
name = _defaultTypeId(cls);
}
synchronized (_typeToId) {
_typeToId.put(key, name);
}
_typeToId.put(key, name);
}
return name;
}
Expand Down

0 comments on commit 8643600

Please sign in to comment.