pulse_map 0.6.4

A CPU cache-line hash table with zero-cost eviction. Every bucket fits in exactly one 64-byte cache line with embedded LFU+LRU eviction metadata.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright (c) 2026 Deendayal Kumawat. All rights reserved.
// Licensed under the MIT OR Apache-2.0 license.

//! Thread-safe concurrent PulseMap with per-bucket spinlocks.
//!
//! `ConcurrentPulseMap<K, V>` allows multiple threads to read and write
//! concurrently. Each bucket has its own spinlock, so operations on
//! different buckets run fully in parallel.
//!
//! ```
//! use pulse_map::ConcurrentPulseMap;
//! use std::sync::Arc;
//!
//! let map = Arc::new(ConcurrentPulseMap::<u32, u32>::new(256));
//! let map2 = map.clone();
//!
//! std::thread::spawn(move || {
//!     map2.insert(42, 100);
//! }).join().unwrap();
//!
//! assert_eq!(map.get(&42), Some(100));
//! ```

use portable_atomic::{AtomicU64, Ordering};
use std::cell::UnsafeCell;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU8, AtomicUsize};
use std::sync::{Mutex, RwLock};

use crate::engine::access_buffer::AccessBuffer;
use crate::engine::bucket::Bucket;
use crate::engine::hash::compute_hash;
use crate::engine::slab::SlabPool;
use crate::raw::SlotTTL;
use crate::{PulseKey, PulseValue, SlotState};

// ═══════════════════════════════════════════════════════════════
// Per-Bucket Spinlock
// ═══════════════════════════════════════════════════════════════

/// Per-bucket spinlock array. Each bucket gets an independent AtomicU8 lock.
struct BucketLocks {
    locks: Vec<AtomicU8>,
}

impl BucketLocks {
    fn new(num_buckets: usize) -> Self {
        let locks = (0..num_buckets).map(|_| AtomicU8::new(0)).collect();
        Self { locks }
    }

    #[inline]
    fn lock(&self, bucket_idx: usize) {
        while self.locks[bucket_idx]
            .compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
            .is_err()
        {
            std::hint::spin_loop();
        }
    }

    #[inline]
    fn unlock(&self, bucket_idx: usize) {
        self.locks[bucket_idx].store(0, Ordering::Release);
    }
}

/// RAII guard that automatically unlocks a bucket when dropped.
struct BucketGuard<'a> {
    locks: &'a BucketLocks,
    idx: usize,
}

impl<'a> BucketGuard<'a> {
    #[inline]
    fn new(locks: &'a BucketLocks, idx: usize) -> Self {
        locks.lock(idx);
        Self { locks, idx }
    }
}

impl Drop for BucketGuard<'_> {
    #[inline]
    fn drop(&mut self) {
        self.locks.unlock(self.idx);
    }
}

// ═══════════════════════════════════════════════════════════════
// Inner State (behind RwLock for resize support)
// ═══════════════════════════════════════════════════════════════

/// Resizable inner state. Protected by RwLock:
/// - Normal ops: read lock (concurrent, cheap)
/// - Resize: write lock (exclusive, blocks everything)
struct MapInner {
    buckets: Vec<UnsafeCell<Bucket>>,
    locks: BucketLocks,
    slab_pool: Mutex<SlabPool>,
    num_buckets: usize,
    bucket_mask: usize,
    /// Insertion epoch + per-entry TTL per slot. Separate Mutex allows safe writes under read lock.
    epochs: Mutex<Vec<SlotTTL>>,
}

// Safety: bucket access is protected by per-bucket spinlocks + RwLock.
unsafe impl Send for MapInner {}
unsafe impl Sync for MapInner {}

impl MapInner {
    fn new(num_buckets: usize) -> Self {
        let actual = num_buckets.max(1).next_power_of_two();
        let buckets = (0..actual)
            .map(|_| UnsafeCell::new(Bucket::empty()))
            .collect();
        Self {
            buckets,
            locks: BucketLocks::new(actual),
            slab_pool: Mutex::new(SlabPool::new()),
            num_buckets: actual,
            bucket_mask: actual - 1,
            epochs: Mutex::new(vec![SlotTTL::default(); actual * 4]),
        }
    }
}

