For optimal performance, usage pattern should be one where matches
should be very common (especially after "warm-up"), and as with most hash-based
maps/sets, that hash codes are uniformly distributed. Also, collisions
are slightly more expensive than with HashMap or HashSet, since hash codes
are not used in resolving collisions; that is, equals() comparison is
done with all symbols in same bucket index.
Finally, rehashing is also more expensive, as hash codes are not
stored; rehashing requires all entries' hash codes to be recalculated.
Reason for not storing hash codes is reduced memory usage, hoping
for better memory locality.
Usual usage pattern is to create a single "master" instance, and either use that instance in sequential fashion, or to create derived "child" instances, which after use, are asked to return possible symbol additions to master instance. In either case benefit is that symbol table gets initialized so that further uses are more efficient, as eventually all symbols needed will already be in symbol table. At that point no more Symbol String allocations are needed, nor changes to symbol table itself.
Note that while individual SymbolTable instances are NOT thread-safe (much like generic collection classes), concurrently used "child" instances can be freely used without synchronization. However, using master table concurrently with child instances can only be done if access to master instance is read-only (i.e. no modifications done).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected tools.jackson.core.sym.CharsToNameCanonicalizer.Bucket[]Overflow buckets; if primary doesn't match, lookup is done from here.protected booleanWhether any canonicalization should be attempted (whether using intern or not.protected final intFeature flags ofTokenStreamFactorythat uses this canonicalizer.protected booleanFlag that indicates whether underlying data structures for the main hash area are shared or not.protected intMask used to get index from hash values; equal to_buckets.length - 1, when _buckets.length is a power of two.protected intWe need to keep track of the longest collision list; this is needed both to indicate problems with attacks and to allow flushing for other cases.protected BitSetLazily constructed structure that is used to keep track of collision buckets that have overflowed once: this is used to detect likely attempts at denial-of-service attacks that uses hash collisions.protected final CharsToNameCanonicalizerSharing of learnt symbols is done by optional linking of symbol table instances with their parents.protected final intSeed value we use as the base to make hash codes non-static between different runs, but still stable for lifetime of a single symbol table instance.protected intCurrent size (number of entries); needed to know if and when rehash.protected intLimit that indicates maximum size this instance can hold before it needs to be expanded and rehashed.protected final StreamReadConstraintsConstraints used byTokenStreamFactorythat uses this canonicalizer.protected String[]Primary matching symbols; it's expected most match occur from here.protected final AtomicReference<tools.jackson.core.sym.CharsToNameCanonicalizer.TableInfo>Member that is only used by the root table instance: root passes immutable state info child instances, and children may return new state if they add entries to the table.static final intstatic final intLet's only share reasonably sized symbol tables. -
Method Summary
Modifier and TypeMethodDescriptionint_hashToIndex(int rawHash) Helper method that takes in a "raw" hash value, shuffles it as necessary, and truncates to be used as the index.protected void_reportTooManyCollisions(int maxLen) intMethod for checking number of primary hash buckets this symbol table uses.intcalcHash(char[] buffer, int start, int len) Implementation of a hashing method for variable length Strings.intintMethod mostly needed by unit tests; calculates number of entries that are in collision list.static CharsToNameCanonicalizercreateRoot(TokenStreamFactory owner) instance.static CharsToNameCanonicalizercreateRoot(TokenStreamFactory owner, int seed) findSymbol(char[] buffer, int start, int len, int h) inthashSeed()"Factory" method; will create a new child instance of this symbol table.intMethod mostly needed by unit tests; calculates length of the longest collision chain.booleanvoidrelease()Method called by the using code to indicate it is done with this instance.intsize()void
-
Field Details
-
HASH_MULT
public static final int HASH_MULT- See Also:
-
MAX_ENTRIES_FOR_REUSE
public static final int MAX_ENTRIES_FOR_REUSELet's only share reasonably sized symbol tables. Max size set to 3/4 of 16k; this corresponds to 64k main hash index. This should allow for enough distinct names for almost any case.- See Also:
-
_parent
Sharing of learnt symbols is done by optional linking of symbol table instances with their parents. When parent linkage is defined, and child instance is released (call torelease), parent's shared tables may be updated from the child instance. -
_tableInfo
protected final AtomicReference<tools.jackson.core.sym.CharsToNameCanonicalizer.TableInfo> _tableInfoMember that is only used by the root table instance: root passes immutable state info child instances, and children may return new state if they add entries to the table. Child tables do NOT use the reference. -
_streamReadConstraints
Constraints used byTokenStreamFactorythat uses this canonicalizer.- Since:
- 2.16
-
_seed
protected final int _seedSeed value we use as the base to make hash codes non-static between different runs, but still stable for lifetime of a single symbol table instance. This is done for security reasons, to avoid potential DoS attack via hash collisions. -
_factoryFeatures
protected final int _factoryFeaturesFeature flags ofTokenStreamFactorythat uses this canonicalizer. -
_canonicalize
protected boolean _canonicalizeWhether any canonicalization should be attempted (whether using intern or not.NOTE: non-final since we may need to disable this with overflow.
-
_symbols
Primary matching symbols; it's expected most match occur from here. -
_buckets
protected tools.jackson.core.sym.CharsToNameCanonicalizer.Bucket[] _bucketsOverflow buckets; if primary doesn't match, lookup is done from here.Note: Number of buckets is half of number of symbol entries, on assumption there's less need for buckets.
-
_size
protected int _sizeCurrent size (number of entries); needed to know if and when rehash. -
_sizeThreshold
protected int _sizeThresholdLimit that indicates maximum size this instance can hold before it needs to be expanded and rehashed. Calculated using fill factor passed in to constructor. -
_indexMask
protected int _indexMaskMask used to get index from hash values; equal to_buckets.length - 1, when _buckets.length is a power of two. -
_longestCollisionList
protected int _longestCollisionListWe need to keep track of the longest collision list; this is needed both to indicate problems with attacks and to allow flushing for other cases. -
_overflows
Lazily constructed structure that is used to keep track of collision buckets that have overflowed once: this is used to detect likely attempts at denial-of-service attacks that uses hash collisions.
-
-
Method Details
-
createRoot
instance. Root instance is never used directly; its main use is for storing and sharing underlying symbol arrays as needed.- Parameters:
owner- Factory that will use the root instance; used for accessing configuration- Returns:
- Root instance to use for constructing new child instances
-
createRoot
-
makeChild
"Factory" method; will create a new child instance of this symbol table. It will be a copy-on-write instance, ie. it will only use read-only copy of parent's data, but when changes are needed, a copy will be created.Note: It is generally not safe to both use makeChild/mergeChild, AND to use instance actively. Instead, a separate 'root' instance should be used on which only makeChild/mergeChild are called, but instance itself is not used as a symbol table.
- Returns:
- Actual canonicalizer instance that can be used by a parser
-
release
public void release()Method called by the using code to indicate it is done with this instance. This lets instance merge accumulated changes into parent (if need be), safely and efficiently, and without calling code having to know about parent information. -
size
public int size()- Returns:
- Number of symbol entries contained by this canonicalizer instance
-
bucketCount
public int bucketCount()Method for checking number of primary hash buckets this symbol table uses.- Returns:
- number of primary slots table has currently
-
maybeDirty
public boolean maybeDirty() -
hashSeed
public int hashSeed() -
collisionCount
public int collisionCount()Method mostly needed by unit tests; calculates number of entries that are in collision list. Value can be at most (size()- 1), but should usually be much lower, ideally 0.- Returns:
- Number of collisions in the primary hash area
- Since:
- 2.1
-
maxCollisionLength
public int maxCollisionLength()Method mostly needed by unit tests; calculates length of the longest collision chain. This should typically be a low number, but may be up tosize()- 1 in the pathological case- Returns:
- Length of the collision chain
- Since:
- 2.1
-
findSymbol
-
_hashToIndex
public int _hashToIndex(int rawHash) Helper method that takes in a "raw" hash value, shuffles it as necessary, and truncates to be used as the index.- Parameters:
rawHash- Raw hash value to use for calculating index- Returns:
- Index value calculated
-
calcHash
public int calcHash(char[] buffer, int start, int len) Implementation of a hashing method for variable length Strings. Most of the time intention is that this calculation is done by caller during parsing, not here; however, sometimes it needs to be done for parsed "String" too.- Parameters:
buffer- Input buffer that contains name to decodestart- Pointer to the first character of the namelen- Length of String; has to be at least 1 (caller guarantees)- Returns:
- Hash code calculated
-
calcHash
-
_reportTooManyCollisions
protected void _reportTooManyCollisions(int maxLen) -
verifyInternalConsistency
public void verifyInternalConsistency()
-