Class SerializerCache

java.lang.Object
tools.jackson.databind.ser.SerializerCache
All Implemented Interfaces:
Serializable, Snapshottable<SerializerCache>

public final class SerializerCache extends Object implements Snapshottable<SerializerCache>, Serializable
Simple cache object that allows for doing 2-level lookups: first level is by "local" read-only lookup Map (used without locking) and second backup level is by a shared modifiable HashMap. The idea is that after a while, most serializers are found from the local Map (to optimize performance, reduce lock contention), but that during buildup we can use a shared map to reduce both number of distinct read-only maps constructed, and number of serializers constructed.

Cache contains three kinds of entries, based on combination of class pair key. First class in key is for the type to serialize, and second one is type used for determining how to resolve value type. One (but not both) of entries can be null.

To prevent threads from observing partially-resolved serializers during addAndResolveNonTypedSerializer(java.lang.Class<?>, tools.jackson.databind.ValueSerializer<java.lang.Object>, tools.jackson.databind.SerializationContext) calls, a two-phase write protocol is used: newly constructed serializers are placed into _inProgressMap first, resolved there (so cyclic POJO lookups can find the in-progress entry), and only moved to _sharedMap after resolve() completes. Each entry is removed from _inProgressMap as soon as it is promoted, so the map tends to stay empty at steady state. The lock-free read path (untypedValueSerializer(java.lang.Class<?>)) reads only from _sharedMap, which therefore never contains a partially-resolved serializer.

See Also: