artmap 0.8.0

A concurrent in-memory Adaptive Radix Tree (ART) featuring optimistic lock coupling.
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
// Copyright (c) 2026 SurrealDB Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Removes, and unlinking the nodes they empty (§9.4, §13)
//!
//! A remove unlinks one leaf from its node `N`. If that leaves `N` empty, the
//! same critical section unlinks `N` from its parent `P` too; otherwise the
//! remove locks only `N`, as before. A node left with a single entry stays as
//! it is: collapsing it would make every remove and re-insert of a key beside
//! one other key free a node and build a new one.
//! [`shrink_to_fit`](RawTree::shrink_to_fit) fits such nodes on demand.
//!
//! **Protocol** (Inv 7). `P` (or `root_latch`) is taken with a blocking
//! `lock()` while holding nothing, then re-checked to point at `N`; `N` with
//! `try_upgrade(v_N)`, restarting on failure. That is the grow and split
//! order, so it cannot deadlock. The commit removes `N` from `P` and marks `N`
//! obsolete before `P` is released. `N`'s slots are left as they are: every
//! reader or writer still inside `N` fails its next validation or upgrade, and
//! `N` is retired once `P` is released.
//!
//! Unlinking allocates nothing, so a remove never fails, and the arena maps
//! unlink empty nodes too (their bytes stay in the arena, as a replaced
//! leaf's do).
//!
//! **Follow-up** (§13 3a). If unlinking `N` leaves `P` empty, the remover
//! releases every latch and unlinks `P` as a new top-down operation, then
//! `P`'s parent if that leaves it empty, and so on up, holding at most two
//! latches at a time. A prefix chain of single-child `Node4`s goes with the
//! last key below it.
//!
//! An empty node is a state that readers, writers and cursors already handle,
//! so no reader depends on this for correctness. At quiescence,
//! [`validate`](RawTree::validate) checks that it completed.

#![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)]

use std::ptr::NonNull;

use crate::latch::{AbortOnUnwind, SpinBackoff, WriteGuard};
use crate::raw::node::{NodeHeader, Taken};
use crate::raw::slot::{AtomicSlot, Slot};
use crate::raw::write::{Parent, ParentGuard};
use crate::raw::{LeafNode, Raw, RawTree, Storage};

/// The result of an emptying remove's latched section.
enum Unlink {
    /// A failed acquisition. Nothing was written; retry from the root.
    Retry,
    /// `confirm` refused the leaf. Nothing was written.
    Refused,
    /// Done. `true` if the commit left the parent empty, so it needs a
    /// follow-up.
    Done(bool),
}

/// Takes the removed leaf out of a node that stays linked. Caller holds the
/// node's latch.
fn take_out<A: AtomicSlot>(node: &NodeHeader<A>, w: &WriteGuard<'_>, taken: Taken) {
    match taken {
        Taken::Exact => node.set_exact_leaf(w, A::Raw::NULL),
        Taken::Child(byte) => node.remove_child(w, byte),
        Taken::Nothing => {}
    }
}

impl<S: Storage> RawTree<S> {
    /// Removes the leaf for `key`, if `matches` accepts it, and unlinks the
    /// nodes that leaves empty (§13). Returns the leaf unlinked, marked
    /// removed and already retired (§9.4).
    ///
    /// `matches` runs before any latch: it compares keys (user code) for
    /// `remove`, or pointer identity for `remove_leaf`.
    pub(crate) fn remove(
        &self,
        key: &[u8],
        matches: impl Fn(NonNull<S::Leaf>) -> bool,
        guard: &S::Guard,
    ) -> Option<NonNull<S::Leaf>> {
        self.remove_confirmed(key, matches, |_| Some(1), guard)
    }

