perf(netstack2): optimize subnet rule matching with BART #204
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
Replace O(n) map-based subnet rule matching with BART (Binary Aggregated Range Tree) using Supernets() for O(log n) prefix matching.
Performance improvements:
Trade-offs:
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:
Files:
How to test?
Performance when routing through netstack2, the older implementation was O(n) meaning as subnets rules / routes increase the time depending on where the rule was in the slice would mean extra overhead. Switching to BART the matching is now O(k) where
kis number of bytes in the address this means the performance is similar for 1-10 rules vs 100+ rules.however a important key stats is that lower amount of rules do see a degradation in perf about 20-30ns overhead compared to the former O(n) simply because iterating over 10 rules can be performant, but when taking into the bigger picture and if we want to route maybe lots and lots of different rules then BART is a good choice.