// ═══════════════════════════════════════════════════════════════
// ConcurrentPulseMap
// ═══════════════════════════════════════════════════════════════

/// Thread-safe PulseMap with per-bucket locking and optional dynamic resize.
///
/// - All methods take `&self` (not `&mut self`) — safe to share via `Arc`.
/// - Different buckets are accessed fully in parallel.
/// - Same bucket: serialized via spinlock (fast for short critical sections).
/// - Resize: stop-the-world (write lock blocks all ops during rehash).
///
/// # Example
/// ```
/// use pulse_map::ConcurrentPulseMap;
/// use std::sync::Arc;
/// use std::thread;
///
/// let map = Arc::new(ConcurrentPulseMap::<u32, u64>::new(1024));
///
/// let handles: Vec<_> = (0..4).map(|t| {
///     let m = map.clone();
///     thread::spawn(move || {
///         for i in 0..1000u32 {
///             m.insert(t * 1000 + i, i as u64);
///         }
///     })
/// }).collect();
///
/// for h in handles { h.join().unwrap(); }
/// assert!(map.len() > 0);
/// ```
pub struct ConcurrentPulseMap<K: PulseKey, V: PulseValue> {
    inner: RwLock<MapInner>,
    count: AtomicUsize,
    eviction_count: AtomicUsize,
    auto_resize: bool,
    resize_threshold: f64,
    /// Global epoch counter — incremented on every insert.
    current_epoch: AtomicU64,
    /// Default TTL in insertion epochs. 0 = disabled.
    default_ttl: AtomicU64,
    /// Lock-free ring buffer for deferred LRU/LFU access tracking.
    /// Reads push events here instead of mutating MetaWord inline.
    access_buffer: AccessBuffer,
    _marker: PhantomData<(K, V)>,
}

// Safety: RwLock + per-bucket spinlocks protect all access.
unsafe impl<K: PulseKey, V: PulseValue> Send for ConcurrentPulseMap<K, V> {}
unsafe impl<K: PulseKey, V: PulseValue> Sync for ConcurrentPulseMap<K, V> {}

impl<K: PulseKey, V: PulseValue> ConcurrentPulseMap<K, V> {
    /// Create a new fixed-size concurrent PulseMap.
    ///
    /// `num_buckets` is rounded up to the next power of 2.
    /// Total capacity = `actual_buckets × 4` entries.
    pub fn new(num_buckets: usize) -> Self {
        Self {
            inner: RwLock::new(MapInner::new(num_buckets)),
            count: AtomicUsize::new(0),
            eviction_count: AtomicUsize::new(0),
            auto_resize: false,
            resize_threshold: 0.75,
            current_epoch: AtomicU64::new(0),
            default_ttl: AtomicU64::new(0),
            access_buffer: AccessBuffer::new(4096),
            _marker: PhantomData,
        }
    }

    /// Create a concurrent PulseMap that auto-resizes when load exceeds threshold.
    ///
    /// Default threshold: 75% load factor. When exceeded, the map doubles in size.
    ///
    /// ```
    /// use pulse_map::ConcurrentPulseMap;
    ///
    /// let map = ConcurrentPulseMap::<u32, u32>::with_auto_resize(64);
    /// for i in 0..1000u32 {
    ///     map.insert(i, i * 10);
    /// }
    /// // Map auto-grew from 64 to 1024+ buckets
    /// assert!(map.capacity() > 256);
    /// ```
    pub fn with_auto_resize(num_buckets: usize) -> Self {
        Self {
            inner: RwLock::new(MapInner::new(num_buckets)),
            count: AtomicUsize::new(0),
            eviction_count: AtomicUsize::new(0),
            auto_resize: true,
            resize_threshold: 0.75,
            current_epoch: AtomicU64::new(0),
            default_ttl: AtomicU64::new(0),
            access_buffer: AccessBuffer::new(4096),
            _marker: PhantomData,
        }
    }

    /// Set TTL in insertion epochs. 0 = disabled (default).
    ///
    /// Entries inserted more than `ttl_epochs` insertions ago
    /// are treated as expired — `get()`/`peek()` return `None`.
    #[inline]
    pub fn set_ttl(&self, ttl: u64) {
        self.default_ttl.store(ttl, Ordering::Relaxed);
    }

