Class FrequencyGatedBidirectionalObjectToIdMap<V>

java.lang.Object
ch.qos.logback.core.kryo.FrequencyGatedBidirectionalObjectToIdMap<V>
Type Parameters:
V - value type (must have correct equals/hashCode for map keys)
All Implemented Interfaces:
BidirectionalObjectToIdMap<V>

A BidirectionalObjectToIdMap that only assigns stable ids to values after they have been seen often enough on the write path.

Motivation

A plain BidirectionalObjectToIdMapDefault caches every distinct value on first sight. For high-cardinality fields (log messages, MDC values, exception text) that creates a large cache of one-off entries and little compression benefit: the stream still pays for a NEW_ENTRY header plus a full payload, and the map fills with values that never recur.

This implementation gates registration by frequency. A value is not entered into the id map until it has been written more than MIN_HITS times. Until then, writers emit a full payload under a sentinel id (KryoCoreConstants.UNREGISTERED_ID); only on later occurrences is a real id assigned so subsequent writes can emit a compact CACHED_ENTRY reference.

Write-path protocol

Used by SerializerBase.write(Kryo, Output, V) as follows:

  1. getIdByValue(Object) — if a real id exists, emit CACHED_ENTRY + id (no payload).
  2. Otherwise insertNewEntryOnWrite(Object):
    • Increments the per-value access count.
    • If count <= MIN_HITS: returns KryoCoreConstants.UNREGISTERED_ID (-1) and does not store the value in the id map.
    • If count > MIN_HITS: assigns the next id, stores the mapping, returns that id.
  3. In both unregistered and newly registered cases, SerializerBase still writes NEW_ENTRY + the returned id + the full payload via writeNewEntry.

Thus the first sighting of a value always carries a full payload with id -1. The second sighting (with default MIN_HITS = 1) carries a full payload again, but with a real id that is stored for later CACHED_ENTRY hits.

Read-path protocol

SerializerBase.read(Kryo, Input, Class) reads the entry flag and id, then:

Read-side registration is not frequency-gated: the stream is authoritative. Gating applies only when assigning ids on write.

State

  • Id map — a BidirectionalObjectToIdMapDefault of capacity capacity, access-order LRU eviction when full.
  • Access counts — a separate access-order LinkedHashMap used only on the write path to decide when a value graduates to a real id. Initial capacity ACCESS_COUNT_CACHE_SIZE.
  • Ids — assigned by the underlying map when a value passes the frequency gate; evicted ids are reused.

Thread safety

recordAccess(Object) is synchronized. Other methods are not; a given map instance is intended for single-threaded use by one serializer (typical Kryo usage).

See Also: