mirror of
http://thekelleys.org.uk/git/dnsmasq.git
synced 2026-11-30 21:28:34 +00:00
9e457b11667d135798817c093ee5ea614629aff5
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>
Languages
C
94.3%
Perl
2.2%
HTML
1.2%
Shell
1%
Makefile
0.6%
Other
0.6%