Skip to content
surrealdbPublic

About

An immutable, copy-on-write, versioned adaptive radix trie data-structure

Topics

Resources

Security policy

Stars

116 stars

Watchers

9 watching

Forks

Repository files navigation

vart

An immutable, versioned, adaptive radix trie data structure for Rust.


     

vart is a pure safe-Rust implementation of an immutable Versioned Adaptive Radix Tree (ART) data structure. It combines the space efficiency and cache locality of adaptive radix trees with copy-on-write structural sharing and multi-version timestamp history.

It is designed as an in-memory index engine for databases, storage engines, and analytical applications that require microsecond-latency point lookups, zero-allocation range scans, snapshot reads, and point-in-time historical queries.

Performance

Benchmarked on bare metal (AMD Ryzen Threadripper 9970X 32-Core / 64-Thread Processor @ 5.48 GHz, 128 GB DDR5 RAM, Linux 6.8):

Data Structure Read
(with standard key)
Read
(with slice key)
Insert
(sequential entries)
Insert
(random entries)
Range scans
(100 items)
vart::Tree 🚀 28.9 ns
(34.6M/s)
🚀 27.3 ns
(36.6M/s)
83.2 ns
(12.0M/s)
114.6 ns
(8.7M/s)
819 ns
(122.1M/s)
imbl::OrdMap 46.5 ns
(21.5M/s)
— 52.8 ns
(19.0M/s)
71.6 ns
(14.0M/s)
341 ns
(293M/s)
std::collections::BTreeMap 58.6 ns
(17.1M/s)
— 🚀 31.5 ns
(31.7M/s)
🚀 64.7 ns
(15.5M/s)
🚀 183 ns
(546M/s)
std::collections::HashMap* 13.1 ns
(76.1M/s)
— 18.7 ns
(53.5M/s)
21.1 ns
(47.4M/s)
N/A

* std::collections::HashMap is included as an unordered $O(1)$ reference baseline and does not support range queries, sorted scans, or snapshots. The rocket icon denotes the fastest implementation among ordered, range-scannable maps. Sequential insert for vart::Tree utilizes in-place mutation via insert_unchecked.

Snapshot Creation & Memory Footprint

