Backport of: https://github.com/PowerDNS/pdns/commit/495f8e5f1f2c147f7431c5a6bfd4f4606b640fe3 From: =?UTF-8?q?Holger=20Hoffst=C3=A4tte?= Date: Thu, 9 Oct 2025 22:04:07 +0200 Subject: [PATCH] dnsdist: add fast path to roundrobin load balancing policy There is no need to collect all servers that are up when the current server is already a good candidate. This avoids needless heap allocation and deallocation in the vast majority of cases. Signed-off-by: Holger Hoffstätte --- a/dnsdist-lbpolicies.cc +++ b/dnsdist-lbpolicies.cc @@ -237,6 +237,14 @@ shared_ptr roundrobin(c return shared_ptr(); } + static std::atomic counter{0}; + + size_t serverIdx = (counter++) % servers.size(); + shared_ptr serverState = servers.at(serverIdx).second; + if (serverState->isUp()) { + return serverState; + } + vector candidates; candidates.reserve(servers.size()); @@ -255,8 +264,7 @@ shared_ptr roundrobin(c } } - static std::atomic counter{0}; - return servers.at(candidates.at((counter++) % candidates.size()) - 1).second; + return servers.at(candidates.at(counter % candidates.size()) - 1).second; } shared_ptr orderedWrandUntag(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dnsq)