    /// As [`remove`](Self::remove), with a last check under the latches.
    ///
    /// `confirm` runs once the latches that the unlink needs are held, just
    /// before the commit. It returns the `len` the leaf accounts for (1 for a
    /// plain leaf; for a versioned leaf, whether its newest version is live),
    /// or `None` to leave the leaf in place, and the remove then returns
    /// `None`. Once it returns `Some`, the leaf is unlinked. It runs under
    /// ART latches, so it must not run user code, allocate from an arena, pin,
    /// or take any latch but a terminal `chain_latch` (Inv 6, Inv 7).
    pub(crate) fn remove_confirmed(
        &self,
        key: &[u8],
        matches: impl Fn(NonNull<S::Leaf>) -> bool,
        confirm: impl Fn(NonNull<S::Leaf>) -> Option<isize>,
        guard: &S::Guard,
    ) -> Option<NonNull<S::Leaf>> {
        let mut backoff = SpinBackoff::new();
        'retry: loop {
            let root = self.root();
            if root.is_null() {
                return None;
            }
            if root.is_leaf() {
                let leaf = self.storage_leaf(root);
                if !matches(leaf) {
                    return None;
                }
                let Some(w) = self.root_latch.lock() else {
                    unreachable!("root latch is never obsolete")
                };
                if self.root() != root {
                    drop(w);
                    continue 'retry;
                }
                let count = confirm(leaf)?;
                let bomb = AbortOnUnwind;
                self.set_root(&w, Raw::<S>::NULL);
                // SAFETY: protected leaf read from `root`.
                unsafe { leaf.as_ref() }.mark_removed();
                self.len_add(-count);
                bomb.defuse();
                drop(w);
                // SAFETY: unlinked under `root_latch` and marked.
                unsafe { self.storage.retire_leaf(leaf, guard) };
                return Some(leaf);
            }

            let mut parent = Parent::Root;
            let mut node_raw = root;
            let mut depth = 0usize;
            loop {
                // SAFETY: protected inner node read from the tree.
                let node = unsafe { self.node_ref(node_raw) };
                let Some(v) = node.latch.read_version() else {
                    backoff.spin();
                    continue 'retry;
                };
                if !self.still_parent(parent, node_raw) {
                    continue 'retry;
                }
                let prefix = node.load_prefix();
                let rest = key.get(depth..).unwrap_or(&[]);
                if !prefix.is_prefix_of(rest) {
                    if !node.latch.validate(v) {
                        continue 'retry;
                    }
                    return None;
                }
                let node_depth = depth + prefix.len;
                let (slot, taken) = if node_depth == key.len() {
                    (node.exact_leaf(), Taken::Exact)
                } else {
                    let byte = key[node_depth];
                    let child = node.find_child(byte);
                    if let Some(c) = child.filter(|c| !c.is_leaf()) {
                        // Coupled at the next level (R4).
                        parent = Parent::Node {
                            raw: node_raw,
                            version: v,
                            byte,
                        };
                        node_raw = c;
                        depth = node_depth + 1;
                        continue;
                    }
                    (child.unwrap_or(Raw::<S>::NULL), Taken::Child(byte))
                };
                let empties = node.emptied_by(taken);
                if !node.latch.validate(v) {
                    continue 'retry;
                }
                if slot.is_null() {
                    return None;
                }
                let leaf = self.storage_leaf(slot);
                if !matches(leaf) {
                    return None;
                }
                if empties {
                    match self.remove_unlinking(parent, node_raw, v, taken, leaf, &confirm, guard) {
                        Unlink::Retry => {
                            backoff.spin();
                            continue 'retry;
                        }
                        Unlink::Refused => return None,
                        Unlink::Done(follow_up) => {
                            if follow_up {
                                self.unlink_emptied(key, guard);
                            }
                            return Some(leaf);
                        }
                    }
                }
                // `N` keeps an entry: lock only `N`. Its absolute path cannot
                // change without bumping its version (Inv 7).
                let Some(nw) = node.latch.try_upgrade(v) else {
                    backoff.spin();
                    continue 'retry;
                };
                let count = confirm(leaf)?;
                let bomb = AbortOnUnwind;
                take_out(node, &nw, taken);
                // SAFETY: protected leaf read from a validated slot.
                unsafe { leaf.as_ref() }.mark_removed();
                self.len_add(-count);
                bomb.defuse();
                drop(nw);
                // SAFETY: unlinked under the node latch and marked.
                unsafe { self.storage.retire_leaf(leaf, guard) };
                return Some(leaf);
            }
        }
    }

    /// The latched part of a remove that takes `N`'s (`node_raw`, read at
    /// version `v`) last entry: takes `P`, then `N`, and unlinks `N` with the
    /// leaf, per the module docs.
    #[allow(clippy::too_many_arguments)]
    fn remove_unlinking(
        &self,
        parent: Parent<Raw<S>>,
        node_raw: Raw<S>,
        v: u64,
        taken: Taken,
        leaf: NonNull<S::Leaf>,
        confirm: &impl Fn(NonNull<S::Leaf>) -> Option<isize>,
        guard: &S::Guard,
    ) -> Unlink {
        // SAFETY: protected inner node read from the tree in this operation.
        let node = unsafe { self.node_ref(node_raw) };
        crate::hooks::pause(crate::hooks::Point::WriterBeforeUpgrade);
        let Some(pg) = self.lock_parent(parent, node_raw) else {
            return Unlink::Retry;
        };
        #[cfg(loom)]
        if crate::latch::mutants::UNLINK_WITHOUT_NODE_LATCH.with(|m| m.get()) {
            // The mutant: unlink `N` holding only `P`, so a writer that locks
            // only `N` can still write into it.
            if !node.latch.validate(v) {
                return Unlink::Retry;
            }
            let Some(count) = confirm(leaf) else {
                return Unlink::Refused;
            };
            self.unlink_in_parent(&pg);
            // SAFETY: protected leaf read from a validated slot.
            unsafe { leaf.as_ref() }.mark_removed();
            self.len_add(-count);
            return Unlink::Done(false);
        }
        let Some(nw) = node.latch.try_upgrade(v) else {
            drop(pg);
            return Unlink::Retry;
        };
        // Read at `v`, which the upgrade just confirmed: exact.
        debug_assert!(node.emptied_by(taken));
        let Some(count) = confirm(leaf) else {
            drop(nw);
            drop(pg);
            return Unlink::Refused;
        };
        let bomb = AbortOnUnwind;
        // The store that unlinks `N`, and with it the removed leaf.
        self.unlink_in_parent(&pg);
        // SAFETY: protected leaf read from a validated slot.
        unsafe { leaf.as_ref() }.mark_removed();
        self.len_add(-count);
        // Inv 7: unlinked, so obsolete, before the parent is released.
        nw.mark_obsolete();
        bomb.defuse();
        let follow_up = match &pg {
            ParentGuard::Node(p, ..) => p.emptied_by(Taken::Nothing),
            ParentGuard::Root(_) => false,
        };
        drop(pg);
        // SAFETY: unlinked under the parent latch and marked.
        unsafe { self.storage.retire_leaf(leaf, guard) };
        // SAFETY: unlinked under the parent latch and obsolete; retired once.
        unsafe { self.storage.retire_node(self.node_ptr(node_raw), guard) };
        Unlink::Done(follow_up)
    }

    /// The follow-up (§13 3a): while the last inner node on `key`'s path is
    /// empty, unlinks it from its parent, then moves up to the parent.
    ///
    /// A new top-down operation: it holds nothing on entry, descends from the
    /// root, and restarts on any failed acquisition. Each step up takes the
    /// parent by `lock()` and a pointer re-check, and the node by
    /// `try_upgrade` of the version its own previous step unlocked it at, so
    /// a chain is unlinked without a re-descent per link. Every step unlinks a
    /// node, so the operation ends.
    fn unlink_emptied(&self, key: &[u8], guard: &S::Guard) {
        let mut backoff = SpinBackoff::new();
        // The ancestors of the node at hand: (node, version, byte to its child).
        let mut path: Vec<(Raw<S>, u64, u8)> = Vec::new();
        let parent_of = |path: &[(Raw<S>, u64, u8)]| match path.last() {
            None => Parent::Root,
            Some(&(raw, version, byte)) => Parent::Node { raw, version, byte },
        };
        'retry: loop {
            path.clear();
            let root = self.root();
            if root.is_null() || root.is_leaf() {
                return;
            }
            // Descend to the last inner node on `key`'s path.
            let mut node_raw = root;
            let mut depth = 0usize;
            let (mut x_raw, mut xv) = loop {
                // SAFETY: protected inner node read from the tree.
                let node = unsafe { self.node_ref(node_raw) };
                let Some(v) = node.latch.read_version() else {
                    backoff.spin();
                    continue 'retry;
                };
                if !self.still_parent(parent_of(&path), node_raw) {
                    continue 'retry;
                }
                let prefix = node.load_prefix();
                let rest = key.get(depth..).unwrap_or(&[]);
                let node_depth = depth + prefix.len;
                let next = match key.get(node_depth) {
                    Some(&byte) if prefix.is_prefix_of(rest) => node
                        .find_child(byte)
                        .filter(|c| !c.is_leaf())
                        .map(|c| (byte, c)),
                    _ => None,
                };
                if let Some((byte, c)) = next {
                    path.push((node_raw, v, byte));
                    node_raw = c;
                    depth = node_depth + 1;
                    continue;
                }
                let empty = node.emptied_by(Taken::Nothing);
                if !node.latch.validate(v) {
                    continue 'retry;
                }
                if !empty {
                    return;
                }
                break (node_raw, v);
            };
            // Unlink upwards while the node at hand is empty.
            loop {
                // SAFETY: protected inner node read from the tree in this operation.
                let x = unsafe { self.node_ref(x_raw) };
                let Some(pg) = self.lock_parent(parent_of(&path), x_raw) else {
                    backoff.spin();
                    continue 'retry;
                };
                let Some(xw) = x.latch.try_upgrade(xv) else {
                    drop(pg);
                    backoff.spin();
                    continue 'retry;
                };
                debug_assert!(x.emptied_by(Taken::Nothing));
                let bomb = AbortOnUnwind;
                self.unlink_in_parent(&pg);
                xw.mark_obsolete();
                bomb.defuse();
                let above_empty = match &pg {
                    ParentGuard::Node(p, ..) => p.emptied_by(Taken::Nothing),
                    ParentGuard::Root(_) => false,
                };
                let unlocked = pg.unlock();
                // SAFETY: unlinked under the parent latch and obsolete; retired once.
                unsafe { self.storage.retire_node(self.node_ptr(x_raw), guard) };
                if !above_empty {
                    return;
                }
                let (Some(pv), Some((p_raw, _, _))) = (unlocked, path.pop()) else {
                    return;
                };
                x_raw = p_raw;
                xv = pv;
            }
        }
    }
}