Data Structure Snapshot Clone
(branching cost)
Allocations
(per insert)
Concurrency & Safety Model
vart::Tree 🚀 8.12 ns 1.0 100% Safe Rust (#![forbid(unsafe_code)]) + Structural Sharing
imbl::OrdMap 🚀 8.12 ns ~0.14 Safe Rust + Chunked Copy-on-Write B-Tree
std::collections::BTreeMap 568.2 µs ~0.17 Standard Library B-Tree (Deep Clone $O(N)$)
std::collections::HashMap* N/A ~0 Standard Library Hash Table
  • Fastest Persistent Point Lookups: Point reads in vart (27.3 ns slice, 28.9 ns standard key) are 58% faster than imbl::OrdMap (46.5 ns) and 2× faster than standard BTreeMap (58.6 ns).
  • Instantaneous Snapshot Creation: $O(1)$ snapshot creation via atomic reference counting (tree.clone()) takes 8.12 ns, matching imbl::OrdMap and outperforming BTreeMap deep clones (568.2 µs) by 70,000×.
  • Zero-Allocation Range Scanning: Traverses 100 contiguous items in 819 ns (122M items/sec) with zero heap allocations during iteration via the unboxed ChildrenIter enum.
  • In-Place Mutation Bypass: High-throughput ingestion via tree.insert_unchecked (83.2 ns), avoiding copy-on-write overhead when writing to uniquely owned trees while safely falling back to copy-on-write for shared snapshot paths.

Features

  • 100% Safe Rust: Enforced with #![forbid(unsafe_code)] at the crate root.
  • Adaptive Radix Tree Architecture: Dynamically resizes inner nodes across 4 compact layouts (Node4 $\leftrightarrow$ Node16 $\leftrightarrow$ Node48 $\leftrightarrow$ Node256) to maximize L1/L2 cache locality and prefix compression.
  • Multi-Version Timestamp Ordering: Supports multiple version and timestamp entries per key for point-in-time reads and transactional MVCC indexes.
  • Copy-on-Write Snapshots: $O(1)$ tree branching; mutating a snapshot or active tree preserves complete isolation with shared subtrees safely cloned on write.
  • Zero-Allocation Slice Queries: Look up records using raw byte slices (&[u8]) or string references (&str) without creating temporary key objects.
  • Unboxed Traversal: Stack-allocated ChildrenIter eliminates double-boxing and dynamic vtable dispatch during forward and backward iteration.
  • Bidirectional Range Scans: Full DoubleEndedIterator support across arbitrary ranges (tree.range(A..B) and tree.range(A..B).rev()), with lazy backward state initialization.
  • Standard Rust Collection Traits: Implements len(), is_empty(), fmt::Debug, IntoIterator, FromIterator, Deref, Borrow, AsRef, From, Default, and Hash.
  • Thread Safety Guaranteed: Compile-time static assertions ensure Tree<P, V>, Node<P, V>, and iterators implement Send + Sync.
  • Deterministic Simulation Tested (DST): Continuously validated by a seeded PRNG fuzzer against an in-memory BTreeMap reference oracle across millions of operations with continuous structural invariant verification.

Quick Start

Add vart to your Cargo.toml:

[dependencies]
vart = "0.11"
use vart::art::Tree;
use vart::VariableSizeKey;

fn main() {
    let mut tree = Tree::<VariableSizeKey, String>::new();

    let user_1 = VariableSizeKey::from("users:0001");
    let user_2 = VariableSizeKey::from("users:0002");

    // Insert versioned key-value pairs (key, value, version, timestamp)
    tree.insert(&user_1, "Alice (v1)".into(), 1, 100).unwrap();
    tree.insert(&user_1, "Alice (v2)".into(), 2, 200).unwrap();
    tree.insert(&user_2, "Bob".into(), 1, 150).unwrap();

    assert_eq!(tree.len(), 2);

    // Point lookup (latest version)
    let (val, version, ts) = tree.get(&user_1, 0).unwrap();
    assert_eq!(val, "Alice (v2)");
    assert_eq!(version, 2);

    // Zero-allocation point lookup via raw byte slice
    assert!(tree.contains_key_slice(b"users:0001"));
    let (val, _, _) = tree.get_by_slice(b"users:0001", 0).unwrap();
    assert_eq!(val, "Alice (v2)");

    // Point-in-time read at historical timestamp
    let (historical_val, _, _) = tree.get_at_ts(&user_1, 150).unwrap();
    assert_eq!(historical_val, "Alice (v1)");

    // Create a copy-on-write snapshot (O(1))
    let snapshot = tree.clone();

    // Mutate the active tree
    let user_3 = VariableSizeKey::from("users:0003");
    tree.insert(&user_3, "Charlie".into(), 1, 300).unwrap();

    // Verify snapshot isolation
    assert!(tree.contains_key_slice(b"users:0003"));
    assert!(!snapshot.contains_key_slice(b"users:0003"));

    // Range scanning
    let start = VariableSizeKey::from("users:0001");
    let end = VariableSizeKey::from("users:0004");
    for (key_bytes, val, ver, ts) in tree.range(&start..&end) {
        println!("{:?} = {} (v{}, ts{})", std::str::from_utf8(key_bytes).unwrap(), val, ver, ts);
    }
}

Core Operations

Zero-Allocation Slice Lookups

Avoid allocating key wrapper structs when querying the tree:

use vart::art::Tree;
use vart::VariableSizeKey;

let mut tree = Tree::<VariableSizeKey, i32>::new();
let key = VariableSizeKey::from("tenant:100:profile");
tree.insert(&key, 42, 1, 100).unwrap();

// Zero-allocation check and retrieval using raw bytes
if tree.contains_key_slice(b"tenant:100:profile") {
    let (val, version, ts) = tree.get_by_slice(b"tenant:100:profile", 0).unwrap();
    assert_eq!(val, 42);
}

Snapshot Isolation & Concurrent Branches

Creating a snapshot is an $O(1)$ reference copy. Shared subtrees are lazily cloned on write:

let mut tree = Tree::<VariableSizeKey, String>::new();
let k1 = VariableSizeKey::from("k1");
tree.insert(&k1, "initial".into(), 1, 10).unwrap();

// Create snapshot
let mut snapshot = tree.clone();

// Mutating one branch does not affect the other
tree.insert(&k1, "updated in main".into(), 2, 20).unwrap();
snapshot.insert(&k1, "updated in snap".into(), 2, 30).unwrap();

assert_eq!(tree.get(&k1, 0).unwrap().0, "updated in main");
assert_eq!(snapshot.get(&k1, 0).unwrap().0, "updated in snap");

Bidirectional Range Scanning

Iterators implement DoubleEndedIterator with zero heap allocation during traversal:

let start = VariableSizeKey::from("prefix:001");
let end = VariableSizeKey::from("prefix:100");

// Forward iteration
for (k, v, _, _) in tree.range(&start..&end) {
    // ...
}

// Reverse iteration
for (k, v, _, _) in tree.range(&start..&end).rev() {
    // ...
}

Key Types

vart provides two built-in key abstractions:

  1. VariableSizeKey: General-purpose variable-length byte key backed by Vec<u8>. Implements From<Vec<u8>>, From<String>, From<&str>, Deref<Target = [u8]>, Borrow<[u8]>, and Hash.
  2. FixedSizeKey<const SIZE: usize>: Inline array-backed key ([u8; SIZE]) for fixed-size keys (e.g. 64-bit/128-bit integers, UUIDs, or hashes) eliminating heap allocations for key storage.

Deterministic Simulation Testing (DST)

vart includes a deterministic simulation testing harness in tests/sim.rs inspired by FoundationDB and TigerBeetle:

  • Seeded PRNG: Every simulation run is parameterized by a 64-bit seed (VART_SIM_SEED=<seed>) to reproduce failures down to the byte.
  • Reference Oracle: Runs state transitions in lockstep against a canonical BTreeMap reference oracle.
  • Continuous Invariant Checking: Validates prefix compression, node capacity boundaries, child bitmaps, and version monotonicity after every simulated step via tree.validate_invariants().

To run the simulation suite:

# Run with a random seed
cargo test --test sim -- --nocapture

# Run with an exact reproducible seed
VART_SIM_SEED=20202 cargo test --test sim -- --nocapture

Benchmarks

Benchmarks can be run locally or executed on a dedicated bare-metal server using the included remote runner:

# Run comparison benchmarks locally
cargo bench --bench comparison_bench

# Run allocation and memory benchmarks locally
cargo bench --bench alloc_comparison

# Run on remote dedicated hardware (AMD Threadripper)
./scripts/bench-remote.sh --all

License

This project is licensed under the Apache License, Version 2.0.

About

An immutable, copy-on-write, versioned adaptive radix trie data-structure

Topics

Resources

Security policy

Stars

116 stars

Watchers

9 watching

Forks

Releases

Used by

Contributors

Languages