java.lang.Object
net.i2p.data.SDSCache.IntWeakCache<V>
- Type Parameters:
V- type of object cached
- Enclosing class:
SDSCache<V extends SimpleDataStructure>
An int-keyed, weak-value cache, replacing a ConcurrentHashMap<Integer, WeakReference<V>> so the hot lookup neither hashes an Integer nor boxes the key - the first 4 bytes of a SimpleDataStructure almost always fall outside the Integer cache.
Occupancy is marked by the AtomicReferenceArray rather than by the key, since every int is a legal key (a hash of ff ff ff ff is -1). Its release/acquire ordering also means a reader that sees a reference sees its key and everything the writer did before publishing it.
As with the map it replaces, a collected value keeps its slot until the key is written again, so the table grows with the number of distinct keys seen rather than with the number of live entries.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classReplaced wholesale (never resized in place) so a reader always sees a complete table.Field Summary
FieldsModifier and TypeFieldDescriptionprivate intOccupied slots in the current table.private SDSCache.IntWeakCache.TableThe table in use now.private static final intLargest table allocated up front, however big the caller expects the cache to get.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescription(package private) voidclear()Drop all entries.(package private) WeakReference<V> get(int key) The cached entry, if still referenced.private SDSCache.IntWeakCache.Tablegrow()Doubles the table.(package private) voidput(int key, WeakReference<V> ref) Store a reference, replacing any existing entry for the key.private static intslot(int key, int mask) Mixes the key, so that nearby keys land in different slots.
Field Details
MAX_INITIAL_CAPACITY
private static final int MAX_INITIAL_CAPACITYLargest table allocated up front, however big the caller expects the cache to get.- See Also:
_table
The table in use now._size
private int _sizeOccupied slots in the current table. Guarded by this.
Constructor Details
IntWeakCache
IntWeakCache(int expectedSize) Builds a cache for the expected size.- Parameters:
expectedSize- the number of entries expected
Method Details
slot
private static int slot(int key, int mask) Mixes the key, so that nearby keys land in different slots.- Parameters:
key- the key to hashmask- the mask of the table- Returns:
- the slot the key hashes to
get
The cached entry, if still referenced.- Parameters:
key- the 4-byte index- Returns:
- the reference for the key, or null. A slot whose key is published but whose reference is not yet reads as empty, so a reader racing a put may see a miss; the put holds the lock.
put
Store a reference, replacing any existing entry for the key. Only misses reach here, so this is off the hot path.- Parameters:
key- the 4-byte indexref- the value
grow
Doubles the table. Callers must synchronize on this.- Returns:
- the new table
clear
void clear()Drop all entries.