Skip to content

Use a radix trie for IX prefix lookup - #85

Merged
lance0 merged 1 commit into
masterfrom
ix-radix-trie
Jun 10, 2026
Merged

lance0 merged 1 commit into
masterfrom
ix-radix-trie

Conversation

@lance0

@lance0 lance0 commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Summary

Implements the roadmap item IX lookup performance — radix trie for O(prefix_len) instead of O(n) linear scan.

  • Replaces the sorted-vec linear scan in src/lookup/ix.rs with a hand-rolled binary radix trie (flat node arena, no new dependencies): separate IPv4/IPv6 tries, terminal nodes index into the shared IxInfo vec, longest-prefix-match tracked during descent
  • Per-IP match goes from O(n) over ~2,000 PeeringDB prefixes (worst on misses — the common case, since most hops are not IX hops) to O(prefix_len): ≤32 node hops for IPv4, ≤128 for IPv6, independent of prefix count
  • Public API unchanged (IxLookup::new/lookup/refresh_cache, run_ix_worker, CacheStatus) — zero call-site edits
  • Preserves existing semantics: family separation (IPv4-mapped IPv6 doesn't match v4 prefixes), first-insert-wins on duplicate prefixes (matching the old stable-sort tie-break), /0 root handling

Testing

16 ix tests pass (new: IPv4/IPv6 match + non-match, nested /8–/16–/24 longest-prefix-match, overlapping IPv6 /32+/48, /32 and /128 host prefixes, family separation, empty trie). Full suite green; fmt + clippy -D warnings clean.

Replace the linear scan over all PeeringDB IX prefixes with per-family
binary radix tries keyed on address bits. Lookup now walks at most one
node per address bit, so matching costs O(prefix_len) — at most 32 steps
for IPv4 and 128 for IPv6 — instead of O(n) over the full prefix list
(roughly 2,000 prefixes per miss with the old sorted-vec scan).

Tries store indices into a shared IxInfo vec at terminal nodes and track
the deepest match during descent, preserving longest-prefix-match
semantics. Separate IPv4/IPv6 tries preserve the address-family
separation that IpNetwork::contains provided. The public IxLookup API is
unchanged.
@lance0
lance0 merged commit 4262754 into master Jun 10, 2026
5 checks passed
@lance0
lance0 deleted the ix-radix-trie branch June 10, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant