diff --git a/src/domain-match.c b/src/domain-match.c index d3ab6b3..6e40e8a 100644 --- a/src/domain-match.c +++ b/src/domain-match.c @@ -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; }