java.lang.Object
net.i2p.data.SDSCache.IntWeakCache<V>
Type Parameters:
V - type of object cached
Enclosing class:
SDSCache<V extends SimpleDataStructure>

private static final class SDSCache.IntWeakCache<V>extends Object
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 Classes
    Modifier and Type
    Class
    Description
    private static final class 
    Replaced wholesale (never resized in place) so a reader always sees a complete table.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    Occupied slots in the current table.
    The table in use now.
    private static final int
    Largest table allocated up front, however big the caller expects the cache to get.
  • Constructor Summary

    Constructors
    Constructor
    Description
    IntWeakCache(int expectedSize)
    Builds a cache for the expected size.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Drop all entries.
    (package private) WeakReference<V>
    get(int key)
    The cached entry, if still referenced.
    Doubles the table.
    (package private) void
    put(int key, WeakReference<V> ref)
    Store a reference, replacing any existing entry for the key.
    private static int
    slot(int key, int mask)
    Mixes the key, so that nearby keys land in different slots.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_INITIAL_CAPACITY

      private static final int MAX_INITIAL_CAPACITY
      Largest table allocated up front, however big the caller expects the cache to get.
      See Also:
    • _table

      private volatile SDSCache.IntWeakCache.Table _table
      The table in use now.
    • _size

      private int _size
      Occupied 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 hash
      mask - the mask of the table
      Returns:
      the slot the key hashes to
    • get

      WeakReference<V> get(int key)
      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

      void put(int key, WeakReference<V> ref)
      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 index
      ref - the value
    • grow

      Doubles the table. Callers must synchronize on this.
      Returns:
      the new table
    • clear

      void clear()
      Drop all entries.