]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Speed up dnsdist::Protocol() ctor
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 30 Jun 2022 13:27:11 +0000 (15:27 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 27 Sep 2022 08:01:32 +0000 (10:01 +0200)
pdns/dnsdist-protocols.cc
pdns/dnsdist-protocols.hh

index 8af8fe836891a5a58af65e61cca04f744a1f0a5f..e6c47a6441183884812823c33e8a11f6a55b10f9 100644 (file)
@@ -27,7 +27,7 @@
 
 namespace dnsdist
 {
-static const std::vector<std::string> names = {
+const std::vector<std::string> Protocol::s_names = {
   "DoUDP",
   "DoTCP",
   "DNSCryptUDP",
@@ -43,22 +43,14 @@ static const std::vector<std::string> 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<Protocol::typeenum>(index);
 }
 
@@ -74,7 +66,7 @@ bool Protocol::operator!=(Protocol::typeenum type) const
 
 const std::string& Protocol::toString() const
 {
-  return names.at(static_cast<uint8_t>(d_protocol));
+  return s_names.at(static_cast<uint8_t>(d_protocol));
 }
 
 const std::string& Protocol::toPrettyString() const
index ddd74e3688e5856282386ca08a188ca424671c03..981a8860d460ddef45c861db814b515426483105 100644 (file)
@@ -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<std::string> s_names;
 };
 }