mirror of
http://thekelleys.org.uk/git/dnsmasq.git
synced 2026-11-30 21:28:34 +00:00
Use binary search to find next shorter domain length in lookup_domain()
When a binary search in lookup_domain() misses on a domain of length old, serverarray[try] points to the last entry sorting before qdomain. To find the length of the next shorter domain bucket, the existing code linearly walks serverarray[++try] until domain_len changes. With large local=/ or address=/ blocklists (e.g. 400k+ entries), common domain lengths contain 20k-35k entries each, so every non-exact label lookup linearly scans tens of thousands of scattered struct server pointers. Multi-label misses (such as search-domain retries like a.b.c.lan) repeat this scan once per label. Since serverarray is sorted monotonically non-increasing by domain_len (order()/order_servers(), with SERV_FOR_NODOTS always having domain_len == 0 and sorting last), replace the linear walk with a binary search in O(log N) time. Because qlen strictly decreases on the next outer iteration and qlen <= old whenever rc > 0, no entry with domain_len >= old can match any shorter substring, so advancing low = b_lo when b_lo < daemon->serverarraysz is also safe. On an ARMv8 router (Asus RT-AX89X, IPQ8074A) with 413k local=/ domains: - Blocked subdomain (50 queries): 100 ms -> 10 ms (10x faster) - 4-label .lan suffix retry (50 queries): 300 ms -> 10 ms (30x faster) - 25 unique uncached forwarded queries (50 queries): 190 ms -> 30 ms - Live 15s dnsmasq CPU time: 610 ms -> 10 ms Verified bit-for-bit identical (rc, lowout, highout) against the original implementation across 503,402 differential test cases covering SERV_FOR_NODOTS, SERV_WILDCARD, SERV_USE_RESOLV, F_DS, F_DNSSECOK, F_CONFIG, F_SERVER, F_DOMAINSRV, empty queries, and length gaps. Assisted-by: Gemini Tested-by: sppmasterspp <46223847+sppmasterspp@users.noreply.github.com> Signed-off-by: sppmasterspp <46223847+sppmasterspp@users.noreply.github.com>
This commit is contained in:
1 parent
34b9f9117f
commit
9e457b1166
1 file changed
+18
-8
+18
-8
@@ -177,20 +177,30 @@ int lookup_domain(char *domain, int flags, int *lowout, int *highout)
|
||||
try+1 is not optimal, consider searching bbb in (aaa,ccc,bb). try will point
|
||||
to aaa, since ccc sorts after bbb, but the first domain that has a chance to
|
||||
match is bb. So find the length of the first domain later than try which is
|
||||
is shorter than it.
|
||||
is shorter than it. Because the domains are sorted first by length, we can
|
||||
use a binary search here: On long blocklists, there will be thousands of
|
||||
names with the same length, so a linear search though those blocks would
|
||||
be slow.
|
||||
There's a nasty edge case when qdomain sorts before _any_ of the
|
||||
server domains, where try _doesn't point_ to the last domain that sorts
|
||||
before the query, since no such domain exists. In that case, the loop
|
||||
exits via the rc < 0 && high == try path above and this code is
|
||||
not executed. */
|
||||
ssize_t len, old = daemon->serverarray[try]->domain_len;
|
||||
while (++try != daemon->serverarraysz)
|
||||
size_t old = daemon->serverarray[try]->domain_len;
|
||||
int b_lo = try + 1, b_hi = daemon->serverarraysz, mid;
|
||||
|
||||
while (b_lo < b_hi)
|
||||
{
|
||||
if (old != (len = daemon->serverarray[try]->domain_len))
|
||||
{
|
||||
crop_query = qlen - len;
|
||||
break;
|
||||
}
|
||||
mid = b_lo + (b_hi - b_lo) / 2;
|
||||
if (daemon->serverarray[mid]->domain_len == old)
|
||||
b_lo = mid + 1;
|
||||
else
|
||||
b_hi = mid;
|
||||
}
|
||||
if (b_lo != daemon->serverarraysz)
|
||||
{
|
||||
crop_query = qlen - daemon->serverarray[b_lo]->domain_len;
|
||||
low = b_lo;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user