From: Remi Gacogne Date: Fri, 3 Jan 2020 17:04:33 +0000 (+0100) Subject: dnsdist: Speed-up of the leastOutstanding policy X-Git-Tag: auth-4.3.0-beta2~1^2~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d122397a15a0600e536772b97c0df4f565f403c;p=thirdparty%2Fpdns.git dnsdist: Speed-up of the leastOutstanding policy --- diff --git a/pdns/dnsdistdist/dnsdist-lbpolicies.cc b/pdns/dnsdistdist/dnsdist-lbpolicies.cc index 095ad9620f..a3175e4e59 100644 --- a/pdns/dnsdistdist/dnsdist-lbpolicies.cc +++ b/pdns/dnsdistdist/dnsdist-lbpolicies.cc @@ -35,19 +35,24 @@ shared_ptr leastOutstanding(const ServerPolicy::NumberedServerV return servers[0].second; } - vector, shared_ptr>> poss; + vector, size_t>> poss; /* so you might wonder, why do we go through this trouble? The data on which we sort could change during the sort, which would suck royally and could even lead to crashes. So first we snapshot on what we sort, and then we sort */ poss.reserve(servers.size()); - for(auto& d : servers) { + size_t position = 0; + for(const auto& d : servers) { if(d.second->isUp()) { - poss.push_back({make_tuple(d.second->outstanding.load(), d.second->order, d.second->latencyUsec), d.second}); + poss.emplace_back(make_tuple(d.second->outstanding.load(), d.second->order, d.second->latencyUsec), position); } + ++position; } - if(poss.empty()) + + if (poss.empty()) { return shared_ptr(); + } + nth_element(poss.begin(), poss.begin(), poss.end(), [](const decltype(poss)::value_type& a, const decltype(poss)::value_type& b) { return a.first < b.first; }); - return poss.begin()->second; + return servers.at(poss.begin()->second).second; } shared_ptr firstAvailable(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq) @@ -66,7 +71,7 @@ static shared_ptr valrandom(unsigned int val, const ServerPolic int sum = 0; int max = std::numeric_limits::max(); - for(auto& d : servers) { // w=1, w=10 -> 1, 11 + for(const auto& d : servers) { // w=1, w=10 -> 1, 11 if(d.second->isUp()) { // Don't overflow sum when adding high weights if(d.second->weight > max - sum) { @@ -184,7 +189,6 @@ shared_ptr roundrobin(const ServerPolicy::NumberedServerVector& return shared_ptr(); static unsigned int counter; - return (*res)[(counter++) % res->size()].second; }