BytesToNameCanonicalizer which aims at more localized
memory access due to flattening of name quad data.
Performance improvement modest for simple JSON document data binding (maybe 3%),
but should help more for larger symbol tables, or for binary formats like Smile.
Hash area is divided into 4 sections:
hash (LSB) >> 1hash (LSB) >> 2ints, where 1 - 3 ints contain 1 - 12
UTF-8 encoded bytes of name (null-padded), and last int is offset in
_names that contains actual name Strings.
| Modifier and Type | Class and Description |
|---|---|
| private static class | ByteQuadsCanonicalizer.TableInfo
Immutable value class used for sharing information as efficiently as possible, by only require synchronization of reference manipulation but not access to contents. |
| Modifier and Type | Field and Description |
|---|---|
| protected int | _count
Total number of Strings in the symbol table; only used for child tables. |
| protected final boolean | _failOnDoS
Flag that indicates whether we should throw an exception if enough hash collisions are detected (true); or just worked around (false). |
| protected int[] | _hashArea
Primary hash information area: consists of |
| protected boolean | _hashShared
Flag that indicates whether underlying data structures for the main hash area are shared or not. |
| protected int | |
| protected final InternCache | _interner
Entity that knows how to |
| protected int | _longNameOffset
Offset within |
| protected String[] | |
| protected final ByteQuadsCanonicalizer | _parent
Reference to the root symbol table, for child tables, so that they can merge table information back as necessary. |
| protected int | _secondaryStart
Offset within |
| protected final int | _seed
Seed 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 int | _spilloverEnd
Pointer to the offset within spill-over area where there is room for more spilled over entries (if any). |
| protected final AtomicReference | _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. |
| protected int | _tertiaryShift
Constant that determines size of buckets for tertiary entries:
|
| protected int | _tertiaryStart
Offset within |
| private static final int | DEFAULT_T_SIZE
Initial size of the primary hash area. |
| protected static final int | MAX_ENTRIES_FOR_REUSE
Let's only share reasonably sized symbol tables. |
| private static final int | MAX_T_SIZE
Let's not expand symbol tables past some maximum size; with unique (~= random) names. |
| private static final int | MIN_HASH_SIZE
No point in trying to construct tiny tables, just need to resize soon. |
| private static final int | |
| private static final int | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| private | ByteQuadsCanonicalizer(int
Initial primary hash area size sz, int Random seed valued used to make it more difficult to cause
collisions (used for collision-based DoS attacks). seed)Constructor used for creating per- |
| private | ByteQuadsCanonicalizer(ByteQuadsCanonicalizer parent, int seed, ByteQuadsCanonicalizer.
Constructor used when creating a child instance |
| private | ByteQuadsCanonicalizer(ByteQuadsCanonicalizer.
Alternate constructor used in cases where a "placeholder" child instance is needed when symbol table is not really used, but caller needs a non-null placeholder to keep code functioning with minimal awareness of distinction (all lookups fail to match any name without error; add methods should NOT be called). |
| Modifier and Type | Method and Description |
|---|---|
| private int | |
| private final int | |
| pack-priv static int | |
| private boolean | |
| private int | _findOffsetForAdd(int
Hash of name for which to find location hash)Method called to find the location within hash table to add a new symbol in. |
| private String | |
| private String | |
| private String | |
| private String | |
| protected void | |
| private int | |
| private final int | |
| private boolean | |
| private boolean | |
| private void | |
| public String | |
| public String | |
| public String | |
| public String | |
| public int | |
| public int | |
| public int | |
| public int | |
| public int | |
| public static ByteQuadsCanonicalizer | Returns: Root instance to use for constructing new child instancesFactory method to call to create a symbol table instance with a randomized seed value. |
| protected static ByteQuadsCanonicalizer | |
| public String | |
| public String | |
| public String | |
| public String | |
| public int | |
| public boolean | Returns: True for "real", canonicalizing child tables; false for root table as well as placeholder "child" tables.
|
| public ByteQuadsCanonicalizer | Returns: Actual canonicalizer instance that can be used by a parserBit flags of active flags)com.s enabled.Factory method used to create actual symbol table instance to use for parsing. |
| public ByteQuadsCanonicalizer | Returns: Actual canonicalizer instance that can be used by a parser if (and only if) canonicalization is enabled; otherwise a non-null "placeholder" instance.Bit flags of active flags)com.s enabled.Method similar to |
| public boolean | Returns: Whether main hash area has been modifiedMethod called to check to quickly see if a child symbol table may have gotten additional entries. |
| private void | |
| private void | nukeSymbols(boolean fill)
Helper method called to empty all shared symbols, but to leave arrays allocated |
| public int | Returns: Number of entries in the primary hash areaMethod mostly needed by unit tests; calculates number of entries that are in the primary slot set. |
| private void | |
| public void | |
| public int | Returns: Number of entries in the secondary hash areaMethod mostly needed by unit tests; calculates number of entries in secondary buckets |
| public int | |
| public int | Returns: Number of entries in the linear spill-over areayMethod mostly needed by unit tests; calculates number of entries in shared spill-over area |
| public int | Returns: Number of entries in the tertiary hash areaMethod mostly needed by unit tests; calculates number of entries in tertiary buckets |
| public String | |
| public int |
| _count | back to summary |
|---|---|
| protected int _count Total number of Strings in the symbol table; only used for child tables. | |
| _failOnDoS | back to summary |
|---|---|
| protected final boolean _failOnDoS Flag that indicates whether we should throw an exception if enough hash collisions are detected (true); or just worked around (false).
| |
| _hashArea | back to summary |
|---|---|
| protected int[] _hashArea Primary hash information area: consists of | |
| _hashShared | back to summary |
|---|---|
| protected boolean _hashShared Flag that indicates whether underlying data structures for the main hash area are shared or not. If they are, then they need to be handled in copy-on-write way, i.e. if they need to be modified, a copy needs to be made first; at this point it will not be shared any more, and can be modified. This flag needs to be checked both when adding new main entries, and when adding new collision list queues (i.e. creating a new collision list head entry) | |
| _hashSize | back to summary |
|---|---|
| protected int _hashSize Number of slots for primary entries within | |
| _interner | back to summary |
|---|---|
| protected final InternCache _interner Entity that knows how to
| |
| _longNameOffset | back to summary |
|---|---|
| protected int _longNameOffset Offset within | |
| _names | back to summary |
|---|---|
| protected String[] _names Array that contains | |
| _parent | back to summary |
|---|---|
| protected final ByteQuadsCanonicalizer _parent Reference to the root symbol table, for child tables, so that they can merge table information back as necessary. | |
| _secondaryStart | back to summary |
|---|---|
| protected int _secondaryStart Offset within | |
| _seed | back to summary |
|---|---|
| protected final int _seed Seed 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. | |
| _spilloverEnd | back to summary |
|---|---|
| protected int _spilloverEnd Pointer to the offset within spill-over area where there is room
for more spilled over entries (if any).
Spill over area is within fixed-size portion of | |
| _tableInfo | back to summary |
|---|---|
| protected final AtomicReference<ByteQuadsCanonicalizer. 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. Child tables do NOT use the reference. | |
| _tertiaryShift | back to summary |
|---|---|
| protected int _tertiaryShift Constant that determines size of buckets for tertiary entries:
Default value is 2, for buckets of 4 slots; grows bigger with bigger table sizes. | |
| _tertiaryStart | back to summary |
|---|---|
| protected int _tertiaryStart Offset within | |
| DEFAULT_T_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_T_SIZE Initial size of the primary hash area. Each entry consumes 4 ints (16 bytes), and secondary area is same as primary; so default size will use 2kB of memory (plus 64x4 or 64x8 (256/512 bytes) for references to Strings, and Strings themselves). | |
| MAX_ENTRIES_FOR_REUSE | back to summary |
|---|---|
| protected static final int MAX_ENTRIES_FOR_REUSE Let's only share reasonably sized symbol tables. Max size set to 3/4 of 8k; this corresponds to 256k main hash index. This should allow for enough distinct names for almost any case, while preventing ballooning for cases where names are unique (or close thereof). | |
| MAX_T_SIZE | back to summary |
|---|---|
| private static final int MAX_T_SIZE Let's not expand symbol tables past some maximum size; with unique (~= random) names. Size is in | |
| MIN_HASH_SIZE | back to summary |
|---|---|
| private static final int MIN_HASH_SIZE No point in trying to construct tiny tables, just need to resize soon. | |
| MULT | back to summary |
|---|---|
| private static final int MULT | |
| MULT2 | back to summary |
|---|---|
| private static final int MULT2 | |
| MULT3 | back to summary |
|---|---|
| private static final int MULT3 | |
| ByteQuadsCanonicalizer | back to summary |
|---|---|
| private ByteQuadsCanonicalizer(int sz, int seed) Constructor used for creating per-
| |
| ByteQuadsCanonicalizer | back to summary |
|---|---|
| private ByteQuadsCanonicalizer(ByteQuadsCanonicalizer parent, int seed, ByteQuadsCanonicalizer. Constructor used when creating a child instance | |
| ByteQuadsCanonicalizer | back to summary |
|---|---|
| private ByteQuadsCanonicalizer(ByteQuadsCanonicalizer. Alternate constructor used in cases where a "placeholder" child instance is needed when symbol table is not really used, but caller needs a non-null placeholder to keep code functioning with minimal awareness of distinction (all lookups fail to match any name without error; add methods should NOT be called).
| |
| _appendLongName | back to summary |
|---|---|
| private int _appendLongName(int[] quads, int qlen) | |
| _calcOffset | back to summary |
|---|---|
| private final int _calcOffset(int hash) | |
| _calcTertiaryShift | back to summary |
|---|---|
| pack-priv static int _calcTertiaryShift(int primarySlots) | |
| _checkNeedForRehash | back to summary |
|---|---|
| private boolean _checkNeedForRehash() | |
| _findOffsetForAdd | back to summary |
|---|---|
| private int _findOffsetForAdd(int hash) throws StreamConstraintsException Method called to find the location within hash table to add a new symbol in.
| |
| _findSecondary | back to summary |
|---|---|
| private String _findSecondary(int origOffset, int q1) | |
| _findSecondary | back to summary |
|---|---|
| private String _findSecondary(int origOffset, int q1, int q2) | |
| _findSecondary | back to summary |
|---|---|
| private String _findSecondary(int origOffset, int q1, int q2, int q3) | |
| _findSecondary | back to summary |
|---|---|
| private String _findSecondary(int origOffset, int hash, int[] q, int qlen) | |
| _reportTooManyCollisions | back to summary |
|---|---|
| protected void _reportTooManyCollisions() throws StreamConstraintsException | |
| _resizeAndFindOffsetForAdd | back to summary |
|---|---|
| private int _resizeAndFindOffsetForAdd(int hash) throws StreamConstraintsException | |
| _spilloverStart | back to summary |
|---|---|
| private final int _spilloverStart() Helper method that calculates start of the spillover area | |
| _verifyLongName | back to summary |
|---|---|
| private boolean _verifyLongName(int[] q, int qlen, int spillOffset) | |
| _verifyLongName2 | back to summary |
|---|---|
| private boolean _verifyLongName2(int[] q, int qlen, int spillOffset) | |
| _verifySharing | back to summary |
|---|---|
| private void _verifySharing() | |
| addName | back to summary |
|---|---|
public String addName(String name, int q1) throws StreamConstraintsException
| |
| addName | back to summary |
|---|---|
public String addName(String name, int q1, int q2) throws StreamConstraintsException
| |
| addName | back to summary |
|---|---|
public String addName(String name, int q1, int q2, int q3) throws StreamConstraintsException
| |
| addName | back to summary |
|---|---|
public String addName(String name, int[] q, int qlen) throws StreamConstraintsException
| |
| bucketCount | back to summary |
|---|---|
public int bucketCount()
| |
| calcHash | back to summary |
|---|---|
| public int calcHash(int q1) | |
| calcHash | back to summary |
|---|---|
| public int calcHash(int q1, int q2) | |
| calcHash | back to summary |
|---|---|
| public int calcHash(int q1, int q2, int q3) | |
| calcHash | back to summary |
|---|---|
public int calcHash(int[] q, int qlen)
| |
| createRoot | back to summary |
|---|---|
| public static ByteQuadsCanonicalizer createRoot() Factory method to call to create a symbol table instance with a randomized seed value.
| |
| createRoot | back to summary |
|---|---|
| protected static ByteQuadsCanonicalizer createRoot(int seed) | |
| findName | back to summary |
|---|---|
| public String findName(int q1) | |
| findName | back to summary |
|---|---|
| public String findName(int q1, int q2) | |
| findName | back to summary |
|---|---|
| public String findName(int q1, int q2, int q3) | |
| findName | back to summary |
|---|---|
| public String findName(int[] q, int qlen) | |
| hashSeed | back to summary |
|---|---|
| public int hashSeed() | |
| isCanonicalizing | back to summary |
|---|---|
public boolean isCanonicalizing()
| |
| makeChild | back to summary |
|---|---|
| public ByteQuadsCanonicalizer makeChild(int flags) Factory method used to create actual symbol table instance to use for parsing.
| |
| makeChildOrPlaceholder | back to summary |
|---|---|
| public ByteQuadsCanonicalizer makeChildOrPlaceholder(int flags) Method similar to
| |
| maybeDirty | back to summary |
|---|---|
| public boolean maybeDirty() Method called to check to quickly see if a child symbol table may have gotten additional entries. Used for checking to see if a child table should be merged into shared table.
| |
| mergeChild | back to summary |
|---|---|
| private void mergeChild(ByteQuadsCanonicalizer. | |
| nukeSymbols | back to summary |
|---|---|
| private void nukeSymbols(boolean fill) Helper method called to empty all shared symbols, but to leave arrays allocated | |
| primaryCount | back to summary |
|---|---|
| public int primaryCount() Method mostly needed by unit tests; calculates number of entries that are in the primary slot set. These are "perfect" entries, accessible with a single lookup
| |
| rehash | back to summary |
|---|---|
| private void rehash() throws StreamConstraintsException | |
| release | back to summary |
|---|---|
| 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. | |
| secondaryCount | back to summary |
|---|---|
| public int secondaryCount() Method mostly needed by unit tests; calculates number of entries in secondary buckets
| |
| size | back to summary |
|---|---|
public int size()
| |
| spilloverCount | back to summary |
|---|---|
| public int spilloverCount() Method mostly needed by unit tests; calculates number of entries in shared spill-over area
| |
| tertiaryCount | back to summary |
|---|---|
| public int tertiaryCount() Method mostly needed by unit tests; calculates number of entries in tertiary buckets
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| totalCount | back to summary |
|---|---|
| public int totalCount() | |
| Modifier and Type | Field and Description |
|---|---|
| public final int | |
| public final int | |
| public final int[] | |
| public final String[] | |
| public final int | |
| public final int | |
| public final int |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static ByteQuadsCanonicalizer. |
| count | back to summary |
|---|---|
| public final int count | |
| longNameOffset | back to summary |
|---|---|
| public final int longNameOffset | |
| mainHash | back to summary |
|---|---|
| public final int[] mainHash | |
| names | back to summary |
|---|---|
| public final String[] names | |
| size | back to summary |
|---|---|
| public final int size | |
| spilloverEnd | back to summary |
|---|---|
| public final int spilloverEnd | |
| tertiaryShift | back to summary |
|---|---|
| public final int tertiaryShift | |
| TableInfo | back to summary |
|---|---|
| public TableInfo(int size, int count, int tertiaryShift, int[] mainHash, String[] names, int spilloverEnd, int longNameOffset) | |
| TableInfo | back to summary |
|---|---|
| public TableInfo(ByteQuadsCanonicalizer src) | |
| createInitial | back to summary |
|---|---|
| public static ByteQuadsCanonicalizer. | |