Skip to content

RRB Vector

Relaxed Radix Balanced Vector

owais
Jul 18, 20262 min read

n RRB vector is a persistent, immutable sequence implemented using a Relaxed Radix Balanced tree1.

It's essentially a persistent vector redesigned to make concatenation and slicing fast, while retaining efficient indexed access2

            root
     ┌───────┼────────┐
     ▼       ▼        ▼
  subtree  subtree  subtree
   64 el.   41 el.   28 el.
     │        │         │
[leaves containing blocks of values]

RRB

  • Radix: an index is divided into bit groups used to navigate the tree. Implementations commonly use a branching factor of 32.
  • Balanced: the leaves remain at approximately the same tree depth. i.e. you do not want one branch to become arbitrarily deep like a linked list. Keeping the tree shallow preserves logarithmic lookup and update behavior.
  • Relaxed: subtrees do not have to be completely full or uniformly sized. Nodes can store cumulative subtree sizes to route index lookups through these uneven branches.

Performance

OperationApproximate complexity
Indexed lookupO(log₃₂ n)
Immutable updateO(log₃₂ n)
AppendO(log₃₂ n), effectively near constant
Concatenate vectorsO(log n)
Materialized sliceO(log n)
IterationO(n)

Use Cases

  • Unlike an array, it supports efficient immutable changes.
  • Unlike a linked list, it provides fast indexed access.
  • Unlike a basic persistent vector, it supports efficient concatenation and real, non-view slicing.
  • Unlike a rope, it remains suitable as a general-purpose indexed sequence.

See Also

Gleam impl

core.rrb-vector

Footnotes

  1. A Relaxed Radix Balanced tree is a wide, shallow tree for implementing immutable indexed sequences.

  2. Bagwell, P., Rompf, T. (2011). RRB-Trees: efficient immutable vectors. http://infoscience.epfl.ch/record/169879/files/RMTrees.pdf

Did you enjoy this article?

Recommend it — Standard Reader surfaces well-loved writing to more readers across the network.

Across the AtmosphereDiscussions