2. Architecture

perennial@0.1.0Updated 2 min readView as markdown

Written from the public algorithm literature (Bayer & McCreight 1972 on B-trees; Bagwell 2001 on hash array mapped tries; Steindorfer & Vinju 2015 on CHAMP; Bagwell & Rompf 2011 and Stucki et al. 2015 on RRB vectors) and from docs/spec/. No source of im, im-rc, imbl, sized-chunks, bitmaps, rpds or archery was read by anyone who writes code here.

Crates

Crate Role Public paths
perennial the collections, generic over a pointer kind; Arc aliases at the root, as im has perennial::{HashMap, HashSet, OrdMap, OrdSet, Vector}, perennial::{hashmap, hashset, ordmap, ordset, vector}::*, perennial::rc::*
perennial-rc a facade that re-exports perennial::rc with im-rc's paths, so a renamed dependency is the whole migration perennial_rc::{HashMap, …}, perennial_rc::hashmap::Entry, …

A user of im-rc migrates with one Cargo.toml line and no source change:

im-rc = { package = "perennial-rc", version = "0.1" }

A user of im does the same with im = { package = "perennial", version = "0.1" }.

Pointer kinds

One internal trait abstracts the shared pointer, with a generic associated type:

  • trait PointerKind { type Ptr<T>: Deref<Target = T> + Clone; fn new; fn make_mut; fn get_mut; fn ptr_eq; }, implemented by two zero-sized marker types, RcK (alloc::rc::Rc) and ArcK (alloc::sync::Arc).
  • Every collection is Generic<Name><…, P: PointerKind>; the public names are type aliases that fix P, so Send/Sync follow the pointer as they do for Rc and Arc themselves.
  • make_mut is the copy-on-write primitive: a node that is uniquely owned is mutated in place; a shared node is cloned once and the clone is mutated. This is the main speed lever in all three structures (chapter 3): after the first write that unshares a path, further writes to the same collection allocate nothing on that path.

Layering (SBIO)

The crates do no I/O. The only environment input is the default hasher's random seed (std::collections::hash_map::RandomState, behind the default std feature). Without std, HashMap/HashSet require an explicit BuildHasher (PCND: no silent default).

unsafe policy

#![forbid(unsafe_code)] in both crates. Node storage uses Vec with exact capacities and, where measured to pay, arrayvec (MIT OR Apache-2.0). Panic safety then follows from the standard library's own guarantees: a panic in a user Clone, Ord, Hash, Eq or Drop can leak or leave a collection unchanged, but cannot double-drop or read freed memory (the bug class of RUSTSEC-2026-0251/-0255). Chapter 4 lists the negative tests.

Modules (each file ≤ 250 lines)

src/lib.rs            re-exports, aliases, macros
src/ptr.rs            PointerKind, RcK, ArcK
src/ordmap/           B+tree: node.rs, insert.rs, remove.rs, iter.rs, range.rs, entry.rs, api*.rs
src/ordset/           thin wrapper over the OrdMap tree with V = ()
src/hashmap/          HAMT: node.rs, bitmap.rs, insert.rs, remove.rs, iter.rs, entry.rs, api*.rs
src/hashset/          thin wrapper over the HAMT with V = ()
src/vector/           RRB: tree.rs, push.rs, concat.rs, split.rs, focus.rs, iter.rs, api*.rs
src/traits/           std trait impls shared by macro (Debug, Eq, Ord, Hash, From, Extend, …)
src/feature/          serde, proptest, arbitrary, quickcheck, rayon (each behind its feature)

The bitmap used by the HAMT is a private u32 helper (popcount, rank); it is not a crate and is not exported.