Class PredictionContextCache

java.lang.Object
org.antlr.v4.runtime.atn.PredictionContextCache

public class PredictionContextCache extends Object
Used to cache PredictionContext objects. It is used for the shared context cache associated with contexts in DFA states. This cache can be used for both lexers and parsers.

Cache instances created with PredictionContextCache() are intended for single-threaded use during a prediction operation. Concurrent access from multiple threads is not supported (the shared UNCACHED instance is safe because caching is disabled and no mutable probe state is retained).

PERF: getChild(org.antlr.v4.runtime.atn.PredictionContext, int) and join(org.antlr.v4.runtime.atn.PredictionContext, org.antlr.v4.runtime.atn.PredictionContext) use mutable probe keys for map lookups so a cache hit does not allocate a key object. Permanent keys are allocated only on a cache miss when the entry is stored. Probe types are distinct from permanent key types so a probe can never be inserted into the map: the maps are typed as Map<PredictionContextAndInt, …> and Map<IdentityCommutativePredictionContextOperands, …>, while Map.get(Object) still accepts probes because equality is defined on the shared ChildKey/JoinKey bases.

join(org.antlr.v4.runtime.atn.PredictionContext, org.antlr.v4.runtime.atn.PredictionContext) may re-enter itself recursively via PredictionContext.join(org.antlr.v4.runtime.atn.PredictionContext, org.antlr.v4.runtime.atn.PredictionContext); the probe key is only used for the duration of each individual map lookup, and map insertions always use freshly allocated immutable keys.