|
| 1 | +//! Compressed node implementations optimized for cache line efficiency. |
| 2 | +//! |
| 3 | +//! This module contains CompressedLeafNode that fits exactly within 4 cache lines (256 bytes) |
| 4 | +//! for optimal memory access patterns and reduced cache pressure. |
| 5 | +
|
| 6 | +use std::marker::PhantomData; |
| 7 | +use std::mem; |
| 8 | +use crate::types::NodeId; |
| 9 | + |
| 10 | +/// A leaf node compressed to exactly 4 cache lines (256 bytes) for optimal cache performance. |
| 11 | +/// |
| 12 | +/// Memory layout: |
| 13 | +/// - Header: 8 bytes (capacity, len, next) + PhantomData (zero-sized) |
| 14 | +/// - Data: 248 bytes (inline storage for keys and values) |
| 15 | +/// |
| 16 | +/// Keys and values are stored in separate contiguous regions within the data array: |
| 17 | +/// [key0, key1, ..., keyN, value0, value1, ..., valueN] |
| 18 | +#[repr(C, align(64))] |
| 19 | +pub struct CompressedLeafNode<K, V> { |
| 20 | + /// Maximum number of key-value pairs this node can hold |
| 21 | + capacity: u16, |
| 22 | + /// Current number of key-value pairs |
| 23 | + len: u16, |
| 24 | + /// Next leaf node in the linked list (for range queries) |
| 25 | + next: NodeId, |
| 26 | + /// Phantom data to maintain type parameters (zero-sized) |
| 27 | + _phantom: PhantomData<(K, V)>, |
| 28 | + /// Raw storage for keys and values |
| 29 | + data: [u8; 248], |
| 30 | +} |
| 31 | + |
| 32 | +impl<K, V> CompressedLeafNode<K, V> |
| 33 | +where |
| 34 | + K: Copy + Ord, |
| 35 | + V: Copy, |
| 36 | +{ |
| 37 | + /// Create a new empty compressed leaf node. |
| 38 | + /// |
| 39 | + /// # Arguments |
| 40 | + /// * `capacity` - Maximum number of key-value pairs (limited by available space) |
| 41 | + /// |
| 42 | + /// # Returns |
| 43 | + /// A new empty compressed leaf node |
| 44 | + pub fn new(capacity: u16) -> Self { |
| 45 | + Self { |
| 46 | + capacity, |
| 47 | + len: 0, |
| 48 | + next: crate::types::NULL_NODE, |
| 49 | + _phantom: PhantomData, |
| 50 | + data: [0; 248], |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Returns the number of key-value pairs in this leaf. |
| 55 | + #[inline] |
| 56 | + pub fn len(&self) -> usize { |
| 57 | + self.len as usize |
| 58 | + } |
| 59 | + |
| 60 | + /// Returns the maximum capacity of this leaf. |
| 61 | + #[inline] |
| 62 | + pub fn capacity(&self) -> usize { |
| 63 | + self.capacity as usize |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns true if this leaf is empty. |
| 67 | + #[inline] |
| 68 | + pub fn is_empty(&self) -> bool { |
| 69 | + self.len == 0 |
| 70 | + } |
| 71 | + |
| 72 | + /// Returns true if this leaf is at capacity. |
| 73 | + #[inline] |
| 74 | + pub fn is_full(&self) -> bool { |
| 75 | + self.len >= self.capacity |
| 76 | + } |
| 77 | + |
| 78 | + /// Calculate the maximum number of key-value pairs that can fit in the available space. |
| 79 | + pub fn calculate_max_capacity() -> u16 { |
| 80 | + let pair_size = mem::size_of::<K>() + mem::size_of::<V>(); |
| 81 | + let available_space = 248; // data array size |
| 82 | + (available_space / pair_size) as u16 |
| 83 | + } |
| 84 | + |
| 85 | + /// Get a pointer to the keys region in the data array. |
| 86 | + #[inline] |
| 87 | + unsafe fn keys_ptr(&self) -> *const K { |
| 88 | + self.data.as_ptr() as *const K |
| 89 | + } |
| 90 | + |
| 91 | + /// Get a mutable pointer to the keys region in the data array. |
| 92 | + #[inline] |
| 93 | + unsafe fn keys_ptr_mut(&mut self) -> *mut K { |
| 94 | + self.data.as_mut_ptr() as *mut K |
| 95 | + } |
| 96 | + |
| 97 | + /// Get a pointer to the values region in the data array. |
| 98 | + #[inline] |
| 99 | + unsafe fn values_ptr(&self) -> *const V { |
| 100 | + let keys_size = self.capacity as usize * mem::size_of::<K>(); |
| 101 | + (self.data.as_ptr().add(keys_size)) as *const V |
| 102 | + } |
| 103 | + |
| 104 | + /// Get a mutable pointer to the values region in the data array. |
| 105 | + #[inline] |
| 106 | + unsafe fn values_ptr_mut(&mut self) -> *mut V { |
| 107 | + let keys_size = self.capacity as usize * mem::size_of::<K>(); |
| 108 | + (self.data.as_mut_ptr().add(keys_size)) as *mut V |
| 109 | + } |
| 110 | + |
| 111 | + /// Get a reference to a key at the given index. |
| 112 | + #[inline] |
| 113 | + unsafe fn key_at(&self, index: usize) -> &K { |
| 114 | + &*self.keys_ptr().add(index) |
| 115 | + } |
| 116 | + |
| 117 | + /// Get a reference to a value at the given index. |
| 118 | + #[inline] |
| 119 | + unsafe fn value_at(&self, index: usize) -> &V { |
| 120 | + &*self.values_ptr().add(index) |
| 121 | + } |
| 122 | + |
| 123 | + /// Get a mutable reference to a value at the given index. |
| 124 | + #[inline] |
| 125 | + unsafe fn value_at_mut(&mut self, index: usize) -> &mut V { |
| 126 | + &mut *self.values_ptr_mut().add(index) |
| 127 | + } |
| 128 | + |
| 129 | + /// Set a key at the given index. |
| 130 | + #[inline] |
| 131 | + unsafe fn set_key_at(&mut self, index: usize, key: K) { |
| 132 | + *self.keys_ptr_mut().add(index) = key; |
| 133 | + } |
| 134 | + |
| 135 | + /// Set a value at the given index. |
| 136 | + #[inline] |
| 137 | + unsafe fn set_value_at(&mut self, index: usize, value: V) { |
| 138 | + *self.values_ptr_mut().add(index) = value; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Placeholder implementations - will be implemented through TDD |
| 143 | +impl<K, V> CompressedLeafNode<K, V> |
| 144 | +where |
| 145 | + K: Copy + Ord, |
| 146 | + V: Copy, |
| 147 | +{ |
| 148 | + /// Insert a key-value pair into the leaf. |
| 149 | + pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, &'static str> { |
| 150 | + todo!("Implement through TDD") |
| 151 | + } |
| 152 | + |
| 153 | + /// Get a value by key. |
| 154 | + pub fn get(&self, key: &K) -> Option<&V> { |
| 155 | + todo!("Implement through TDD") |
| 156 | + } |
| 157 | + |
| 158 | + /// Remove a key-value pair from the leaf. |
| 159 | + pub fn remove(&mut self, key: &K) -> Option<V> { |
| 160 | + todo!("Implement through TDD") |
| 161 | + } |
| 162 | + |
| 163 | + /// Iterator over key-value pairs in sorted order. |
| 164 | + pub fn iter(&self) -> CompressedLeafIter<K, V> { |
| 165 | + todo!("Implement through TDD") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// Iterator over key-value pairs in a compressed leaf node. |
| 170 | +pub struct CompressedLeafIter<'a, K, V> { |
| 171 | + node: &'a CompressedLeafNode<K, V>, |
| 172 | + index: usize, |
| 173 | + _phantom: PhantomData<(&'a K, &'a V)>, |
| 174 | +} |
| 175 | + |
| 176 | +impl<'a, K, V> Iterator for CompressedLeafIter<'a, K, V> |
| 177 | +where |
| 178 | + K: Copy + Ord, |
| 179 | + V: Copy, |
| 180 | +{ |
| 181 | + type Item = (&'a K, &'a V); |
| 182 | + |
| 183 | + fn next(&mut self) -> Option<Self::Item> { |
| 184 | + todo!("Implement through TDD") |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +#[cfg(test)] |
| 189 | +mod tests { |
| 190 | + use super::*; |
| 191 | + |
| 192 | + // Phase 1: Memory Layout Verification Tests |
| 193 | + |
| 194 | + #[test] |
| 195 | + fn compressed_leaf_fits_four_cache_lines() { |
| 196 | + assert_eq!(std::mem::size_of::<CompressedLeafNode<i32, i32>>(), 256); |
| 197 | + assert_eq!(std::mem::align_of::<CompressedLeafNode<i32, i32>>(), 64); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn memory_is_contiguous() { |
| 202 | + let leaf = CompressedLeafNode::<i32, i32>::new(10); |
| 203 | + let start_ptr = &leaf as *const _ as *const u8; |
| 204 | + let end_ptr = unsafe { start_ptr.add(256) }; |
| 205 | + |
| 206 | + // Verify the struct spans exactly 256 bytes |
| 207 | + assert_eq!(std::mem::size_of_val(&leaf), 256); |
| 208 | + |
| 209 | + // Print actual field offsets for debugging |
| 210 | + let capacity_offset = unsafe { |
| 211 | + (&leaf.capacity as *const u16 as *const u8).offset_from(start_ptr) |
| 212 | + }; |
| 213 | + let len_offset = unsafe { |
| 214 | + (&leaf.len as *const u16 as *const u8).offset_from(start_ptr) |
| 215 | + }; |
| 216 | + let next_offset = unsafe { |
| 217 | + (&leaf.next as *const u32 as *const u8).offset_from(start_ptr) |
| 218 | + }; |
| 219 | + let phantom_offset = unsafe { |
| 220 | + (&leaf._phantom as *const _ as *const u8).offset_from(start_ptr) |
| 221 | + }; |
| 222 | + let data_offset = unsafe { |
| 223 | + (leaf.data.as_ptr()).offset_from(start_ptr) |
| 224 | + }; |
| 225 | + |
| 226 | + println!("Field offsets:"); |
| 227 | + println!(" capacity: {}", capacity_offset); |
| 228 | + println!(" len: {}", len_offset); |
| 229 | + println!(" next: {}", next_offset); |
| 230 | + println!(" phantom: {}", phantom_offset); |
| 231 | + println!(" data: {}", data_offset); |
| 232 | + |
| 233 | + assert_eq!(capacity_offset, 0); |
| 234 | + assert_eq!(len_offset, 2); |
| 235 | + assert_eq!(next_offset, 4); |
| 236 | + assert_eq!(phantom_offset, 8); |
| 237 | + assert_eq!(data_offset, 8); // PhantomData is zero-sized |
| 238 | + |
| 239 | + // Verify data array ends at struct boundary |
| 240 | + let data_end = unsafe { leaf.data.as_ptr().add(248) }; |
| 241 | + assert_eq!(data_end as *const u8, end_ptr); |
| 242 | + } |
| 243 | + |
| 244 | + #[test] |
| 245 | + fn verify_cache_line_alignment() { |
| 246 | + let leaf = CompressedLeafNode::<i32, i32>::new(10); |
| 247 | + let addr = &leaf as *const _ as usize; |
| 248 | + |
| 249 | + // Should be aligned to 64-byte boundary |
| 250 | + assert_eq!(addr % 64, 0); |
| 251 | + } |
| 252 | + |
| 253 | + // Phase 2: Basic Construction Tests |
| 254 | + |
| 255 | + #[test] |
| 256 | + fn new_compressed_leaf() { |
| 257 | + let leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 258 | + assert_eq!(leaf.len(), 0); |
| 259 | + assert_eq!(leaf.capacity(), 8); |
| 260 | + assert!(leaf.is_empty()); |
| 261 | + assert!(!leaf.is_full()); |
| 262 | + } |
| 263 | + |
| 264 | + #[test] |
| 265 | + fn calculate_max_capacity_for_i32_pairs() { |
| 266 | + let max_cap = CompressedLeafNode::<i32, i32>::calculate_max_capacity(); |
| 267 | + |
| 268 | + // i32 + i32 = 8 bytes per pair |
| 269 | + // 248 bytes available / 8 bytes per pair = 31 pairs |
| 270 | + assert_eq!(max_cap, 31); |
| 271 | + } |
| 272 | + |
| 273 | + #[test] |
| 274 | + fn calculate_max_capacity_for_different_types() { |
| 275 | + // u8 + u8 = 2 bytes per pair |
| 276 | + let u8_cap = CompressedLeafNode::<u8, u8>::calculate_max_capacity(); |
| 277 | + assert_eq!(u8_cap, 124); // 248 / 2 = 124 |
| 278 | + |
| 279 | + // u64 + u64 = 16 bytes per pair |
| 280 | + let u64_cap = CompressedLeafNode::<u64, u64>::calculate_max_capacity(); |
| 281 | + assert_eq!(u64_cap, 15); // 248 / 16 = 15 |
| 282 | + } |
| 283 | + |
| 284 | + // Phase 3: Single Insert/Get Tests (will fail until implemented) |
| 285 | + |
| 286 | + #[test] |
| 287 | + #[should_panic] // Remove this when implementing |
| 288 | + fn insert_single_item() { |
| 289 | + let mut leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 290 | + assert!(leaf.insert(42, 100).is_ok()); |
| 291 | + assert_eq!(leaf.len(), 1); |
| 292 | + assert_eq!(leaf.get(&42), Some(&100)); |
| 293 | + } |
| 294 | + |
| 295 | + #[test] |
| 296 | + #[should_panic] // Remove this when implementing |
| 297 | + fn get_nonexistent_key() { |
| 298 | + let leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 299 | + assert_eq!(leaf.get(&42), None); |
| 300 | + } |
| 301 | + |
| 302 | + // Phase 4: Multiple Insert Tests (will fail until implemented) |
| 303 | + |
| 304 | + #[test] |
| 305 | + #[should_panic] // Remove this when implementing |
| 306 | + fn insert_multiple_sorted() { |
| 307 | + let mut leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 308 | + for i in 0..5 { |
| 309 | + assert!(leaf.insert(i, i * 10).is_ok()); |
| 310 | + } |
| 311 | + assert_eq!(leaf.len(), 5); |
| 312 | + |
| 313 | + // Verify sorted order maintained |
| 314 | + for i in 0..5 { |
| 315 | + assert_eq!(leaf.get(&i), Some(&(i * 10))); |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + // Phase 5: Capacity Management Tests (will fail until implemented) |
| 320 | + |
| 321 | + #[test] |
| 322 | + #[should_panic] // Remove this when implementing |
| 323 | + fn insert_at_capacity() { |
| 324 | + let mut leaf = CompressedLeafNode::<i32, i32>::new(4); |
| 325 | + |
| 326 | + // Fill to capacity |
| 327 | + for i in 0..4 { |
| 328 | + assert!(leaf.insert(i, i * 10).is_ok()); |
| 329 | + } |
| 330 | + assert!(leaf.is_full()); |
| 331 | + |
| 332 | + // Attempt overflow |
| 333 | + assert!(leaf.insert(99, 990).is_err()); |
| 334 | + } |
| 335 | + |
| 336 | + // Phase 6: Remove Tests (will fail until implemented) |
| 337 | + |
| 338 | + #[test] |
| 339 | + #[should_panic] // Remove this when implementing |
| 340 | + fn remove_existing_key() { |
| 341 | + let mut leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 342 | + leaf.insert(42, 100).unwrap(); |
| 343 | + |
| 344 | + assert_eq!(leaf.remove(&42), Some(100)); |
| 345 | + assert_eq!(leaf.len(), 0); |
| 346 | + assert_eq!(leaf.get(&42), None); |
| 347 | + } |
| 348 | + |
| 349 | + // Phase 7: Iterator Tests (will fail until implemented) |
| 350 | + |
| 351 | + #[test] |
| 352 | + #[should_panic] // Remove this when implementing |
| 353 | + fn iterate_empty_leaf() { |
| 354 | + let leaf = CompressedLeafNode::<i32, i32>::new(8); |
| 355 | + let items: Vec<(&i32, &i32)> = leaf.iter().collect(); |
| 356 | + assert!(items.is_empty()); |
| 357 | + } |
| 358 | + |
| 359 | + // Memory efficiency verification |
| 360 | + #[test] |
| 361 | + fn memory_efficiency_comparison() { |
| 362 | + use crate::types::LeafNode; |
| 363 | + |
| 364 | + let regular_size = std::mem::size_of::<LeafNode<i32, i32>>(); |
| 365 | + let compressed_size = std::mem::size_of::<CompressedLeafNode<i32, i32>>(); |
| 366 | + |
| 367 | + println!("Regular LeafNode: {} bytes", regular_size); |
| 368 | + println!("Compressed LeafNode: {} bytes", compressed_size); |
| 369 | + |
| 370 | + assert_eq!(compressed_size, 256); // Exactly 4 cache lines |
| 371 | + |
| 372 | + // Should be more memory-efficient for reasonable capacities |
| 373 | + if regular_size > 256 { |
| 374 | + println!("Compressed node is {}x more memory efficient", |
| 375 | + regular_size as f64 / compressed_size as f64); |
| 376 | + } |
| 377 | + } |
| 378 | +} |
0 commit comments