2 releases
Uses new Rust 2024
| 0.1.1 | Oct 23, 2025 |
|---|---|
| 0.1.0 | Oct 22, 2025 |
#253 in Profiling
13KB
179 lines
Compile-time optimized membership checks for static byte sequences.
comtains expands fixed sets of byte strings into zero-allocation,
branch-ordered decision trees. The resulting ByteSet can be embedded
directly into hot parsers, opcode dispatchers, or protocol classifiers
without any runtime preprocessing.
Example
use comtains::{byte_set, ByteSet};
const KEYWORDS: ByteSet = byte_set![
b"GET",
b"POST",
b"PUT",
b"PATCH",
];
assert!(KEYWORDS.contains(b"POST"));
assert!(!KEYWORDS.contains(b"DELETE"));
Design
- The
byte_set!macro parses string or byte-sequence literals at compile time and builds a trie that merges shared prefixes. - Each branch records how many sequences pass through it; siblings are sorted by descending weight so the hottest paths are checked first.
- The macro emits a nested
matchladder that compares each byte viacandidate.get(depth), returning early on the first mismatch. - Optional debug metadata mirrors the trie structure so unit tests and benchmarks can assert branch ordering.
comtains
comtains expands static byte sequences into zero-allocation matchers at compile time. The byte_set! macro emits a branch-ordered decision tree, keeping membership checks to a handful of predictable instructions—ideal for tight opcode dispatchers or protocol parsers.
use comtains::{byte_set, ByteSet};
const HTTP_METHODS: ByteSet = byte_set![b"GET", b"POST", b"PUT", b"PATCH"];
assert!(HTTP_METHODS.contains(b"GET"));
assert!(!HTTP_METHODS.contains(b"DELETE"));
How it works
- All inputs are parsed at macro expansion time into a trie that shares common prefixes.
- Each edge records how many sequences traverse it; siblings are sorted by descending weight to probe common paths first.
- The macro generates a nested
matchladder that comparescandidate[depth], short-circuiting on the first mismatch. - Debug metadata is emitted alongside the matcher so tests and benchmarks can assert branch ordering or inspect the trie layout.
Dependencies
~130–510KB
~12K SLoC