Use maps for wildcard matching#45
Merged
Merged
Conversation
The previous implementation of wildcard matching iterated through the full list of wildcards looking for one that matched the query name. This made query performance depend on the number of list entries, which is a problem for large lists. By switching to map lookups, query performance now depends only on the number of subdomains in the query. The old implementation also performed string concatenation as part of each wildcard entry check, requiring allocations that hurt performance. The following benchmark results show the difference between the original implementation (Linear), the original implementation without the allocations (LinearNoAlloc), and the new version (Map): goos: linux goarch: amd64 pkg: github.com/wranders/coredns-filter cpu: 12th Gen Intel(R) Core(TM) i7-12700K BenchmarkWildcardListExternal_Linear-20 624 1937816 ns/op 77500 B/op 1566 allocs/op BenchmarkWildcardListExternal_LinearNoAlloc-20 3612 327248 ns/op 2237 B/op 46 allocs/op BenchmarkWildcardListExternal_Map-20 458190 2600 ns/op 2236 B/op 46 allocs/op Removing the allocations improves performance by >5x while switching to the map is a >700x improvement. These tests used the committed "small" OISD list with ~47k entries. The "big" list currently contains 487k entries, so I expect the improvement is larger as the list size grows.
Owner
|
Looks good, thanks for putting this together.
Merging. A new tag and release should be put out soon-ish. |
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.
Changes
Improve performance by using maps to implement wildcard matching. Wildcards behave the same as before, but matching is at least two orders of magnitude faster with large wildcard lists.
Justification
The previous implementation of wildcard matching iterated through the full list of wildcards looking for one that matched the query name. This made query performance depend on the number of list entries, which is a problem for large lists. By switching to map lookups, query performance now depends only on the number of subdomains in the query.
The old implementation also performed string concatenation as part of each wildcard entry check, requiring allocations that hurt performance. The following benchmark results show the difference between the original implementation (
Linear), the original implementation without the allocations (LinearNoAlloc), and the new version (Map):Removing the allocations improves performance by >5x while switching to the map is a >700x improvement. These tests used the committed "small" OISD list with ~47k entries. The "big" list currently contains 487k entries, so I expect the improvement is larger as the list size grows.
Note that the performance of the old version hasn't caused any practical problems for me yet. Rather, while evaluating blocklist/blackhole/sinkhole plugins for CoreDNS, I spotted this optimization. Your plugin otherwise seems to be one of the most flexible and feature-complete versions, so thanks for releasing and maintaining it!