Class ATNConfigSet
- Direct Known Subclasses:
OrderedATNConfigSet
ATNConfig). As
configurations are added to the set, they are merged with other
ATNConfig instances already in the set when possible using the
graph-structured stack.
An instance of this class represents the complete set of positions (with context) in an ATN which would be associated with a single DFA state. Its internal representation is more complex than traditional state used for NFA to DFA conversion due to performance requirements (both improving speed and reducing memory overhead) as well as supporting features such as semantic predicates and non-greedy operators in a form to support ANTLR's prediction algorithm.
Writable sets use a primitive long-keyed map for the
(state, alt) merge index so hot add/merge paths avoid
Long boxing on every lookup.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intCachedhashCode()for both writable scratch sets and readonly DFA sets.This is a list of all configs in this set.private ConflictInfoprivate booleanprivate booleanprivate final ClearableLongObjectHashMap<ATNConfig>This maps (state, alt) -> mergedATNConfig.private static final intMinimum expected-element hint passed to the primitivelong-keyed merge map when a positive capacity is requested.private booleanWhentrue, this config set represents configurations where the entire outer context has been consumed by the ATN interpreter.(package private) static final intCapacity floor shared by hot-path scratch sets (lexer reach, parser reach/intermediate, epsilon-closure BFS buffers).(package private) static final intWhen the number of configs is less than table-length / this factor,clear()removes known merge keys one-by-one (O(n)) instead of bulk-filling the open-addressed table (O(capacity)).private intThis is an "overflow" list holding configs which cannot be merged with one of the configs inmergedConfigsbut have a colliding key. -
Constructor Summary
ConstructorsModifierConstructorDescriptionATNConfigSet(int expectedSize) Constructs an empty, writable config set with the given capacity hint for the primary configuration storage.protectedATNConfigSet(ATNConfigSet set, boolean readonly) -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanadd(ATNConfig e, PredictionContextCache contextCache) booleanaddAll(Collection<? extends ATNConfig> c) booleanaddAll(Collection<? extends ATNConfig> c, PredictionContextCache contextCache) protected booleanWhetherleftmay merge into the existingrightconfig.voidclear()Removes all configurations and resets working-set flags so the instance can be reused as hot-path scratch (seeRetainedConfigSet).voidclone(boolean readonly) booleanbooleancontainsAll(Collection<?> c) protected final voidbooleanget(int index) booleanprotected longPacked(stateNumber, alt)key.Get the set of all alternatives represented by configurations in this set.intinthashCode()booleanprivate voidbooleanisEmpty()booleanbooleanfinal booleaniterator()voidprivate booleanmergeConfigContext(ATNConfig e, PredictionContextCache contextCache, ATNConfig unmergedConfig) private static intmergedMapExpectedElements(int expectedSize) Converts an expected configuration count into an expected-element hint for the private primitive merge map.voidoptimizeConfigs(ATNSimulator interpreter) voidremove(int index) booleanbooleanremoveAll(Collection<?> c) private voidResets conflict / context flags used while a writable set is being built.booleanretainAll(Collection<?> c) (package private) static intscratchCapacity(int sourceSize) Converts a source config count into a scratch-buffer capacity hint used when allocating or sizing retainedATNConfigSetinstances on prediction / lexing hot paths.voidsetConflictInfo(ConflictInfo conflictInfo) voidsetOutermostConfigSet(boolean outermostConfigSet) intsize()Object[]toArray()<T> T[]toArray(T[] a) toString()toString(boolean showContext) private voidprivate voidMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
spliterator
-
Field Details
-
MIN_MERGED_CONFIG_CAPACITY
private static final int MIN_MERGED_CONFIG_CAPACITYMinimum expected-element hint passed to the primitivelong-keyed merge map when a positive capacity is requested. Matches the previousHashMapdefault capacity floor so small hints still expand reasonably during closure fan-out.- See Also:
-
SCRATCH_CAPACITY_FLOOR
static final int SCRATCH_CAPACITY_FLOORCapacity floor shared by hot-path scratch sets (lexer reach, parser reach/intermediate, epsilon-closure BFS buffers).- See Also:
-
SPARSE_CLEAR_CAPACITY_FACTOR
static final int SPARSE_CLEAR_CAPACITY_FACTORWhen the number of configs is less than table-length / this factor,clear()removes known merge keys one-by-one (O(n)) instead of bulk-filling the open-addressed table (O(capacity)). Retained scratch sets often keep a large table after a wide fan-out edge while later edges hold only a handful of configs.- See Also:
-
mergedConfigs
This maps (state, alt) -> mergedATNConfig. The key does not account for theATNConfig.getSemanticContext()of the value, which is only a problem if a singleATNConfigSetcontains two configs with the same state and alternative but different semantic contexts. When this case arises, the first config added to this map stays, and the remaining configs are placed inunmerged.This map is only used for optimizing the process of adding configs to the set, and is
nullfor read-only sets stored in the DFA.Implemented as a private primitive
long-keyed open-addressed map (package-private storage; not part of any public or protected API) sogetKey(org.antlr.v4.runtime.atn.ATNConfig)values are stored and looked up withoutLongboxing on the prediction hot path. Hotadd(org.antlr.v4.runtime.atn.ATNConfig)/contains(java.lang.Object)hash each key once per operation.clear()uses an empty-fast path and may sparse-remove known keys when occupancy is low relative to table capacity (seeSPARSE_CLEAR_CAPACITY_FACTOR). External callers interact only with theSet<ATNConfig>surface of this class. -
unmerged
This is an "overflow" list holding configs which cannot be merged with one of the configs inmergedConfigsbut have a colliding key. This occurs when two configs in the set have the same state and alternative but different semantic contexts.This list is only used for optimizing the process of adding configs to the set, and is
nullfor read-only sets stored in the DFA. -
configs
This is a list of all configs in this set. -
uniqueAlt
private int uniqueAlt -
conflictInfo
-
hasSemanticContext
private boolean hasSemanticContext -
dipsIntoOuterContext
private boolean dipsIntoOuterContext -
outermostConfigSet
private boolean outermostConfigSetWhentrue, this config set represents configurations where the entire outer context has been consumed by the ATN interpreter. This prevents theParserATNSimulator.closure(org.antlr.v4.runtime.atn.ATNConfigSet, org.antlr.v4.runtime.atn.ATNConfigSet, boolean, boolean, org.antlr.v4.runtime.atn.PredictionContextCache, boolean)from pursuing the global FOLLOW when a rule stop state is reached with an empty prediction context.Note:
outermostConfigSetanddipsIntoOuterContextshould never be true at the same time. -
cachedHashCode
private int cachedHashCodeCachedhashCode()for both writable scratch sets and readonly DFA sets.0means uncomputed (zero is reserved; a raw hash of zero is stored as1). Invalidated on every mutating operation that participates inhashCode().
-
-
Constructor Details
-
ATNConfigSet
public ATNConfigSet() -
ATNConfigSet
public ATNConfigSet(int expectedSize) Constructs an empty, writable config set with the given capacity hint for the primary configuration storage. Larger hints reduce rehashing when the approximate number of configurations is known in advance (for example when seeding a set from another configuration set during closure or reach operations).The hint is treated as an expected element count for both the configuration list and the primitive
(state, alt)merge map. The merge-map expected size is floored atMIN_MERGED_CONFIG_CAPACITYso that small expected sizes which later expand during closure do not rehash more aggressively than an unhinted set.- Parameters:
expectedSize- expected number of configurations, or<= 0for the default map/list capacities
-
ATNConfigSet
-
-
Method Details
-
scratchCapacity
static int scratchCapacity(int sourceSize) Converts a source config count into a scratch-buffer capacity hint used when allocating or sizing retainedATNConfigSetinstances on prediction / lexing hot paths. Doubles the source size (with overflow guard) and floors atSCRATCH_CAPACITY_FLOOR.- Parameters:
sourceSize- number of configs in the source set, or0- Returns:
- positive expected-element capacity for a writable config set
-
mergedMapExpectedElements
private static int mergedMapExpectedElements(int expectedSize) Converts an expected configuration count into an expected-element hint for the private primitive merge map. The result is never smaller thanMIN_MERGED_CONFIG_CAPACITYso undersized hints cannot regress expansion cost relative to an unhinted map when the set grows beyond the estimate. -
getRepresentedAlternatives
Get the set of all alternatives represented by configurations in this set. -
isReadOnly
public final boolean isReadOnly() -
isOutermostConfigSet
public boolean isOutermostConfigSet() -
setOutermostConfigSet
public void setOutermostConfigSet(boolean outermostConfigSet) -
getStates
-
optimizeConfigs
-
clone
-
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
add
-
add
-
mergeConfigContext
private boolean mergeConfigContext(ATNConfig e, @Nullable PredictionContextCache contextCache, ATNConfig unmergedConfig) -
updatePropertiesForMergedConfig
-
updatePropertiesForAddedConfig
-
canMerge
Whetherleftmay merge into the existingrightconfig.PERF: The packed
leftKeyalready encodes(stateNumber, alt)(seegetKey(org.antlr.v4.runtime.atn.ATNConfig)). Comparing keys alone establishes state+alt equality; a separategetState().stateNumberwalk is redundant on the hot merge path. -
getKey
Packed(stateNumber, alt)key. The alternative keeps the full 24 bits stored byATNConfig. -
remove
-
containsAll
- Specified by:
containsAllin interfaceCollection<ATNConfig>- Specified by:
containsAllin interfaceSet<ATNConfig>
-
addAll
-
addAll
-
retainAll
-
removeAll
-
clear
public void clear()Removes all configurations and resets working-set flags so the instance can be reused as hot-path scratch (seeRetainedConfigSet).In particular
outermostConfigSetis cleared: retention across predictions would otherwise leave a sticky outermost flag and tripadd(org.antlr.v4.runtime.atn.ATNConfig)/setOutermostConfigSet(boolean)asserts on later edges.PERF: Empty clear is O(1) for the merge map (no bulk
Arrays.fill) and O(1) for emptyArrayLists. When the set holds few configs relative to the grown open-addressed table (size *SPARSE_CLEAR_CAPACITY_FACTOR< tableLength), known merge keys are removed individually (O(n)) instead of filling the entire table (O(capacity)). Dense sets still use bulk clear. List clear only nulls the used prefix (JDKArrayList.clear()), never the full capacity. Together these policies remove the double full-table zeroing that dominated lexercomputeTargetStatepool obtain/release on many-core workloads. -
resetWorkingFlags
private void resetWorkingFlags()Resets conflict / context flags used while a writable set is being built. Invoked byclear()after storage is empty (or was already empty). -
invalidateHash
private void invalidateHash() -
equals
-
hashCode
public int hashCode() -
toString
-
toString
-
getUniqueAlt
public int getUniqueAlt() -
hasSemanticContext
public boolean hasSemanticContext() -
clearExplicitSemanticContext
public void clearExplicitSemanticContext() -
markExplicitSemanticContext
public void markExplicitSemanticContext() -
getConflictInfo
-
setConflictInfo
-
getConflictingAlts
-
isExactConflict
public boolean isExactConflict() -
getDipsIntoOuterContext
public boolean getDipsIntoOuterContext() -
get
-
remove
public void remove(int index) -
ensureWritable
protected final void ensureWritable()
-