masstree 0.9.5

A high-performance concurrent ordered map (trie of B+trees)
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
//! Shared helpers for forward and reverse batch processing.

use std::cmp::Ordering;

use crate::hints::unlikely;
use crate::key::IKEY_SIZE;
use crate::leaf_trait::TreeLeafNode;
use crate::leaf15::KSUF_KEYLENX;
use crate::leaf15::LAYER_KEYLENX;
use crate::leaf15::LeafNode15;
use crate::ordering::READ_ORD;
use crate::policy::LeafPolicy;
use crate::policy::atomic_read_value;
use crate::prefetch::prefetch_read;

use super::cursor_key::CursorKey;
use super::find::LeafBatchResult;
use super::find_rev::LeafBatchResultBack;
use super::iterator::RangeBound;
use super::iterator::iter_flags::ReverseFlags;
use super::scan_state::{LayerContext, LayerStack, ScanSnapshot, ScanSnapshotPtr};

/// Build the full key in `cursor_key` from slot data.
#[inline(always)]
pub fn build_slot_key<P: LeafPolicy>(
    cursor_key: &mut CursorKey,
    leaf: &LeafNode15<P>,
    slot: usize,
    slot_ikey: u64,
    slot_keylenx: u8,
) {
    cursor_key.assign_store_ikey(slot_ikey);

    if slot_keylenx == KSUF_KEYLENX {
        if let Some(suffix) = leaf.ksuf(slot) {
            let suffix_len = suffix.len();
            let _ = cursor_key.assign_store_suffix(suffix);
            cursor_key.assign_store_length(IKEY_SIZE + suffix_len);
        } else {
            cursor_key.assign_store_length(IKEY_SIZE);
        }
    } else {
        let len = slot_keylenx as usize;
        cursor_key.assign_store_length(len);
    }
}

/// Visitor for batch slot processing.
pub trait SlotVisitor<P: LeafPolicy> {
    /// Visit a slot after key has been built in `cursor_key`.
    ///
    /// # Returns
    ///
    /// - `None`: Value was concurrently removed
    /// - `Some(true)`: Continue scanning
    /// - `Some(false)`: Visitor requested stop
    fn visit(&mut self, leaf: &LeafNode15<P>, slot: usize, key: &[u8]) -> Option<bool>;
}

/// Ref visitor: loads `&P::Value` via pointer dereference.
///
/// For use with `RefLeafPolicy` types where values are pointer-backed (Arc, Box).
pub struct RefSlotVisitor<F>(pub F);

impl<P, F> SlotVisitor<P> for RefSlotVisitor<F>
where
    P: LeafPolicy,
    F: FnMut(&[u8], &P::Value) -> bool,
{
    #[inline(always)]
    fn visit(&mut self, leaf: &LeafNode15<P>, slot: usize, key: &[u8]) -> Option<bool> {
        if P::CAN_WRITE_THROUGH {
            // Atomic read avoids aliasing violation with concurrent
            // write_through_update. The copy lives on the stack, so the
            // callback's &V is to owned data, not the mutable Box allocation.
            // Relaxed pointer load: the subsequent atomic_read_value(Acquire)
            // provides synchronization for the value contents. The pointer
            // itself is stable under write-through (Box is never swapped).
            let raw: *mut u8 = leaf.load_value_raw_relaxed(slot);

            if raw.is_null() {
                return None;
            }

            // SAFETY: CAN_WRITE_THROUGH guarantees size 1/2/4/8 with natural
            // alignment. Guard protects the allocation from retirement.
            let v: P::Value = unsafe { atomic_read_value::<P::Value>(raw, READ_ORD) };

            Some((self.0)(key, &v))
        } else {
            // SAFETY: Guard protects value, slot is valid (in permutation).
            // Null-check handles TOCTOU race.
            let ptr: *const P::Value = unsafe { leaf.load_value_ptr(slot) };

            if ptr.is_null() {
                return None;
            }

            // SAFETY: Non-null pointer to a valid P::Value, protected by OCC
            // guard. No concurrent modification (non-write-through types
            // allocate a new Box on update).
            let value_ref: &P::Value = unsafe { &*ptr };

            Some((self.0)(key, value_ref))
        }
    }
}

/// Copy visitor: loads `P::Output` via `load_value()` (universal).
///
/// Works for all `LeafPolicy` types including true-inline storage.
pub struct CopySlotVisitor<F>(pub F);

impl<P, F> SlotVisitor<P> for CopySlotVisitor<F>
where
    P: LeafPolicy,
    F: FnMut(&[u8], P::Output) -> bool,
{
    #[inline(always)]
    fn visit(&mut self, leaf: &LeafNode15<P>, slot: usize, key: &[u8]) -> Option<bool> {
        // Use load_value to handle TOCTOU race.
        let output: P::Output = leaf.load_value(slot)?;

        Some((self.0)(key, output))
    }
}