    /// Returns the current TTL setting (0 = disabled).
    #[inline]
    pub fn get_ttl(&self) -> u64 {
        self.default_ttl.load(Ordering::Relaxed)
    }

    /// Returns the current epoch counter (total insertions so far).
    #[inline]
    pub fn current_epoch(&self) -> u64 {
        self.current_epoch.load(Ordering::Relaxed)
    }

    /// Check if a slot has expired (per-entry or global TTL).
    #[inline]
    fn is_expired(&self, state: &MapInner, bucket_idx: usize, slot_idx: u8) -> bool {
        let entry = state.epochs.lock().unwrap()[bucket_idx * 4 + slot_idx as usize];
        let effective_ttl = if entry.ttl == 0 {
            self.default_ttl.load(Ordering::Relaxed)
        } else {
            entry.ttl
        };
        if effective_ttl == 0 || effective_ttl == u64::MAX {
            return false;
        }
        let epoch = self.current_epoch.load(Ordering::Relaxed);
        epoch.wrapping_sub(entry.epoch) > effective_ttl
    }

    /// Stamp the current epoch and per-entry TTL onto a slot.
    #[inline]
    fn stamp_epoch(&self, state: &MapInner, bucket_idx: usize, slot_idx: u8, ttl: u64) {
        let epoch = self.current_epoch.load(Ordering::Relaxed);
        state.epochs.lock().unwrap()[bucket_idx * 4 + slot_idx as usize] = SlotTTL { epoch, ttl };
    }

    /// Thread-safe insert. Uses the map's default TTL.
    pub fn insert(&self, key: K, value: V) {
        self.insert_internal(key, value, 0);
    }

    /// Thread-safe insert with a per-entry TTL override.
    ///
    /// - `ttl = 0`: use the map's default TTL
    /// - `ttl = u64::MAX`: this entry never expires
    /// - `ttl = N`: this entry expires after N insertions
    pub fn insert_ttl(&self, key: K, value: V, ttl: u64) {
        self.insert_internal(key, value, ttl);
    }

