perf(proxy): optimize subdomain checking with incremental string building#113
Open
lamtrinhdev wants to merge 1 commit intoivpn:mainfrom
Open
perf(proxy): optimize subdomain checking with incremental string building#113lamtrinhdev wants to merge 1 commit intoivpn:mainfrom
lamtrinhdev wants to merge 1 commit intoivpn:mainfrom
Conversation
…mentally Replace O(n²) strings.Join in loop with O(n) incremental string building. For domains with many subdomains (e.g., a.b.c.d.e.com), this reduces string operations from n*(n+1)/2 to n. Before: strings.Join(parts[i:], ".") in loop creates n+(n-1)+...+1 operations After: Build strings incrementally by prepending parts: n operations
Contributor
Author
|
Dear @MaciejTe and @jurajhilje , I have an update for optimize blocklist. When you have time, please help me to review it. Thanks, |
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.
Replace O(n²) strings.Join in loop with O(n) incremental string building. For domains with many subdomains (e.g., a.b.c.d.e.com), this reduces string operations from n*(n+1)/2 to n.
Before: strings.Join(parts[i:], ".") in loop creates n+(n-1)+...+1 operations
After: Build strings incrementally by prepending parts: n operations
PR type
What kind of change does this PR introduce?
PR checklist
Please check if your PR fulfills the following requirements:
Other information
Optimizes the subdomain checking logic in proxy/filter/blocklists.go by building candidate domains incrementally instead of using strings.Join in a loop. This reduces the complexity from O(n²) to O(n).
Problem
The previous implementation used strings.Join(parts[i:], ".") inside a loop, which creates O(n²) string operations:
For a domain with n parts (e.g., "a.b.c.d.e.com" has 7 parts):
Build candidate domains incrementally by walking backwards and prepending parts:
Before (O(n²)) - Using strings.Join in loop:
Let's trace what strings.Join does internally:
Total: 6 concatenations = 3 + 2 + 1 = O(n²)
After (O(n)) - Building incrementally:
Let's trace the operations:
Total: 3 concatenations = O(n)
Why Does This Matter?
The difference grows exponentially with domain length:
For a domain like a.b.c.d.e.f.g.h.i.j.com (11 parts):