/// Abstracts clone vs zero-copy value emission for per-entry scanning.
pub trait ScanEmitter<P: LeafPolicy> {
    type Snapshot;

    /// Load a value from `leaf[slot]` and wrap it with `key_len`.
    fn emit_value(leaf: &LeafNode15<P>, slot: usize, key_len: usize) -> Option<Self::Snapshot>;
}

/// Clone-based emitter: calls `leaf.load_value(slot)`, returns `ScanSnapshot<P>`.
pub struct CloneEmitter;

impl<P: LeafPolicy> ScanEmitter<P> for CloneEmitter
where
    P::Output: Clone,
{
    type Snapshot = ScanSnapshot<P>;

    #[inline(always)]
    fn emit_value(leaf: &LeafNode15<P>, slot: usize, key_len: usize) -> Option<ScanSnapshot<P>> {
        let output = leaf.load_value(slot)?;
        Some(ScanSnapshot {
            value: output,
            key_len,
        })
    }
}

/// Zero-copy emitter: calls `leaf.load_value_raw(slot)`, returns `ScanSnapshotPtr<P::Value>`.
///
/// For `CAN_WRITE_THROUGH` types, callers must use `ScanSnapshotPtr::value_copy`
/// (atomic read) instead of `value_ref` to avoid aliasing violations.
pub struct PtrEmitter;

impl<P: LeafPolicy> ScanEmitter<P> for PtrEmitter {
    type Snapshot = ScanSnapshotPtr<P::Value>;

    #[inline(always)]
    fn emit_value(
        leaf: &LeafNode15<P>,
        slot: usize,
        key_len: usize,
    ) -> Option<ScanSnapshotPtr<P::Value>> {
        let ptr = leaf.load_value_raw(slot);
        if ptr.is_null() {
            return None;
        }
        Some(ScanSnapshotPtr::from_raw(ptr, key_len))
    }
}

/// Direction-specific operations for batch leaf processing.
///
/// Implemented by zero-sized marker types [`Forward`] and [`Backward`].
/// All methods are trivially inlineable, so monomorphization produces
/// identical code to hand-written direction-specific loops.
pub trait BatchDirection: Sized {
    /// Result type for batch processing.
    type Result: Copy;

    /// Direction-specific flags type. `()` for forward, [`ReverseFlags`] for backward.
    type Flags: ?Sized;

    /// Advance position by one step in this direction.
    fn step(ki: isize) -> isize;

    /// Position to store when a layer pointer is encountered.
    fn layer_ki(ki: isize) -> isize;

    /// Prefetch neighbor slot's value to hide memory latency.
    /// Forward: no-op. Backward: prefetches previous slot.
    fn prefetch_neighbor<P: LeafPolicy>(
        ki: isize,
        leaf: &LeafNode15<P>,
        perm: &<LeafNode15<P> as TreeLeafNode<P>>::Perm,
    );

    /// The [`Ordering`] that indicates the bound has been exceeded.
    /// Forward: `Greater` (`slot_ikey` > `end_bound`). Backward: `Less` (`slot_ikey` < `start_bound`).
    fn exceeded_ordering() -> Ordering;

    /// Full key bound check.
    /// Forward: `bound.contains(key)`. Backward: `bound.contains_reverse(key)`.
    fn key_in_bound(bound: &RangeBound<'_>, key: &[u8]) -> bool;

    /// Called before emitting a value. Forward: no-op. Backward: clears `upper_bound` flag.
    fn on_pre_emit(flags: &mut Self::Flags);

    // Result constructors
    fn exhausted() -> Self::Result;
    fn layer_encountered() -> Self::Result;
    fn version_changed() -> Self::Result;
    fn stopped() -> Self::Result;
    fn bound_exceeded() -> Self::Result;
}

/// Forward direction marker for batch processing.
pub struct Forward;

impl BatchDirection for Forward {
    type Result = LeafBatchResult;
    type Flags = ();

    #[inline(always)]
    fn step(ki: isize) -> isize {
        ki + 1
    }

    #[inline(always)]
    fn layer_ki(ki: isize) -> isize {
        ki
    }

    #[inline(always)]
    fn prefetch_neighbor<P: LeafPolicy>(
        ki: isize,
        leaf: &LeafNode15<P>,
        perm: &<LeafNode15<P> as TreeLeafNode<P>>::Perm,
    ) {
        let next_ki: usize = (ki + 1).cast_unsigned();
        if next_ki < perm.size() {
            let next_slot: usize = perm.get(next_ki);
            leaf.prefetch_value(next_slot);
        }
    }