#[cfg(all(test, not(loom)))]
pub(crate) mod tests {
    use crate::raw::heap::{NODE_COUNTS, RETIRED_NODES};
    use crate::raw::node::{NodeHeader, Rest, Taken};
    use crate::raw::slot::Slot;
    use crate::raw::{Layout, Mode};
    use crate::tree::{Storage, Tree};

    pub(crate) type T = Tree<Vec<u8>, u64>;
    pub(crate) type Header = NodeHeader<<Storage<Vec<u8>, u64> as Layout>::Atomic>;

    pub(crate) fn tree(keys: &[&[u8]]) -> T {
        let t = T::new();
        let g = &crate::guard::pin();
        for k in keys {
            t.insert(k.to_vec(), 0, Mode::Replace, g);
        }
        t
    }

    pub(crate) fn remove(t: &T, k: &[u8]) -> bool {
        t.remove(k, &crate::guard::pin()).is_some()
    }

    pub(crate) fn retired() -> usize {
        RETIRED_NODES.with(|c| c.get())
    }

    pub(crate) fn root_node(t: &T) -> &Header {
        let r = t.raw.root();
        assert!(!r.is_null() && !r.is_leaf(), "the root is an inner node");
        // SAFETY: the test's tree is quiescent and alive for the borrow.
        unsafe { t.raw.node_ref(r) }
    }