    /// Internal insert with TTL parameter.
    fn insert_internal(&self, key: K, value: V, ttl: u64) {
        if self.auto_resize {
            let state = self.inner.read().unwrap();
            let num_bkts = state.num_buckets;
            let cap = num_bkts * 4;
            let len = self.count.load(Ordering::Relaxed);
            let load = len as f64 / cap as f64;
            drop(state);
            if load > self.resize_threshold {
                self.resize(num_bkts * 2);
            }
        }

        // Advance epoch on every insert
        self.current_epoch.fetch_add(1, Ordering::Relaxed);

        // Note: Access buffer events (from get()) are NOT drained here.
        // The buffer is lossy — when full, new events are silently dropped.
        // This keeps insert latency low while providing approximate LRU/LFU tracking.

        let kb = key.to_bytes();
        let vb = value.to_bytes();
        let key_bytes = kb.as_ref();
        let val_bytes = vb.as_ref();
        let hr = compute_hash(key_bytes);

        let state = self.inner.read().unwrap();
        let idx = (hr.h1 as usize) & state.bucket_mask;

        let _guard = BucketGuard::new(&state.locks, idx);

        // Safety: We hold the bucket lock + RwLock read, exclusive bucket access.
        let bucket = unsafe { &mut *state.buckets[idx].get() };

        // 1. Check if key already exists (update in place)
        let mask = bucket.meta.match_mask(hr.h2);
        let mut m = mask;
        while m != 0 {
            let slot_idx = m.trailing_zeros() as u8;
            m &= m - 1;
            let slot = &bucket.slots[slot_idx as usize];
            if slot.matches_key(key_bytes, &hr, &state.slab_pool.lock().unwrap()) {
                // Free old slab entry if in slab mode
                if slot.get_mode() == 1 {
                    state.slab_pool.lock().unwrap().free(slot.slab_idx());
                }
                let s = &mut bucket.slots[slot_idx as usize];
                if key_bytes.len() <= 6 && val_bytes.len() <= 7 {
                    s.set_inline(key_bytes, val_bytes);
                } else {
                    let idx = state.slab_pool.lock().unwrap().alloc(key_bytes, val_bytes);
                    s.set_slab(hr.ext_fp_hi, hr.ext_fp, idx);
                }
                bucket.meta.on_access(slot_idx);
                // Refresh epoch and TTL on update
                self.stamp_epoch(&state, idx, slot_idx, ttl);
                return;
            }
        }

        // 2. Find free slot or evict
        let (target_slot, is_eviction) = if let Some(free) = bucket.meta.find_free_slot() {
            (free, false)
        } else if let Some(evict) = bucket.meta.find_evict_target() {
            // Free slab memory on eviction
            let old_slot = &bucket.slots[evict as usize];
            if old_slot.get_mode() == 1 {
                state.slab_pool.lock().unwrap().free(old_slot.slab_idx());
            }
            self.eviction_count.fetch_add(1, Ordering::Relaxed);
            (evict, true)
        } else {
            return;
        };

        // 3. Insert into target slot
        let slot = &mut bucket.slots[target_slot as usize];
        if key_bytes.len() <= 6 && val_bytes.len() <= 7 {
            slot.set_inline(key_bytes, val_bytes);
        } else {
            let idx = state.slab_pool.lock().unwrap().alloc(key_bytes, val_bytes);
            slot.set_slab(hr.ext_fp_hi, hr.ext_fp, idx);
        }

        bucket.meta.set_state(target_slot, SlotState::Full);
        bucket.meta.set_h2(target_slot, hr.h2);
        bucket.meta.on_insert(target_slot);

        // Stamp epoch for TTL
        self.stamp_epoch(&state, idx, target_slot, ttl);

        if !is_eviction {
            self.count.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Thread-safe lookup. Returns owned `Option<V>`.
    ///
    /// Optimized: inline keys skip the slab_pool mutex entirely,
    /// avoiding a global lock on the hot read path.
    pub fn get(&self, key: &K) -> Option<V> {
        key.with_key_bytes(|key_bytes| {
            let hr = compute_hash(key_bytes);

            let state = self.inner.read().unwrap();
            let idx = (hr.h1 as usize) & state.bucket_mask;

            let _guard = BucketGuard::new(&state.locks, idx);

            let bucket = unsafe { &mut *state.buckets[idx].get() };

            let mask = bucket.meta.match_mask(hr.h2);
            let mut m = mask;
            while m != 0 {
                let slot_idx = m.trailing_zeros() as u8;
                m &= m - 1;
                let slot = &bucket.slots[slot_idx as usize];

                // Optimization: inline keys (mode=0) don't need the slab lock at all.
                let matched = if slot.get_mode() == 0 {
                    slot.inline_key() == key_bytes
                } else {
                    // Slab mode: check fingerprint first (no lock needed),
                    // only acquire slab lock for the rare full-key comparison.
                    if slot.data[0] & 0x7F != hr.ext_fp_hi {
                        false
                    } else {
                        let mut fp_bytes = [0u8; 4];
                        fp_bytes.copy_from_slice(&slot.data[1..5]);
                        if u32::from_le_bytes(fp_bytes) != hr.ext_fp {
                            false
                        } else {
                            let slab = state.slab_pool.lock().unwrap();
                            slab.get(slot.slab_idx()).key() == key_bytes
                        }
                    }
                };

                if matched {
                    // Check TTL expiry
                    if self.is_expired(&state, idx, slot_idx) {
                        return None;
                    }
                    // Defer LRU/LFU update to access buffer instead of mutating inline.
                    // This keeps the bucket's cache line clean during reads.
                    self.access_buffer.push(idx, slot_idx);
                    let val_bytes = if slot.get_mode() == 0 {
                        slot.inline_value().to_vec()
                    } else {
                        let slab = state.slab_pool.lock().unwrap();
                        slot.get_value(&slab).to_vec()
                    };
                    return V::from_bytes(&val_bytes);
                }
            }
            None
        })
    }

    /// Thread-safe lookup without priority update.
    pub fn peek(&self, key: &K) -> Option<V> {
        key.with_key_bytes(|key_bytes| {
            let hr = compute_hash(key_bytes);

            let state = self.inner.read().unwrap();
            let idx = (hr.h1 as usize) & state.bucket_mask;

            let _guard = BucketGuard::new(&state.locks, idx);

            let bucket = unsafe { &*state.buckets[idx].get() };

            // Use match_mask — same branchless path as get()
            let mask = bucket.meta.match_mask(hr.h2);
            let mut m = mask;
            while m != 0 {
                let slot_idx = m.trailing_zeros() as u8;
                m &= m - 1;
                let slot = &bucket.slots[slot_idx as usize];
                let slab = state.slab_pool.lock().unwrap();
                if slot.matches_key(key_bytes, &hr, &slab) {
                    // Check TTL expiry
                    if self.is_expired(&state, idx, slot_idx) {
                        return None;
                    }
                    let val_bytes = slot.get_value(&slab).to_vec();
                    drop(slab);
                    return V::from_bytes(&val_bytes);
                }
            }
            None
        })
    }

    /// Thread-safe key existence check.
    #[inline]
    pub fn contains_key(&self, key: &K) -> bool {
        self.peek(key).is_some()
    }

    /// Thread-safe removal. Returns true if key was found and removed.
    pub fn remove(&self, key: &K) -> bool {
        key.with_key_bytes(|key_bytes| {
            let hr = compute_hash(key_bytes);

            let state = self.inner.read().unwrap();
            let idx = (hr.h1 as usize) & state.bucket_mask;

            let _guard = BucketGuard::new(&state.locks, idx);

            let bucket = unsafe { &mut *state.buckets[idx].get() };

            // Use match_mask — same branchless path as get()
            let mask = bucket.meta.match_mask(hr.h2);
            let mut m = mask;
            while m != 0 {
                let slot_idx = m.trailing_zeros() as u8;
                m &= m - 1;
                let slot = &bucket.slots[slot_idx as usize];
                if slot.matches_key(key_bytes, &hr, &state.slab_pool.lock().unwrap()) {
                    // Free slab memory on remove
                    if slot.get_mode() == 1 {
                        state.slab_pool.lock().unwrap().free(slot.slab_idx());
                    }
                    bucket.meta.set_state(slot_idx, SlotState::Tombstone);
                    bucket.slots[slot_idx as usize].clear();
                    self.count.fetch_sub(1, Ordering::Relaxed);
                    return true;
                }
            }
            false
        })
    }

    // ═══════════════════════════════════════════════════════════
    // Dynamic Resize
    // ═══════════════════════════════════════════════════════════

    /// Resize the map to a new number of buckets.
    ///
    /// Stop-the-world: acquires exclusive write lock, blocking all concurrent
    /// operations until rehashing completes. This is safe but causes a brief pause.
    ///
    /// `new_num_buckets` is rounded up to the next power of 2.
    pub fn resize(&self, new_num_buckets: usize) {
        let mut new_actual = new_num_buckets.max(1).next_power_of_two();

        // Acquire write lock — blocks ALL reads and writes
        let mut state = self.inner.write().unwrap();

        // Skip if already at target size (another thread may have resized)
        if state.num_buckets >= new_actual {
            return;
        }

        // Collect all live entries with their TTL data before rehashing.
        // This decouples extraction from insertion so we can retry with a
        // larger capacity if bucket collisions cause overflow.
        struct EntryData {
            key_bytes: Vec<u8>,
            val_bytes: Vec<u8>,
            slot_ttl: SlotTTL,
        }

        let old_epochs = state.epochs.lock().unwrap().clone();
        let mut entries: Vec<EntryData> = Vec::new();

        for (bucket_idx, bucket_cell) in state.buckets.iter().enumerate() {
            let bucket = unsafe { &*bucket_cell.get() };
            for slot_idx in 0..4u8 {
                if bucket.meta.get_state(slot_idx) != SlotState::Full {
                    continue;
                }
                let slot = &bucket.slots[slot_idx as usize];

                let slab = state.slab_pool.lock().unwrap();
                let key_bytes = slot.get_key_bytes(&slab).to_vec();
                let val_bytes = slot.get_value_bytes(&slab).to_vec();
                drop(slab);

                if key_bytes.is_empty() {
                    continue;
                }

                // Preserve the original TTL data for this slot
                let ttl_idx = bucket_idx * 4 + slot_idx as usize;
                let slot_ttl = if ttl_idx < old_epochs.len() {
                    old_epochs[ttl_idx]
                } else {
                    SlotTTL::default()
                };

                entries.push(EntryData {
                    key_bytes,
                    val_bytes,
                    slot_ttl,
                });
            }
        }

        // Retry loop: if any entry can't find a free slot, double the
        // capacity and try again. This guarantees zero data loss.
        loop {
            let new_mask = new_actual - 1;
            let new_buckets: Vec<UnsafeCell<Bucket>> = (0..new_actual)
                .map(|_| UnsafeCell::new(Bucket::empty()))
                .collect();
            let new_slab = Mutex::new(SlabPool::new());
            let mut new_epochs = vec![SlotTTL::default(); new_actual * 4];
            let mut new_count = 0usize;
            let mut overflow = false;

            for entry in entries.iter() {
                let hr = compute_hash(&entry.key_bytes);
                let new_idx = (hr.h1 as usize) & new_mask;
                let new_bucket = unsafe { &mut *new_buckets[new_idx].get() };

                if let Some(free) = new_bucket.meta.find_free_slot() {
                    let new_slot = &mut new_bucket.slots[free as usize];
                    if entry.key_bytes.len() <= 6 && entry.val_bytes.len() <= 7 {
                        new_slot.set_inline(&entry.key_bytes, &entry.val_bytes);
                    } else {
                        let mut slab = new_slab.lock().unwrap();
                        let idx = slab.alloc(&entry.key_bytes, &entry.val_bytes);
                        new_slot.set_slab(hr.ext_fp_hi, hr.ext_fp, idx);
                    }
                    new_bucket.meta.set_state(free, SlotState::Full);
                    new_bucket.meta.set_h2(free, hr.h2);
                    new_bucket.meta.on_insert(free);

                    // Migrate TTL data to the new slot position
                    new_epochs[new_idx * 4 + free as usize] = entry.slot_ttl;
                    new_count += 1;
                } else {
                    // Bucket overflow — double capacity and retry
                    overflow = true;
                    break;
                }
            }

            if overflow {
                new_actual *= 2;
                continue;
            }

            // Success — swap state
            state.buckets = new_buckets;
            state.locks = BucketLocks::new(new_actual);
            state.slab_pool = new_slab;
            state.num_buckets = new_actual;
            state.bucket_mask = new_mask;
            *state.epochs.lock().unwrap() = new_epochs;
            self.count.store(new_count, Ordering::Relaxed);
            break;
        }
    }

    // ── Stats ──

    #[inline]
    pub fn len(&self) -> usize {
        self.count.load(Ordering::Relaxed)
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub fn capacity(&self) -> usize {
        let state = self.inner.read().unwrap();
        state.num_buckets * 4
    }

    #[inline]
    pub fn load_factor(&self) -> f64 {
        let cap = self.capacity();
        self.len() as f64 / cap as f64
    }

    #[inline]
    pub fn eviction_count(&self) -> usize {
        self.eviction_count.load(Ordering::Relaxed)
    }

    #[inline]
    pub fn num_buckets(&self) -> usize {
        let state = self.inner.read().unwrap();
        state.num_buckets
    }
}

// ═══════════════════════════════════════════════════════════════
// Display + Debug
// ═══════════════════════════════════════════════════════════════

impl<K: PulseKey, V: PulseValue> std::fmt::Debug for ConcurrentPulseMap<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConcurrentPulseMap")
            .field("len", &self.len())
            .field("capacity", &self.capacity())
            .field(
                "load_factor",
                &format!("{:.1}%", self.load_factor() * 100.0),
            )
            .field("evictions", &self.eviction_count())
            .finish()
    }
}

impl<K: PulseKey, V: PulseValue> std::fmt::Display for ConcurrentPulseMap<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ConcurrentPulseMap({}/{} entries, {:.1}% load, {} evictions)",
            self.len(),
            self.capacity(),
            self.load_factor() * 100.0,
            self.eviction_count()
        )
    }
}