fastskip 0.1.1

Lock-free arena-backed skip list memtable for LSM-tree storage engines
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
//! Node memory layout and atomic tower operations.
//!
//! Each skip list node is a fixed-layout struct allocated from the arena:
//!
//! ```text
//! Offset  Size   Field
//! 0       4      key_offset   (u32) — offset of key bytes from node start
//! 4       4      key_len      (u32)
//! 8       4      value_offset (u32) — offset of value bytes from node start
//! 12      4      value_len    (u32)
//! 16      1      flags        (u8)  — bit 0 = tombstone
//! 17      1      height       (u8)  — tower height (1..MAX_HEIGHT)
//! 18      6      _pad         (zero-filled)
//! 24      8      seq          (u64) — insertion sequence number
//! 32      8*H    tower[0..H]  — each is AtomicU64 storing a TowerPtr
//! 32+8*H         key bytes
//! ...            value bytes
//! ```
//!
//! Key and value bytes are stored inline after the tower. Zero-copy
//! accessors read directly from the arena memory without deserialization.

use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};

/// Maximum skip list tower height. Limits the number of levels to 20,
/// which supports lists up to ~2^20 (1M) nodes with high probability.
pub(crate) const MAX_HEIGHT: usize = 20;

/// Size of the node header in bytes (before the tower array).
pub(crate) const NODE_HEADER_SIZE: usize = 32;

/// Bitmask for the tombstone flag in the node's `flags` byte.
pub(crate) const TOMBSTONE_BIT: u8 = 0x01;

/// Byte offset of the `flags` field within the node header.
pub(crate) const FLAGS_OFFSET: usize = 16;

/// Byte offset of the `seq` field within the node header.
pub(crate) const NODE_SEQ_OFFSET: usize = 24;

// ─── TowerPtr ──────────────────────────────────────────────────────────────────

/// Opaque packed pointer stored in the skip list tower as `AtomicU64`.
///
/// The lower 48 bits hold the pointer value (sufficient for all current
/// architectures). A raw value of `0` encodes a null pointer. Upper bits
/// are masked off on read for portability across platforms.
///
/// `TowerPtr` is `#[repr(transparent)]` around `u64`, making it safe to
/// store and load via `AtomicU64` operations.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct TowerPtr(u64);

impl TowerPtr {
    /// The null tower pointer (raw value `0`).
    pub const NULL: Self = Self(0);

    /// Pack a pointer into a `TowerPtr`.
    #[inline]
    pub fn new(ptr: *const u8) -> Self {
        Self(ptr as usize as u64)
    }

    /// Construct from a raw `u64` value (e.g., loaded from `AtomicU64`).
    #[inline]
    pub fn from_raw(raw: u64) -> Self {
        Self(raw)
    }

    /// Get the raw `u64` representation for atomic store/CAS.
    #[inline]
    pub fn raw(self) -> u64 {
        self.0
    }

    /// Unpack the pointer, masking off upper bits for portability.
    #[inline]
    pub fn ptr(self) -> *const u8 {
        (self.0 & 0x0000_FFFF_FFFF_FFFF) as usize as *const u8
    }

    /// Returns `true` if this is a null pointer.
    #[inline]
    pub fn is_null(self) -> bool {
        self.0 == 0
    }
}

// ─── Node layout ───────────────────────────────────────────────────────────────

/// Compute the total allocation size for a node with the given tower height,
/// key length, and value length.
///
/// Returns `NODE_HEADER_SIZE + height * 8 + key_len + value_len`.
#[inline]
pub(crate) fn node_alloc_size(height: usize, key_len: usize, value_len: usize) -> usize {
    NODE_HEADER_SIZE + height * 8 + key_len + value_len
}

/// Compute allocation size using expected height (≈2) for quick estimation.
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn node_alloc_size_expected(key_len: usize, value_len: usize) -> usize {
    NODE_HEADER_SIZE + 16 + key_len + value_len
}

