|
| 1 | +"""4-bit packed Count-Min Sketch for frequency estimation. |
| 2 | +
|
| 3 | +Uses the same design as Caffeine/Theine: 4 hash functions, 4-bit counters |
| 4 | +packed 16 per uint64 word, periodic halving for aging. |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | + |
| 10 | +def _next_power_of_2(n: int) -> int: |
| 11 | + if n <= 0: |
| 12 | + return 1 |
| 13 | + n -= 1 |
| 14 | + n |= n >> 1 |
| 15 | + n |= n >> 2 |
| 16 | + n |= n >> 4 |
| 17 | + n |= n >> 8 |
| 18 | + n |= n >> 16 |
| 19 | + n |= n >> 32 |
| 20 | + return n + 1 |
| 21 | + |
| 22 | + |
| 23 | +def _rehash(h: int) -> int: |
| 24 | + h = (h ^ (h >> 32)) & 0xFFFFFFFFFFFFFFFF |
| 25 | + h = (h * 0x94D049BB133111EB) & 0xFFFFFFFFFFFFFFFF |
| 26 | + h = (h ^ (h >> 32)) & 0xFFFFFFFFFFFFFFFF |
| 27 | + return h |
| 28 | + |
| 29 | + |
| 30 | +_RESET_MASK = np.uint64(0x7777777777777777) |
| 31 | +_MAX_COUNT = 15 |
| 32 | + |
| 33 | + |
| 34 | +class CountMinSketch: |
| 35 | + """4-bit packed Count-Min Sketch with 4 hash functions. |
| 36 | +
|
| 37 | + Each counter is 4 bits (max value 15). 16 counters are packed into |
| 38 | + one uint64 word. The sketch uses 4 independent hash functions derived |
| 39 | + via iterative rehashing. |
| 40 | +
|
| 41 | + :param capacity: expected max number of tracked items (determines width) |
| 42 | + :param width_multiplier: width = next_power_of_2(capacity * multiplier) |
| 43 | + :param sample_size_multiplier: reset after this * capacity increments |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + capacity: int, |
| 49 | + width_multiplier: int = 1, |
| 50 | + sample_size_multiplier: int = 10, |
| 51 | + ): |
| 52 | + self._width = _next_power_of_2(max(capacity * width_multiplier, 16)) |
| 53 | + self._mask = self._width - 1 |
| 54 | + # 4 rows, each row has width counters, packed 16 per uint64 |
| 55 | + words_per_row = max(self._width // 16, 1) |
| 56 | + self._table = np.zeros(4 * words_per_row, dtype=np.uint64) |
| 57 | + self._words_per_row = words_per_row |
| 58 | + self._additions = 0 |
| 59 | + self._sample_size = max(capacity * sample_size_multiplier, 16) |
| 60 | + |
| 61 | + def increment(self, key_hash: int) -> bool: |
| 62 | + """Increment counters for the given hash. Returns True if any counter changed.""" |
| 63 | + h0 = _rehash(key_hash) |
| 64 | + h1 = _rehash(h0) |
| 65 | + h2 = _rehash(h1) |
| 66 | + h3 = _rehash(h2) |
| 67 | + |
| 68 | + added = self._inc_counter(0, h0 & self._mask) |
| 69 | + added |= self._inc_counter(1, h1 & self._mask) |
| 70 | + added |= self._inc_counter(2, h2 & self._mask) |
| 71 | + added |= self._inc_counter(3, h3 & self._mask) |
| 72 | + |
| 73 | + if added: |
| 74 | + self._additions += 1 |
| 75 | + |
| 76 | + return added |
| 77 | + |
| 78 | + def estimate(self, key_hash: int) -> int: |
| 79 | + """Return the estimated frequency (minimum across all rows).""" |
| 80 | + h0 = _rehash(key_hash) |
| 81 | + h1 = _rehash(h0) |
| 82 | + h2 = _rehash(h1) |
| 83 | + h3 = _rehash(h2) |
| 84 | + |
| 85 | + c0 = self._read_counter(0, h0 & self._mask) |
| 86 | + c1 = self._read_counter(1, h1 & self._mask) |
| 87 | + c2 = self._read_counter(2, h2 & self._mask) |
| 88 | + c3 = self._read_counter(3, h3 & self._mask) |
| 89 | + |
| 90 | + return min(c0, c1, c2, c3) |
| 91 | + |
| 92 | + def reset(self): |
| 93 | + """Halve all counters (aging / decay).""" |
| 94 | + self._table = (self._table >> np.uint64(1)) & _RESET_MASK |
| 95 | + self._additions = self._additions // 2 |
| 96 | + |
| 97 | + def _inc_counter(self, row: int, index: int) -> bool: |
| 98 | + word_idx = row * self._words_per_row + index // 16 |
| 99 | + nibble_pos = np.uint64((index % 16) * 4) |
| 100 | + current = int((self._table[word_idx] >> nibble_pos) & np.uint64(0xF)) |
| 101 | + if current < _MAX_COUNT: |
| 102 | + self._table[word_idx] += np.uint64(1) << nibble_pos |
| 103 | + return True |
| 104 | + return False |
| 105 | + |
| 106 | + def _read_counter(self, row: int, index: int) -> int: |
| 107 | + word_idx = row * self._words_per_row + index // 16 |
| 108 | + nibble_pos = np.uint64((index % 16) * 4) |
| 109 | + return int((self._table[word_idx] >> nibble_pos) & np.uint64(0xF)) |
| 110 | + |
| 111 | + @property |
| 112 | + def additions(self) -> int: |
| 113 | + return self._additions |
0 commit comments