Class CompactHashMap<K,V>
- All Implemented Interfaces:
Serializable
,Map<K,
V>
- Direct Known Subclasses:
CompactLinkedHashMap
containsKey(k)
, put(k, v)
and remove(k)
are all (expected and
amortized) constant time operations. Expected in the hashtable sense (depends on the hash
function doing a good job of distributing the elements to the buckets to a distribution not far
from uniform), and amortized since some operations can trigger a hash table resize.
Unlike java.util.HashMap
, iteration is only proportional to the actual size()
,
which is optimal, and not the size of the internal hashtable, which could be much larger
than size()
. Furthermore, this structure places significantly reduced load on the garbage
collector by only using a constant number of internal objects.
If there are no removals, then iteration order for the entrySet()
, keySet()
, and
values
views is the same as insertion order. Any removal invalidates any ordering
guarantees.
This class should not be assumed to be universally superior to java.util.HashMap
.
Generally speaking, this class reduces object allocation and memory consumption at the price of
moderately increased constant factors of CPU. Only use this class when there is a specific reason
to prioritize memory over CPU.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
private class
(package private) class
(package private) final class
(package private) class
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) int[]
Contains the logical entries, in the range of [0, size()).(package private) static final double
Maximum allowed false positive probability of detecting a hash flooding attack given random input.(package private) Object[]
The keys of the entries in the map, in the range of [0, size()).private static final int
Maximum allowed length of a hash table bucket before falling back to a j.u.LinkedHashMap-based implementation.private int
Keeps track of metadata like the number of hash table bits and modifications of this data structure (to make it possible to throw ConcurrentModificationException in the iterator).private static final Object
private int
The number of elements contained in the set.private Object
The hashtable object.(package private) Object[]
The values of the entries in the map, in the range of [0, size()).private Collection
<V> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty instance ofCompactHashMap
.CompactHashMap
(int expectedSize) Constructs a new instance ofCompactHashMap
with the specified capacity. -
Method Summary
Modifier and TypeMethodDescription(package private) void
accessEntry
(int index) Mark an access of the specified entry.(package private) int
adjustAfterRemove
(int indexBeforeRemove, int indexRemoved) Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at.(package private) int
Handle lazy allocation of arrays.void
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) static <K,
V> CompactHashMap <K, V> create()
Creates an emptyCompactHashMap
instance.createHashFloodingResistantDelegate
(int tableSize) (package private) Collection
<V> static <K,
V> CompactHashMap <K, V> createWithExpectedSize
(int expectedSize) Creates aCompactHashMap
instance, with a high enough "initial capacity" that it should holdexpectedSize
elements without growth.private int
entry
(int i) entrySet()
(package private) int
void
forEach
(BiConsumer<? super K, ? super V> action) (package private) int
getSuccessor
(int entryIndex) private int
Gets the hash table mask using the stored number of hash table bits.(package private) void
private int
(package private) void
init
(int expectedSize) Pseudoconstructor for serialization support.(package private) void
insertEntry
(int entryIndex, K key, V value, int hash, int mask) Creates a fresh entry with the specified object at the specified position in the entry arrays.boolean
isEmpty()
private K
key
(int i) keySet()
(package private) void
moveLastEntry
(int dstIndex, int mask) Moves the last entry in the entry array intodstIndex
, and nulls out its old position.(package private) boolean
Returns whether arrays need to be allocated.private void
readObject
(ObjectInputStream stream) private Object
removeHelper
(Object key) void
replaceAll
(BiFunction<? super K, ? super V, ? extends V> function) private int[]
private Object[]
private Object
private Object[]
(package private) void
resizeEntries
(int newCapacity) Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity.private void
resizeMeMaybe
(int newSize) Resizes the entries storage if necessary.private int
resizeTable
(int oldMask, int newCapacity, int targetHash, int targetEntryIndex) private void
setEntry
(int i, int value) private void
setHashTableMask
(int mask) Stores the hash table mask as the number of bits needed to represent an index.private void
private void
int
size()
void
Ensures that thisCompactHashMap
has the smallest representation in memory, given its current size.private V
value
(int i) values()
private void
writeObject
(ObjectOutputStream stream) Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, getOrDefault, merge, putIfAbsent, remove, replace, replace
-
Field Details
-
NOT_FOUND
-
HASH_FLOODING_FPP
static final double HASH_FLOODING_FPPMaximum allowed false positive probability of detecting a hash flooding attack given random input.- See Also:
-
MAX_HASH_BUCKET_LENGTH
private static final int MAX_HASH_BUCKET_LENGTHMaximum allowed length of a hash table bucket before falling back to a j.u.LinkedHashMap-based implementation. Experimentally determined.- See Also:
-
table
The hashtable object. This can be either:- a byte[], short[], or int[], with size a power of two, created by
CompactHashing.createTable, whose values are either
- UNSET, meaning "null pointer"
- one plus an index into the keys, values, and entries arrays
- another java.util.Map delegate implementation. In most modern JDKs, normal java.util hash collections intelligently fall back to a binary search tree if hash table collisions are detected. Rather than going to all the trouble of reimplementing this ourselves, we simply switch over to use the JDK implementation wholesale if probable hash flooding is detected, sacrificing the compactness guarantee in very rare cases in exchange for much more reliable worst-case behavior.
- null, if no entries have yet been added to the map
- a byte[], short[], or int[], with size a power of two, created by
CompactHashing.createTable, whose values are either
-
entries
@CheckForNull transient int[] entriesContains the logical entries, in the range of [0, size()). The high bits of each int are the part of the smeared hash of the key not covered by the hashtable mask, whereas the low bits are the "next" pointer (pointing to the next entry in the bucket chain), which will always be less than or equal to the hashtable mask.hash = aaaaaaaa mask = 00000fff next = 00000bbb entry = aaaaabbb
The pointers in [size(), entries.length) are all "null" (UNSET).
-
keys
The keys of the entries in the map, in the range of [0, size()). The keys in [size(), keys.length) are allnull
. -
values
The values of the entries in the map, in the range of [0, size()). The values in [size(), values.length) are allnull
. -
metadata
private transient int metadataKeeps track of metadata like the number of hash table bits and modifications of this data structure (to make it possible to throw ConcurrentModificationException in the iterator). Note that we choose not to make this volatile, so we do less of a "best effort" to track such errors, for better performance.For a new instance, where the arrays above have not yet been allocated, the value of
metadata
is the size that the arrays should be allocated with. Once the arrays have been allocated, the value ofmetadata
combines the number of bits in the "short hash", in its bottom 5 bits, with a modification count in the remaining bits that is used to detect concurrent modification during iteration. -
size
private transient int sizeThe number of elements contained in the set. -
keySetView
-
entrySetView
-
valuesView
-
-
Constructor Details
-
CompactHashMap
CompactHashMap()Constructs a new empty instance ofCompactHashMap
. -
CompactHashMap
CompactHashMap(int expectedSize) Constructs a new instance ofCompactHashMap
with the specified capacity.- Parameters:
expectedSize
- the initial capacity of thisCompactHashMap
.
-
-
Method Details
-
create
Creates an emptyCompactHashMap
instance. -
createWithExpectedSize
Creates aCompactHashMap
instance, with a high enough "initial capacity" that it should holdexpectedSize
elements without growth.- Parameters:
expectedSize
- the number of elements you expect to add to the returned set- Returns:
- a new, empty
CompactHashMap
with enough capacity to holdexpectedSize
elements without resizing - Throws:
IllegalArgumentException
- ifexpectedSize
is negative
-
init
void init(int expectedSize) Pseudoconstructor for serialization support. -
needsAllocArrays
boolean needsAllocArrays()Returns whether arrays need to be allocated. -
allocArrays
int allocArrays()Handle lazy allocation of arrays. -
delegateOrNull
-
createHashFloodingResistantDelegate
-
convertToHashFloodingResistantImplementation
-
setHashTableMask
private void setHashTableMask(int mask) Stores the hash table mask as the number of bits needed to represent an index. -
hashTableMask
private int hashTableMask()Gets the hash table mask using the stored number of hash table bits. -
incrementModCount
void incrementModCount() -
accessEntry
void accessEntry(int index) Mark an access of the specified entry. Used only inCompactLinkedHashMap
for LRU ordering. -
put
-
insertEntry
Creates a fresh entry with the specified object at the specified position in the entry arrays. -
resizeMeMaybe
private void resizeMeMaybe(int newSize) Resizes the entries storage if necessary. -
resizeEntries
void resizeEntries(int newCapacity) Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity. -
resizeTable
private int resizeTable(int oldMask, int newCapacity, int targetHash, int targetEntryIndex) -
indexOf
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
get
-
remove
-
removeHelper
-
moveLastEntry
void moveLastEntry(int dstIndex, int mask) Moves the last entry in the entry array intodstIndex
, and nulls out its old position. -
firstEntryIndex
int firstEntryIndex() -
getSuccessor
int getSuccessor(int entryIndex) -
adjustAfterRemove
int adjustAfterRemove(int indexBeforeRemove, int indexRemoved) Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at. -
replaceAll
- Specified by:
replaceAll
in interfaceMap<K,
V>
-
keySet
-
createKeySet
-
keySetIterator
-
forEach
-
entrySet
-
createEntrySet
-
entrySetIterator
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Overrides:
containsValue
in classAbstractMap<K,
V>
-
values
-
createValues
Collection<V> createValues() -
valuesIterator
-
trimToSize
public void trimToSize()Ensures that thisCompactHashMap
has the smallest representation in memory, given its current size. -
clear
public void clear() -
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOException
ClassNotFoundException
-
requireTable
-
requireEntries
private int[] requireEntries() -
requireKeys
-
requireValues
-
key
-
value
-
entry
private int entry(int i) -
setKey
-
setValue
-
setEntry
private void setEntry(int i, int value)
-