    pub(crate) fn get(t: &T, k: &[u8]) -> bool {
        let _g = crate::guard::pin();
        t.raw.get(k).is_some()
    }

    /// Inner nodes on the path of children at byte `b` from the root, down
    /// to the first leaf.
    pub(crate) fn chain_len(t: &T) -> usize {
        let mut n = 0;
        let mut raw = t.raw.root();
        while !raw.is_null() && !raw.is_leaf() {
            n += 1;
            // SAFETY: quiescent tree, alive for the borrow.
            let node = unsafe { t.raw.node_ref(raw) };
            raw = node.next_child(0).map_or(node.exact_leaf(), |(_, c)| c);
        }
        n
    }

    /// A 100-byte shared prefix, then `last`.
    pub(crate) fn long_key(last: u8) -> Vec<u8> {
        let mut k = vec![7u8; 100];
        k.push(last);
        k
    }

    #[test]
    fn rest_and_emptied_by_see_what_a_node_keeps() {
        let t = tree(&[b"ka", b"kb", b"k"]);
        let n = root_node(&t);
        assert_eq!(n.rest(Taken::Nothing), Rest::Many);
        assert!(matches!(n.rest(Taken::Exact), Rest::Many));
        assert!(!n.emptied_by(Taken::Exact) && !n.emptied_by(Taken::Child(b'a')));
        drop(t);
        let t = tree(&[b"ka", b"k"]);
        let n = root_node(&t);
        assert!(matches!(n.rest(Taken::Exact), Rest::Leaf(l) if l.is_leaf()));
        assert!(matches!(n.rest(Taken::Child(b'a')), Rest::Leaf(l) if l == n.exact_leaf()));
        assert!(!n.emptied_by(Taken::Exact));
        drop(t);
        let t = tree(&[b"ka1", b"ka2", b"kb"]);
        let n = root_node(&t);
        assert!(matches!(n.rest(Taken::Child(b'b')), Rest::Inner(b'a', c) if !c.is_leaf()));
        assert!(!n.emptied_by(Taken::Nothing));
    }

