From 3b3871dc385ae6dc31ba093c6297f970f443240b Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Thu, 30 Jun 2022 15:27:11 +0200 Subject: [PATCH] dnsdist: Speed up dnsdist::Protocol() ctor --- pdns/dnsdist-protocols.cc | 18 +++++------------- pdns/dnsdist-protocols.hh | 11 ++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pdns/dnsdist-protocols.cc b/pdns/dnsdist-protocols.cc index 8af8fe8368..e6c47a6441 100644 --- a/pdns/dnsdist-protocols.cc +++ b/pdns/dnsdist-protocols.cc @@ -27,7 +27,7 @@ namespace dnsdist { -static const std::vector names = { +const std::vector Protocol::s_names = { "DoUDP", "DoTCP", "DNSCryptUDP", @@ -43,22 +43,14 @@ static const std::vector prettyNames = { "DNS over TLS", "DNS over HTTPS"}; -Protocol::Protocol(Protocol::typeenum protocol) : - d_protocol(protocol) -{ - if (protocol >= names.size()) { - throw std::runtime_error("Unknown protocol: '" + std::to_string(protocol) + "'"); - } -} - Protocol::Protocol(const std::string& s) { - const auto& it = std::find(names.begin(), names.end(), s); - if (it == names.end()) { + const auto& it = std::find(s_names.begin(), s_names.end(), s); + if (it == s_names.end()) { throw std::runtime_error("Unknown protocol name: '" + s + "'"); } - auto index = std::distance(names.begin(), it); + auto index = std::distance(s_names.begin(), it); d_protocol = static_cast(index); } @@ -74,7 +66,7 @@ bool Protocol::operator!=(Protocol::typeenum type) const const std::string& Protocol::toString() const { - return names.at(static_cast(d_protocol)); + return s_names.at(static_cast(d_protocol)); } const std::string& Protocol::toPrettyString() const diff --git a/pdns/dnsdist-protocols.hh b/pdns/dnsdist-protocols.hh index ddd74e3688..981a8860d4 100644 --- a/pdns/dnsdist-protocols.hh +++ b/pdns/dnsdist-protocols.hh @@ -39,7 +39,14 @@ public: DoH }; - Protocol(typeenum protocol = DoUDP); + Protocol(typeenum protocol = DoUDP) : + d_protocol(protocol) + { + if (protocol >= s_names.size()) { + throw std::runtime_error("Unknown protocol: '" + std::to_string(protocol) + "'"); + } + } + explicit Protocol(const std::string& protocol); bool operator==(typeenum) const; @@ -50,5 +57,7 @@ public: private: typeenum d_protocol; + + static const std::vector s_names; }; } -- 2.47.3