antirez/rax logo

antirez/rax

Free

A radix tree implementation in ANSI C

FreeFree tier
Type
Open Source

About antirez/rax

Rax is a radix tree implementation initially written for Redis to solve a performance problem, but soon converted into a standalone project. It aims to balance performance and memory usage while providing a fully featured radix tree implementation with a flexible iterator. The library features memory-conscious packed nodes, fast lookups using byte arrays for edges, cache-friendly scanning, deletion with node re-compression, iterators (including safe iteration during tree modification), random walk iteration, out-of-memory resistance, and portability in ANSI C99. It relies heavily on fuzz testing to ensure robustness and is released under the BSD two-clause license.

Key Features

Memory-conscious packed nodes representation avoiding NULL pointer stores
Fast lookups with edges stored as byte arrays directly in parent node for fewer cache misses
Cache-friendly scanning of child nodes using separate arrays for edge characters and pointers
Complete implementation with deletion, node re-compression, and multiple iterator types (including safe iteration during tree modification)
Random walk iteration and ability to report and resist out-of-memory errors
Readable and fixable code with algorithmic comments, debugging messages, and ASCII art representation of nodes
Portable ANSI C99 implementation with no unaligned memory accesses
Extensive fuzz testing and comparison with hash tables and sorted arrays for correctness

Pros & Cons

Pros
  • Memory efficient with packed node representation and reduced pointer overhead
  • Fast lookups through cache-friendly edge storage and byte array scanning
  • Robust implementation with extensive fuzz testing, OOM handling, and edge case coverage
  • Portable ANSI C99 code with no platform-specific extensions
  • Well-documented with algorithmic comments, debugging support, and ASCII art visualization
  • Open source under permissive BSD two-clause license
Cons
  • Limited to ANSI C environments, not applicable to other programming languages directly
  • Not a general-purpose data structure; specifically optimized for radix tree use cases
  • May have higher complexity for simple use cases compared to hash tables or binary search trees

Best For

Prefix matching and auto-complete systemsIP routing and dictionary implementationsEfficient key-value storage with string keysRedis internal data structure for performance improvement

FAQ

What is Rax?
Rax is an ANSI C radix tree implementation originally developed for Redis, now available as a standalone open-source project under the BSD two-clause license.
What are the main features of Rax?
Rax features memory-conscious packed nodes, fast lookups with cache-friendly edge storage, deletion with node re-compression, iterators (including safe iteration during tree modification), random walk iteration, out-of-memory resistance, portability in ANSI C99, and extensive fuzz testing.
Is Rax production-ready?
Yes, Rax is designed to be a battle-tested implementation, relying heavily on fuzz testing and comparison with hash tables and sorted arrays to ensure robustness. It has been used in Redis.