    #[inline(always)]
    fn exceeded_ordering() -> Ordering {
        Ordering::Greater
    }

    #[inline(always)]
    fn key_in_bound(bound: &RangeBound<'_>, key: &[u8]) -> bool {
        bound.contains(key)
    }

    #[inline(always)]
    fn on_pre_emit(_flags: &mut ()) {}

    #[inline(always)]
    fn exhausted() -> LeafBatchResult {
        LeafBatchResult::LeafExhausted
    }

    #[inline(always)]
    fn layer_encountered() -> LeafBatchResult {
        LeafBatchResult::LayerEncountered
    }

    #[inline(always)]
    fn version_changed() -> LeafBatchResult {
        LeafBatchResult::VersionChanged
    }

    #[inline(always)]
    fn stopped() -> LeafBatchResult {
        LeafBatchResult::Stopped
    }

    #[inline(always)]
    fn bound_exceeded() -> LeafBatchResult {
        LeafBatchResult::EndBoundExceeded
    }
}

/// Backward direction marker for batch processing.
pub struct Backward;

impl BatchDirection for Backward {
    type Result = LeafBatchResultBack;
    type Flags = ReverseFlags;

    #[inline(always)]
    fn step(ki: isize) -> isize {
        ki - 1
    }

    #[inline(always)]
    fn layer_ki(ki: isize) -> isize {
        ki - 1
    }

    #[inline(always)]
    fn prefetch_neighbor<P: LeafPolicy>(
        ki: isize,
        leaf: &LeafNode15<P>,
        perm: &<LeafNode15<P> as TreeLeafNode<P>>::Perm,
    ) {
        if ki > 0 {
            let prev_slot: usize = perm.get((ki - 1).cast_unsigned());
            leaf.prefetch_value(prev_slot);
        }
    }

    #[inline(always)]
    fn exceeded_ordering() -> Ordering {
        Ordering::Less
    }

    #[inline(always)]
    fn key_in_bound(bound: &RangeBound<'_>, key: &[u8]) -> bool {
        bound.contains_reverse(key)
    }

    #[inline(always)]
    fn on_pre_emit(flags: &mut ReverseFlags) {
        flags.clear_upper_bound();
    }

    #[inline(always)]
    fn exhausted() -> LeafBatchResultBack {
        LeafBatchResultBack::LeafExhausted
    }

    #[inline(always)]
    fn layer_encountered() -> LeafBatchResultBack {
        LeafBatchResultBack::LayerEncountered
    }

    #[inline(always)]
    fn version_changed() -> LeafBatchResultBack {
        LeafBatchResultBack::VersionChanged
    }

    #[inline(always)]
    fn stopped() -> LeafBatchResultBack {
        LeafBatchResultBack::Stopped
    }

    #[inline(always)]
    fn bound_exceeded() -> LeafBatchResultBack {
        LeafBatchResultBack::StartBoundExceeded
    }
}

/// Extracted batch processing state, direction-agnostic.
///
/// Callers build this from their direction-specific stack type, call the
/// unified batch function, then write back `ki` and `root`.
pub struct BatchCtx<'a, P: LeafPolicy> {
    pub leaf: &'a LeafNode15<P>,
    pub perm: <LeafNode15<P> as TreeLeafNode<P>>::Perm,
    pub perm_size: usize,
    pub cached_version: u32,
    pub ki: isize,
    pub root: *const u8,
    pub leaf_ptr: *mut LeafNode15<P>,
    pub cursor_key: &'a mut CursorKey,
    pub layer_stack: &'a mut LayerStack<P>,
}

/// Direction-parameterized keyed batch processor.
#[inline]
pub fn process_batch_keyed<D, V, P>(
    ctx: &mut BatchCtx<'_, P>,
    bound: &RangeBound<'_>,
    bound_ikey: Option<u64>,
    slot_visitor: &mut V,
    count: &mut usize,
    flags: &mut D::Flags,
) -> D::Result
where
    D: BatchDirection,
    V: SlotVisitor<P>,
    P: LeafPolicy,
{
    if ctx.leaf.version().is_deleted() {
        return D::version_changed();
    }

    while ctx.ki >= 0 && ctx.ki.unsigned_abs() < ctx.perm_size {
        let slot: usize = ctx.perm.get(ctx.ki.unsigned_abs());
        let slot_ikey: u64 = ctx.leaf.ikey_relaxed(slot);
        let slot_keylenx: u8 = ctx.leaf.keylenx_relaxed(slot);

        D::prefetch_neighbor::<P>(ctx.ki, ctx.leaf, &ctx.perm);

        // Layer pointer check
        if unlikely(slot_keylenx >= LAYER_KEYLENX) {
            let layer_ptr: *mut u8 = ctx.leaf.load_layer_raw(slot);
            ctx.layer_stack
                .push(LayerContext::new(ctx.root, ctx.leaf_ptr));
            ctx.cursor_key.assign_store_ikey(slot_ikey);
            prefetch_read(layer_ptr);
            ctx.root = layer_ptr.cast_const();
            ctx.ki = D::layer_ki(ctx.ki);
            return D::layer_encountered();
        }

        // Empty value check
        if unlikely(ctx.leaf.is_value_empty_relaxed(slot)) {
            ctx.ki = D::step(ctx.ki);
            continue;
        }

        // Fast bound pre-check using ikey
        let mut needs_full_bound_check = true;

        if bound_ikey.is_none() {
            needs_full_bound_check = false;
        } else if ctx.cursor_key.is_at_root_layer()
            && let Some(b_ikey) = bound_ikey
        {
            match slot_ikey.cmp(&b_ikey) {
                ord if ord == D::exceeded_ordering() => return D::bound_exceeded(),

                Ordering::Equal => {}
                _ => needs_full_bound_check = false,
            }
        }

        // Build key
        build_slot_key(ctx.cursor_key, ctx.leaf, slot, slot_ikey, slot_keylenx);
        ctx.cursor_key.mark_key_complete();

        D::on_pre_emit(flags);

        // Full key bound check
        let key: &[u8] = ctx.cursor_key.full_key();
        if needs_full_bound_check && !D::key_in_bound(bound, key) {
            return D::bound_exceeded();
        }

        // Visit slot
        match slot_visitor.visit(ctx.leaf, slot, key) {
            None => {
                ctx.ki = D::step(ctx.ki);
            }

            Some(should_continue) => {
                *count += 1;
                ctx.ki = D::step(ctx.ki);

                if !should_continue {
                    return D::stopped();
                }
            }
        }
    }

    if ctx.leaf.version().has_changed(ctx.cached_version) {
        return D::version_changed();
    }

    D::exhausted()
}

// ============================================================================
//  Unified Values-Only Batch Processor
// ============================================================================

/// Direction-parameterized values-only batch processor (no key materialization).
#[inline]
pub fn process_batch_values<D, P>(
    ctx: &mut BatchCtx<'_, P>,
    bound_ikey: Option<u64>,
    visitor: &mut impl FnMut(P::Output) -> bool,
    count: &mut usize,
    flags: &mut D::Flags,
) -> D::Result
where
    D: BatchDirection,
    P: LeafPolicy,
{
    if ctx.leaf.version().is_deleted() {
        return D::version_changed();
    }

    while ctx.ki >= 0 && ctx.ki.unsigned_abs() < ctx.perm_size {
        let slot: usize = ctx.perm.get(ctx.ki.unsigned_abs());
        let slot_ikey: u64 = ctx.leaf.ikey_relaxed(slot);
        let slot_keylenx: u8 = ctx.leaf.keylenx_relaxed(slot);

        D::prefetch_neighbor::<P>(ctx.ki, ctx.leaf, &ctx.perm);

        // Layer pointer check
        if unlikely(slot_keylenx >= LAYER_KEYLENX) {
            let layer_ptr: *mut u8 = ctx.leaf.load_layer_raw(slot);
            ctx.layer_stack
                .push(LayerContext::new(ctx.root, ctx.leaf_ptr));
            ctx.cursor_key.assign_store_ikey(slot_ikey);
            prefetch_read(layer_ptr);
            ctx.root = layer_ptr.cast_const();
            ctx.ki = D::layer_ki(ctx.ki);
            return D::layer_encountered();
        }

        // Fast bound check (ikey only)
        if let Some(b_ikey) = bound_ikey
            && slot_ikey.cmp(&b_ikey) == D::exceeded_ordering()
        {
            return D::bound_exceeded();
        }

        // Empty value check
        if unlikely(ctx.leaf.is_value_empty_relaxed(slot)) {
            ctx.ki = D::step(ctx.ki);
            continue;
        }

        D::on_pre_emit(flags);

        let Some(output) = ctx.leaf.load_value(slot) else {
            ctx.ki = D::step(ctx.ki);
            continue;
        };

        *count += 1;
        ctx.ki = D::step(ctx.ki);

        if !visitor(output) {
            return D::stopped();
        }
    }

    if ctx.leaf.version().has_changed(ctx.cached_version) {
        return D::version_changed();
    }

    D::exhausted()
}