xdb-parse
A high-performance, zero-copy Rust library for querying xdb IP geolocation databases (ip2region-compatible). Supports IPv4 and IPv6 via an optimized two-level binary search.
Features
- Dual Protocol: Full IPv4 and IPv6 support
- Blazing Fast: ~258ns per IPv4
search_ip(u32)lookup, ~527ns persearch_ip(&str)(all-segment IP pool) - Zero-copy: Returns
&strborrowing from the loaded data — no heap allocation per query - Raw integer API:
search_by_uint/search_by_u128skip string parsing entirely - Thread Safe:
Send + Sync, load once and share viaArc
Installation
[]
= "0.2.0"
Quick Start
use ;
API Reference
Loading
Pre-allocates the buffer with the exact file size.
Searching
// Search by IP string (dotted-decimal, colon-hex, or numeric)
All search functions return &str borrowing from the loaded data — zero allocation per query.
Supported IP Formats
search_ip?; // dotted-decimal IPv4
search_ip?; // colon-hex IPv6
search_ip?; // numeric IPv4
search_ip?; // numeric IPv6
Header & Version Detection
use ;
let data = load_file?;
let version = detect_version?;
assert_eq!;
let header = parse?;
println!;
Multi-threaded Usage
use Arc;
use ;
let data = new;
let data_clone = clone;
spawn.join.unwrap;
Performance
Measured with all-segment IP pool (524K IPv4 / 704K IPv6), criterion release profile:
| Scenario | Time |
|---|---|
IPv4 search_by_uint |
~272 ns |
IPv4 search_ip(u32) |
~258 ns |
IPv4 search_ip(&str) |
~527 ns |
IPv6 search_ip(&str) |
~476 ns |
Run benchmarks:
Database File Format
┌─────────────────┐
│ Header │ 256 bytes
├─────────────────┤
│ Vector Index │ 256×256×8 bytes
├─────────────────┤
│ Segment Data │ Variable length
└─────────────────┘
IPv4 segments: 14 bytes (u32 start, u32 end, u16 data_len, u32 data_ptr).
IPv6 segments: 38 bytes (u128 start, u128 end, u16 data_len, u32 data_ptr).
License
MIT OR Apache-2.0