Skip to content

About

Cache aware and mem efficient data structures for rust.

Topics

Resources

Stars

25 stars

Watchers

1 watching

Forks

Repository files navigation

Embed Collections

docs.rs: https://docs.rs/embed-collections/latest/embed_collections/

embed-collections

A collection of memory efficient data structures, for embedding environment and server applications that need tight memory management.

This repo consists some small crates.

NOTE: There nothing in the main crate, all modules have moved to sub crates.

Cache efficient collections

Sub crates:

  • embed-btree: A cache aware B+tree for small and large dataset, optimized for numeric types, with special entry API allows peeking adjacent values.
  • embed-seglist: A cache aware (short-live) list to store elements with adaptive size segments
  • embed-constvec: Fixed capacity inline vec

Intrusive collections:

We support all-types from the pointers crate, with Pointer trait for intrusive collections:

  • raw pointers (NonNull<T>, *const T, *mut T)
  • std Box (owned),
  • std Arc, Rc (multiple ownership)
  • Irc: Highly customizable Intrusive Reference Counter.
  • WaitGroupZeroGuard: see the doc in crossfire crate

Sub crates:

  • embed-dlist: Intrusive Doubly Linked List (Queue / Stack).
  • embed-slist: Intrusive Singly Linked List ( Queue / stack).
  • embed-avl: Intrusive AVL Tree (Balanced Binary Search Tree), port to rust from ZFS

Disclaimer

Intrusive code is not recommended unless you are full aware of what you are doing. Most traits are mark with unsafe. While we try to give you freedom, not letting the rules probibit you build highly customized logic, this library still has regular scheduled Miri test routine. But the disadvantages includes:

  • Complex to write (This crate seal most boilderplates)

  • Linking heap object to another is bad for cache hit (Use structure like SegList is preferable)

There're three usage scenarios:

  1. Push smart pointer to the list, so that the list hold 1 ref count when the type is Arc / Rc, but you have to use UnsafeCell for internal mutation.

  2. Push Box to the list, the list own the items until they are popped, it's better than std LinkedList because no additional allocation is needed. It will not move the item in-and-out of hidden Box on every push / pop.

  3. Push raw pointer (better use NonNull instead of *const T for smaller footprint) to the list, for temporary usage. You must ensure the list item not dropped be other refcount (for example, the item is holding by Arc in other structure).

Difference to intrusive-collections crate

This crate choose to use trait instead of c like offset_of!, mainly because:

  • Mangling with offset conversion makes the code hard to read (for people not used to c style coding).

  • You don't have to understand some complex macro style.

  • It's dangerous to use pointer offset conversion when the embedded Node not perfectly aligned, and using memtion to return the node ref is more safer approach. For example, the default repr(Rust) might reorder the field, or you mistakenly use repr(packed).

embed-seglist

crate: embed-seglist

SegList and Various is designed for parameter passing.

More CPU-cache friendly compared to LinkedList. And because it does not re-allocate, it's faster than Vec::push() when the number of elements is small. It's nice to the memory allocator (always allocate with fixed size segment).

Benchmark: append + drain (x86_64, cache line 128 bytes):

(platform: intel i7-8550U)

Elements SegList Vec SegList vs Vec
10 40.5 ns 147.0 ns 3.6x faster
32 99.1 ns 237.8 ns 2.4x faster
100 471.1 ns 464.0 ns ~1.0x
500 2.77 µs 895.5 ns 3.1x slower

embed-btree

crate: embed-btree

We provide a BTreeMap for single-threaded either short term (compact), long-term (large) in-memory dataset. It's a cache aware b+tree:

  • Being B+Tree, the leaves are linked, provide faster iteration and teardown.
  • Nodes are filled up in 4 cache lines (256 bytes on x86_64).
    • Capacity in compile-time determined according to the size of Key, Value.
    • Reduce memory fragmentation by alignment.
  • Optimised for numeric key
    • Respecting numeric space for sequential insertion.
    • Reduce latency for sequential insertion.
  • Bytes keys on the heap are supported, but we will not do prefix compress.
  • Support unbalanced size K / V, and 0-size V, will fill the space (to increase fanout) as much as it could.
  • Limitation:
    • K should have clone (for propagate into the InterNode during split)
    • K & V should <= CACHE_LINE_SIZE - 16
      • It make sure InterNode can hold at least two children.
      • If K & V is large you should put into Box, for room saving, and for the speed to move value
  • Special API:
    • Peak and move to previous/next Entry (for modification).
    • Alter key of an OccupiedEntry.
    • Batch remove with range.
    • Movable Cursor (for readonly)

Compared to std::collections::btree (as of rust 1.94):

  • The std impl is pure btree (not b+tree) without horizontal links. Each key store only once at either leaf and inter nodes.
  • The std impl is optimised for point lookup.
  • The std impl has fixed Cap=11, node size varies according to T. (For T=U64, size is 288B for InterNode and 192B for LeafNode)
  • The std cursor API is still unstable (as of 1.94) and relatively complex to use.

benchmark

benchmarks: https://github.com/NaturalIO/embed-collections-rs/wiki/embed%E2%80%90btree-v0.4-2026.10.11

About

Cache aware and mem efficient data structures for rust.

Topics

Resources

Stars

25 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages