perf: optimize DNS query hot path #15

Merged
razvandimescu merged 2 commits from perf/hot-path-optimizations into main 2026-03-27 08:01:08 +08:00
razvandimescu commented 2026-03-25 11:36:32 +08:00 (Migrated from github.com)

Summary

  • Mutex → RwLock for cache, blocklist, and overrides — concurrent DNS queries no longer serialize on read locks
  • Inline filtering in DnsPacket::write() — eliminates 3 Vec allocations per response serialization
  • Read-only lookups for cache and overrides — expired entries cleaned up lazily on write path
  • Pre-allocated strings (String::with_capacity(64)) in DNS record parsing — avoids reallocation for typical domain names
  • Criterion benchmarks added (cargo bench) with hot_path and throughput suites + bench README

Benchmark results (before → after)

Operation Before After Change
buffer_parse 534 ns 449 ns -14%
buffer_serialize 358 ns 339 ns -3.5%
round_trip_cached 733 ns 698 ns -3.2%
pipeline_throughput/100 49.0 µs 44.0 µs -8.5%

Test plan

  • All 27 existing tests pass
  • cargo fmt --check clean
  • cargo clippy -- -D warnings clean
  • Criterion benchmarks run successfully with measurable improvement

🤖 Generated with Claude Code

## Summary - **Mutex → RwLock** for cache, blocklist, and overrides — concurrent DNS queries no longer serialize on read locks - **Inline filtering** in `DnsPacket::write()` — eliminates 3 Vec allocations per response serialization - **Read-only lookups** for cache and overrides — expired entries cleaned up lazily on write path - **Pre-allocated strings** (`String::with_capacity(64)`) in DNS record parsing — avoids reallocation for typical domain names - **Criterion benchmarks** added (`cargo bench`) with hot_path and throughput suites + bench README ## Benchmark results (before → after) | Operation | Before | After | Change | |-----------|--------|-------|--------| | buffer_parse | 534 ns | 449 ns | **-14%** | | buffer_serialize | 358 ns | 339 ns | **-3.5%** | | round_trip_cached | 733 ns | 698 ns | **-3.2%** | | pipeline_throughput/100 | 49.0 µs | 44.0 µs | **-8.5%** | ## Test plan - [x] All 27 existing tests pass - [x] `cargo fmt --check` clean - [x] `cargo clippy -- -D warnings` clean - [x] Criterion benchmarks run successfully with measurable improvement 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.