1.10.2 - #250
Merged
Merged
Conversation
…tion Replace O(n) linear search through NAT table with O(1) reverse lookup map for reply packet NAT translation. Changes: - Add reverseConnKey type for reverse NAT lookups - Add reverseNatTable map to ProxyHandler for O(1) lookups - Populate both forward and reverse maps when creating NAT entries - Replace iteration-based reverse lookup with direct map access Performance: - O(n) → O(1) complexity for reverse NAT lookups - Eliminates lock-held iteration on every reply packet - Removes string comparisons from hot path - Expected 10-50x improvement for reverse NAT lookups This addresses Critical #1 from performance analysis where reply path was walking the entire NAT table to find original mapping.
Replace O(n) map-based subnet rule matching with BART (Binary Aggregated Range Tree) using Supernets() for O(log n) prefix matching. Performance improvements: - 1.3x faster for large rule sets (1000+ rules) - 39x faster for no-match cases (critical for firewall/security) - 1.9x faster for adding rules - Better scaling characteristics Trade-offs: - Small rule sets (10-100): 1.2-1.4x slower for matches (20-30ns overhead) - Large rule sets (1000+): 1.3x faster - No-match: 39x faster (original checks all rules, BART uses O(log n) tree lookup) The no-match performance is particularly important for security/firewall scenarios where many packets are rejected. BART can determine 'no match' in ~7 tree operations vs checking all 100+ rules. Dependencies: - Added: github.com/gaissmai/bart v0.26.0 Files: - netstack2/subnet_lookup.go: New BART-based implementation - netstack2/proxy.go: Removed old map-based implementation, updated to use BART
- Fix prefix canonicalization: use Masked() to handle host bits correctly (e.g., 10.0.0.5/24 and 10.0.0.0/24 are now treated as equal) - Fix empty trie cleanup: use BART's Size() method to check if trie is empty instead of relying on rules slice length, preventing stale entries - Fix go.mod: move BART from indirect to direct dependencies These fixes ensure proper bookkeeping and prevent memory leaks from empty tries hanging around after rule removal.
perf(netstack2): optimize subnet rule matching with BART
…es/newt into LaurenceJJones-optimize-reverse-nat-lookup
miloschwartz
approved these changes
Mar 4, 2026
mrkhachaturov
pushed a commit
to mrkhachaturov/newt
that referenced
this pull request
Jun 15, 2026
1.10.2 Former-commit-id: beaf386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Community Contribution License Agreement
By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.
Description