open-humanity
Privacy-first peer assistance network for sovereign autonomous agents
Brought by drakestapleton
open-humanity / crates / beacon-core / src / cuckoo.rs
| 1 | //! Fixed-capacity, zero-allocation Cuckoo Filter optimized for L3 CPU cache. |
| 2 | //! |
| 3 | //! Total footprint: 2048 buckets * 4 slots * 8 bytes = 64 KiB. |
| 4 | //! Provides deterministic sub-10-nanosecond fingerprint matching. |
| 5 | |
| 6 | pub const BUCKET_COUNT: usize = 2048; |
| 7 | pub const ENTRIES_PER_BUCKET: usize = 4; |
| 8 | pub const MAX_KICKS: usize = 500; |
| 9 | |
| 10 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] |
| 11 | #[repr(transparent)] |
| 12 | pub struct Tag(pub u64); |
| 13 | |
| 14 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 15 | #[repr(C, align(64))] |
| 16 | pub struct Bucket { |
| 17 | pub slots: [Tag; ENTRIES_PER_BUCKET], |
| 18 | } |
| 19 | |
| 20 | impl Default for Bucket { |
| 21 | fn default() -> Self { |
| 22 | Self { |
| 23 | slots: [Tag(0); ENTRIES_PER_BUCKET], |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | pub struct CuckooFilter { |
| 29 | buckets: Box<[Bucket; BUCKET_COUNT]>, |
| 30 | count: usize, |
| 31 | } |
| 32 | |
| 33 | impl Default for CuckooFilter { |
| 34 | fn default() -> Self { |
| 35 | Self::new() |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl CuckooFilter { |
| 40 | pub fn new() -> Self { |
| 41 | let buckets = Box::new([Bucket::default(); BUCKET_COUNT]); |
| 42 | Self { buckets, count: 0 } |
| 43 | } |
| 44 | |
| 45 | #[inline(always)] |
| 46 | fn get_tag(item: &[u8; 32]) -> Tag { |
| 47 | let mut bytes = [0u8; 8]; |
| 48 | bytes.copy_from_slice(&item[0..8]); |
| 49 | let raw = u64::from_le_bytes(bytes); |
| 50 | // Ensure non-zero tag so 0 represents empty slot |
| 51 | if raw == 0 { |
| 52 | Tag(1) |
| 53 | } else { |
| 54 | Tag(raw) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[inline(always)] |
| 59 | fn primary_index(item: &[u8; 32]) -> usize { |
| 60 | let mut bytes = [0u8; 8]; |
| 61 | bytes.copy_from_slice(&item[8..16]); |
| 62 | (u64::from_le_bytes(bytes) as usize) % BUCKET_COUNT |
| 63 | } |
| 64 | |
| 65 | #[inline(always)] |
| 66 | fn secondary_index(primary: usize, tag: Tag) -> usize { |
| 67 | let tag_hash = tag.0.wrapping_mul(0x517cc1b727220a95); |
| 68 | (primary ^ (tag_hash as usize)) % BUCKET_COUNT |
| 69 | } |
| 70 | |
| 71 | #[inline(always)] |
| 72 | pub fn contains(&self, item: &[u8; 32]) -> bool { |
| 73 | let tag = Self::get_tag(item); |
| 74 | let i1 = Self::primary_index(item); |
| 75 | let b1 = &self.buckets[i1]; |
| 76 | if b1.slots[0] == tag || b1.slots[1] == tag || b1.slots[2] == tag || b1.slots[3] == tag { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | let i2 = Self::secondary_index(i1, tag); |
| 81 | let b2 = &self.buckets[i2]; |
| 82 | b2.slots[0] == tag || b2.slots[1] == tag || b2.slots[2] == tag || b2.slots[3] == tag |
| 83 | } |
| 84 | |
| 85 | pub fn insert(&mut self, item: &[u8; 32]) -> bool { |
| 86 | if self.contains(item) { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | let mut tag = Self::get_tag(item); |
| 91 | let mut idx = Self::primary_index(item); |
| 92 | |
| 93 | for slot in &mut self.buckets[idx].slots { |
| 94 | if slot.0 == 0 { |
| 95 | *slot = tag; |
| 96 | self.count += 1; |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | let alt_idx = Self::secondary_index(idx, tag); |
| 102 | for slot in &mut self.buckets[alt_idx].slots { |
| 103 | if slot.0 == 0 { |
| 104 | *slot = tag; |
| 105 | self.count += 1; |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Cuckoo displacement |
| 111 | let mut rng = idx as u64; |
| 112 | for _ in 0..MAX_KICKS { |
| 113 | let slot_idx = (rng as usize) % ENTRIES_PER_BUCKET; |
| 114 | rng = rng.wrapping_mul(6364136223846793005).wrapping_add(1); |
| 115 | |
| 116 | let evicted = self.buckets[idx].slots[slot_idx]; |
| 117 | self.buckets[idx].slots[slot_idx] = tag; |
| 118 | tag = evicted; |
| 119 | idx = Self::secondary_index(idx, tag); |
| 120 | |
| 121 | for slot in &mut self.buckets[idx].slots { |
| 122 | if slot.0 == 0 { |
| 123 | *slot = tag; |
| 124 | self.count += 1; |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | false |
| 131 | } |
| 132 | |
| 133 | pub fn delete(&mut self, item: &[u8; 32]) -> bool { |
| 134 | let tag = Self::get_tag(item); |
| 135 | let i1 = Self::primary_index(item); |
| 136 | for slot in &mut self.buckets[i1].slots { |
| 137 | if *slot == tag { |
| 138 | *slot = Tag(0); |
| 139 | self.count = self.count.saturating_sub(1); |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | let i2 = Self::secondary_index(i1, tag); |
| 145 | for slot in &mut self.buckets[i2].slots { |
| 146 | if *slot == tag { |
| 147 | *slot = Tag(0); |
| 148 | self.count = self.count.saturating_sub(1); |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | false |
| 154 | } |
| 155 | |
| 156 | pub fn len(&self) -> usize { |
| 157 | self.count |
| 158 | } |
| 159 | |
| 160 | pub fn is_empty(&self) -> bool { |
| 161 | self.count == 0 |
| 162 | } |
| 163 | } |