    #[test]
    fn a_node_left_with_one_entry_stays() {
        let mut t = tree(&[b"a1", b"a2", b"b"]);
        let before = retired();
        assert!(remove(&t, b"a1"));
        assert_eq!(retired(), before, "nothing is unlinked");
        let child = root_node(&t).find_child(b'a').unwrap();
        assert!(!child.is_leaf(), "the 'a' node keeps a2");
        assert!(get(&t, b"a2") && get(&t, b"b") && !get(&t, b"a1"));
        assert_eq!(t.raw.validate(), 2);
    }

    #[test]
    fn a_node_emptied_by_a_remove_is_unlinked() {
        let mut t = tree(&[b"a1", b"a2", b"b"]);
        assert!(remove(&t, b"a1"));
        let before = retired();
        assert!(remove(&t, b"a2"));
        assert_eq!(retired() - before, 1, "the 'a' node is retired");
        assert!(root_node(&t).find_child(b'a').is_none());
        assert_eq!(t.raw.validate(), 1);
        // Exact leaves count as entries.
        let mut t = tree(&[b"k", b"ka", b"x"]);
        assert!(remove(&t, b"ka"));
        assert!(get(&t, b"k"));
        assert!(remove(&t, b"k"));
        assert!(root_node(&t).find_child(b'k').is_none());
        assert_eq!(t.raw.validate(), 1);
    }

    #[test]
    fn the_root_node_goes_with_its_last_key() {
        let mut t = tree(&[b"ka", b"kb"]);
        assert!(remove(&t, b"ka"));
        assert!(!t.raw.root().is_leaf(), "the root node keeps kb");
        let before = retired();
        assert!(remove(&t, b"kb"));
        assert!(t.raw.root().is_null());
        assert_eq!(retired() - before, 1);
        assert_eq!(t.raw.validate(), 0);
    }