/// Initialize a node at `ptr`. Node is NOT yet visible (no CAS has occurred).
/// Header and key/value use plain stores. Tower uses relaxed atomic stores
/// to avoid UB with concurrent `tower_load` (Acquire) after the node becomes
/// visible via CAS.
///
/// # Safety
/// `ptr` must point to at least `node_alloc_size(height, key.len(), value.len())`
/// bytes of valid, writable memory from the arena.
#[inline(always)]
pub(crate) unsafe fn init_node(
    ptr: *mut u8,
    height: usize,
    key: &[u8],
    value: &[u8],
    is_tombstone: bool,
    seq: u64,
) {
    let key_len = key.len() as u32;
    let value_len = value.len() as u32;
    let h = height as u8;

    let key_offset = (NODE_HEADER_SIZE + height * 8) as u32;
    let value_offset = key_offset + key_len;
    let flags: u8 = if is_tombstone { TOMBSTONE_BIT } else { 0 };

    // Bulk write header as u64 chunks for fewer stores
    // Layout: [key_offset:u32, key_len:u32] [value_offset:u32, value_len:u32] [flags:u8, h:u8, pad:6] [seq:u64]
    let header0 = (key_len as u64) << 32 | key_offset as u64;
    let header1 = (value_len as u64) << 32 | value_offset as u64;
    let header2 = (h as u64) << 8 | flags as u64;

    ptr.cast::<u64>().write(header0);
    ptr.add(8).cast::<u64>().write(header1);
    ptr.add(16).cast::<u64>().write(header2);
    // Offset 24-31: seq as u64
    ptr.add(NODE_SEQ_OFFSET).cast::<u64>().write(seq);

    // Tower — bulk zero all tower slots
    std::ptr::write_bytes(ptr.add(NODE_HEADER_SIZE).cast::<u64>(), 0, height);

    // Key/value — plain stores (node not yet visible)
    std::ptr::copy_nonoverlapping(key.as_ptr(), ptr.add(key_offset as usize), key.len());
    std::ptr::copy_nonoverlapping(value.as_ptr(), ptr.add(value_offset as usize), value.len());
}

// ─── Zero-copy accessors ───────────────────────────────────────────────────────

/// Read the key bytes from a node (zero-copy).
///
/// Uses a single 64-bit read to fetch both `key_offset` and `key_len`,
/// then constructs a slice directly from arena memory.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node. The returned slice is
/// valid for the lifetime of the arena (tracked by the `'static` bound
/// on the raw pointer; actual lifetime is managed by the owning struct).
#[inline]
pub(crate) unsafe fn node_key(ptr: *const u8) -> &'static [u8] {
    // Single 64-bit read: [key_offset:u32, key_len:u32]
    let packed = ptr.cast::<u64>().read();
    let key_offset = packed as u32 as usize;
    let key_len = (packed >> 32) as usize;
    std::slice::from_raw_parts(ptr.add(key_offset), key_len)
}

/// Read the value bytes from a node (zero-copy).
///
/// Uses a single 64-bit read to fetch both `value_offset` and `value_len`,
/// then constructs a slice directly from arena memory.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node.
#[inline]
pub(crate) unsafe fn node_value(ptr: *const u8) -> &'static [u8] {
    // Single 64-bit read: [value_offset:u32, value_len:u32]
    let packed = ptr.add(8).cast::<u64>().read();
    let value_offset = packed as u32 as usize;
    let value_len = (packed >> 32) as usize;
    std::slice::from_raw_parts(ptr.add(value_offset), value_len)
}

/// Check if a node has the tombstone flag set.
///
/// Uses an Acquire load to synchronize with the Release CAS in
/// [`set_tombstone`], ensuring the caller sees any prior tombstone.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node.
#[inline]
pub(crate) unsafe fn is_tombstone(ptr: *const u8) -> bool {
    let atomic = &*ptr.add(FLAGS_OFFSET).cast::<AtomicU8>();
    (atomic.load(Ordering::Acquire) & TOMBSTONE_BIT) != 0
}

