Package io.github.dhruv1110.jcachex
Class OptimizedFrequencySketch<K>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.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 Summary
Constructors Constructor Description OptimizedFrequencySketch(long capacity)
Creates a new optimized frequency sketch.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
frequency(K key)
Get the frequency estimate for a key.String
getStats()
Get statistics about the sketch.void
increment(K key)
Increment the frequency counter for a key with minimal allocation.void
reset()
Reset all counters (periodic aging).
-
-
-
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
-
-