    #[test]
    fn a_prefix_chain_goes_with_its_last_key() {
        // 100 shared bytes: a chain of single-child Node4s above the fork.
        let mut t = tree(&[&long_key(1), &long_key(2)]);
        let chain = chain_len(&t);
        assert!(chain >= 6, "a chain of {chain} nodes");
        let before = retired();
        assert!(remove(&t, &long_key(1)));
        assert_eq!(retired(), before, "the fork keeps a key");
        assert!(remove(&t, &long_key(2)));
        assert_eq!(retired() - before, chain, "the whole chain is retired");
        assert!(t.raw.root().is_null());
        assert_eq!(t.raw.validate(), 0);
    }

    #[test]
    fn an_emptied_chain_is_unlinked_up_to_the_first_fork() {
        // A fork above a 40-byte chain: the chain goes, the fork stays.
        let key = |mid: u8, last: u8| {
            let mut k = vec![mid];
            k.extend_from_slice(&[9u8; 40]);
            k.push(last);
            k
        };
        let mut t = tree(&[&key(1, 1), &key(1, 2), &key(2, 0)]);
        assert!(remove(&t, &key(1, 1)));
        assert!(remove(&t, &key(1, 2)));
        let root = root_node(&t);
        assert!(root.find_child(1).is_none());
        assert!(root.find_child(2).unwrap().is_leaf());
        assert_eq!(t.raw.validate(), 1);
    }

    /// Every node is allocated once and then either freed unpublished, freed
    /// by drop, or retired: never leaked and never retired twice.
    #[test]
    fn every_node_is_accounted_for_under_churn() {
        use rand::{Rng, SeedableRng};
        let counts = || NODE_COUNTS.with(|c| (c.allocated.get(), c.freed.get(), c.retired.get()));
        let (a0, f0, r0) = counts();
        let mut rng = rand::rngs::StdRng::seed_from_u64(7);
        let n = if cfg!(miri) { 300 } else { 20_000 };
        {
            let mut t = T::new();
            let mut live = std::collections::BTreeSet::new();
            let g = &crate::guard::pin();
            for i in 0..n {
                let len = rng.gen_range(0..6);
                let k: Vec<u8> = (0..len).map(|_| rng.gen_range(0..4u8)).collect();
                if rng.gen_bool(0.5) {
                    t.insert(k.clone(), i, Mode::Replace, g);
                    live.insert(k);
                } else {
                    assert_eq!(t.remove(&k, g).is_some(), live.remove(&k));
                }
                if i % 97 == 0 {
                    t.raw.shrink_to_fit(g);
                }
            }
            assert_eq!(t.raw.validate(), live.len());
            for k in &live {
                assert!(t.remove(k, g).is_some());
            }
            assert!(t.raw.root().is_null(), "an emptied tree has no nodes");
            assert_eq!(t.raw.validate(), 0);
        }
        let (a, f, r) = counts();
        assert!(a > a0);
        assert_eq!(
            a - a0,
            (f - f0) + (r - r0),
            "a node leaked or was retired twice"
        );
    }

    /// A sliding window keeps the tree at the window's size.
    #[test]
    fn a_sliding_window_stays_small() {
        let counts = || NODE_COUNTS.with(|c| c.allocated.get() - c.freed.get() - c.retired.get());
        let window = 64u64;
        let ops: u64 = if cfg!(miri) { 600 } else { 100_000 };
        let t = T::new();
        let base = counts();
        let mut peak = 0;
        let g = &crate::guard::pin();
        for i in 0..ops {
            t.insert(i.to_be_bytes().to_vec(), i, Mode::Replace, g);
            if i >= window {
                assert!(t.remove(&(i - window).to_be_bytes(), g).is_some());
            }
            peak = peak.max(counts() - base);
        }
        assert!(peak <= 8, "{peak} live nodes for a window of {window} keys");
    }
}