/// Atomically set the tombstone flag via CAS.
///
/// Returns `true` if this call set the flag (won the CAS). Returns `false`
/// if the flag was already set by a concurrent delete. Uses Release ordering
/// on success to publish the tombstone, and Acquire on failure to retry.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node.
#[inline]
pub(crate) unsafe fn set_tombstone(ptr: *const u8) -> bool {
    let atomic = &*ptr.add(FLAGS_OFFSET).cast::<AtomicU8>();
    let mut current = atomic.load(Ordering::Acquire);
    loop {
        if current & TOMBSTONE_BIT != 0 {
            return false;
        }
        match atomic.compare_exchange_weak(
            current,
            current | TOMBSTONE_BIT,
            Ordering::Release,
            Ordering::Acquire,
        ) {
            Ok(_) => return true,
            Err(new) => current = new,
        }
    }
}

/// Read the tower height of a node.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node.
#[inline]
#[allow(dead_code)]
pub(crate) unsafe fn node_height(ptr: *const u8) -> usize {
    ptr.add(17).read() as usize
}

/// Read the insertion sequence number from a node.
///
/// The sequence number was written before the Release fence/CAS that
/// published the node. Since the caller obtained `ptr` via [`tower_load`]
/// (Acquire), which synchronizes with the publishing Release, a Relaxed
/// load is sufficient here.
///
/// # Safety
///
/// `ptr` must point to a fully initialized node.
#[inline]
pub(crate) unsafe fn node_seq(ptr: *const u8) -> u64 {
    let atomic = &*ptr.add(NODE_SEQ_OFFSET).cast::<AtomicU64>();
    atomic.load(Ordering::Relaxed)
}

// ─── Tower atomic operations ───────────────────────────────────────────────────
//
// All tower entries are AtomicU64. We never mix plain and atomic accesses
// to the same location (Miri catches this).
//
// Load:  Acquire  — sees node contents written before Release CAS
// Store: Relaxed  — used during init and upper-level splice (non-publishing)
// CAS:   Release/Acquire — publishes pointer, sees latest on failure

/// Load a tower pointer at `level` with Acquire ordering.
///
/// Acquire ordering ensures that all node contents (key, value, flags)
/// written before the Release CAS that published this pointer are visible
/// to the reader.
///
/// # Safety
///
/// `node` must be a valid, fully initialized node. `level` must be `<`
/// the node's tower height.
#[inline]
pub(crate) unsafe fn tower_load(node: *const u8, level: usize) -> TowerPtr {
    let offset = NODE_HEADER_SIZE + level * 8;
    let atomic = node.add(offset).cast::<AtomicU64>();
    TowerPtr::from_raw((*atomic).load(Ordering::Acquire))
}

/// Store a tower pointer at `level` with Relaxed ordering.
///
/// Used during node initialization (before the node is published) and
/// during upper-level splice (where the pointer is an optimization hint,
/// not the publishing mechanism). Relaxed is sufficient because the
/// Release fence + CAS at level 0 is the actual publication barrier.
///
/// # Safety
///
/// `node` must be a valid node. `level` must be `<` the node's tower height.
#[inline]
pub(crate) unsafe fn tower_store(node: *mut u8, level: usize, val: TowerPtr) {
    let offset = NODE_HEADER_SIZE + level * 8;
    let atomic = node.add(offset).cast::<AtomicU64>();
    (*atomic).store(val.raw(), Ordering::Relaxed);
}

/// Compare-and-swap a tower pointer at `level`.
///
/// Uses Release ordering on success (publishes the new pointer and all
/// preceding writes) and Acquire on failure (sees the latest value for
/// retry). This is the core atomic operation that commits inserts.
///
/// # Safety
///
/// `node` must be a valid node. `level` must be `<` the node's tower height.
#[inline]
pub(crate) unsafe fn tower_cas(
    node: *const u8,
    level: usize,
    current: TowerPtr,
    new: TowerPtr,
) -> Result<TowerPtr, TowerPtr> {
    let offset = NODE_HEADER_SIZE + level * 8;
    let atomic = node.add(offset).cast::<AtomicU64>();
    match (*atomic).compare_exchange_weak(
        current.raw(),
        new.raw(),
        Ordering::Release,
        Ordering::Acquire,
    ) {
        Ok(v) => Ok(TowerPtr::from_raw(v)),
        Err(v) => Err(TowerPtr::from_raw(v)),
    }
}

