Class OptimizedFrequencySketch<K>

  • Type Parameters:
    K - the type of keys to track frequency for

    public class OptimizedFrequencySketch<K>
    extends Object
    Ultra-optimized frequency sketch implementation with minimal allocation overhead.

    This implementation uses several advanced techniques to minimize object allocation:

    • Primitive Arrays: Uses long arrays instead of objects for counters
    • Bit Manipulation: Packs multiple 4-bit counters into single longs
    • Fast Hashing: Uses multiple hash functions with bit shifting
    • Atomic Operations: Lock-free updates with compare-and-swap
    • Memory Locality: Optimized data layout for CPU cache efficiency

    Performance Characteristics:

    • Increment: O(1) with ~2-3 CPU instructions
    • Frequency: O(1) with ~1-2 CPU instructions
    • Memory: 4 bits per counter, 64 counters per long
    • Concurrency: Lock-free with atomic operations
    Since:
    1.0.0
    • Constructor Detail

      • OptimizedFrequencySketch

        public OptimizedFrequencySketch​(long capacity)
        Creates a new optimized frequency sketch.
        Parameters:
        capacity - the maximum number of unique keys to track
    • Method Detail

      • increment

        public void increment​(K key)
        Increment the frequency counter for a key with minimal allocation.
        Parameters:
        key - the key to increment
      • frequency

        public int frequency​(K key)
        Get the frequency estimate for a key.
        Parameters:
        key - the key to check
        Returns:
        the frequency estimate (0-15)
      • reset

        public void reset()
        Reset all counters (periodic aging).
      • getStats

        public String getStats()
        Get statistics about the sketch.
        Returns:
        statistics string