// ─── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn alloc_node(height: usize, key: &[u8], value: &[u8]) -> (*mut u8, std::alloc::Layout) {
        let size = node_alloc_size(height, key.len(), value.len());
        let layout = std::alloc::Layout::from_size_align(size, 8).unwrap();
        let ptr = unsafe { std::alloc::alloc(layout) };
        assert!(!ptr.is_null());
        (ptr, layout)
    }

    #[test]
    fn test_node_alloc_size() {
        assert_eq!(
            node_alloc_size(4, 10, 20),
            NODE_HEADER_SIZE + 4 * 8 + 10 + 20
        );
    }

    #[test]
    fn test_init_and_read_roundtrip() {
        let key = b"hello";
        let value = b"world";
        let (ptr, layout) = alloc_node(3, key, value);
        unsafe {
            init_node(ptr, 3, key, value, false, 42);
            assert_eq!(node_key(ptr), key);
            assert_eq!(node_value(ptr), value);
            assert!(!is_tombstone(ptr));
            assert_eq!(node_seq(ptr), 42);
            for i in 0..3 {
                assert!(tower_load(ptr, i).is_null());
            }
            std::alloc::dealloc(ptr, layout);
        }
    }

    #[test]
    fn test_tombstone_flag() {
        let (ptr, layout) = alloc_node(1, b"key", b"val");
        unsafe {
            init_node(ptr, 1, b"key", b"val", false, 1);
            assert!(!is_tombstone(ptr));
            assert!(set_tombstone(ptr));
            assert!(is_tombstone(ptr));
            assert!(!set_tombstone(ptr));
            std::alloc::dealloc(ptr, layout);
        }
    }

    #[test]
    #[cfg(not(miri))] // CAS assertion flaky on some Miri versions (passes locally)
    fn test_tower_cas() {
        let (ptr, layout) = alloc_node(4, b"", b"");
        unsafe {
            init_node(ptr, 4, b"", b"", false, 1);

            let null = TowerPtr::NULL;
            let node_ptr = TowerPtr::new(ptr);

            assert_eq!(tower_cas(ptr, 0, null, node_ptr), Ok(null));
            assert_eq!(tower_load(ptr, 0), node_ptr);
            assert!(tower_cas(ptr, 0, null, node_ptr).is_err());

            std::alloc::dealloc(ptr, layout);
        }
    }

    #[test]
    fn test_tower_ptr_basic() {
        // Use a real allocation to avoid integer-to-pointer casts under Miri
        let dummy = Box::into_raw(Box::new(42u8));
        let p = TowerPtr::new(dummy);
        assert!(!p.is_null());
        assert_eq!(p.ptr(), dummy);

        assert!(TowerPtr::NULL.is_null());
        assert_eq!(TowerPtr::NULL.ptr(), std::ptr::null());

        unsafe { drop(Box::from_raw(dummy)) };
    }

    #[test]
    fn test_tower_store_atomic() {
        let (ptr, layout) = alloc_node(2, b"", b"");
        unsafe {
            init_node(ptr, 2, b"", b"", false, 1);

            // Use a real allocation to avoid integer-to-pointer casts under Miri
            let dummy = Box::into_raw(Box::new(99u8));
            let target = TowerPtr::new(dummy);
            tower_store(ptr, 0, target);
            assert_eq!(tower_load(ptr, 0), target);

            // Verify via atomic load directly (same path as tower_load)
            let offset = NODE_HEADER_SIZE;
            let atomic = ptr.add(offset).cast::<AtomicU64>();
            assert_eq!((*atomic).load(Ordering::Acquire), target.raw());

            drop(Box::from_raw(dummy));
            std::alloc::dealloc(ptr, layout);
        }
    }
}