From ddb7e6c67244ae644c2acfcf58da55bff4a1ff13 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Fri, 23 Oct 2015 15:04:50 +0200 Subject: [PATCH] speed up canonical ordering by a factor of 5, increasing the ugliness of the code by a similar amount --- pdns/dnsname.cc | 11 +++--- pdns/dnsname.hh | 78 ++++++++++++++++++++++++++++++++++++++++- pdns/test-dnsname_cc.cc | 6 +++- 3 files changed, 88 insertions(+), 7 deletions(-) diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index 227363d65..ffea62f2d 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -182,6 +182,12 @@ void DNSName::prependRawLabel(const std::string& label) d_storage = prep+d_storage; } +bool DNSName::slowCanonCompare(const DNSName& rhs) const +{ + auto ours=getRawLabels(), rhsLabels = rhs.getRawLabels(); + return std::lexicographical_compare(ours.rbegin(), ours.rend(), rhsLabels.rbegin(), rhsLabels.rend(), CIStringCompare()); +} + vector DNSName::getRawLabels() const { vector ret; @@ -192,11 +198,6 @@ vector DNSName::getRawLabels() const return ret; } -bool DNSName::canonCompare(const DNSName& rhs) const -{ - auto ours=getRawLabels(), rhsLabels = rhs.getRawLabels(); - return std::lexicographical_compare(ours.rbegin(), ours.rend(), rhsLabels.rbegin(), rhsLabels.rend(), CIStringCompare()); -} bool DNSName::chopOff() { diff --git a/pdns/dnsname.hh b/pdns/dnsname.hh index 82ba34911..b0a571975 100644 --- a/pdns/dnsname.hh +++ b/pdns/dnsname.hh @@ -5,6 +5,7 @@ #include #include #include + // #include "dns.hh" // #include "logger.hh" @@ -77,11 +78,12 @@ public: ar & d_empty; } - bool canonCompare(const DNSName& rhs) const; + inline bool canonCompare(const DNSName& rhs) const; private: // typedef __gnu_cxx::__sso_string string_t; typedef std::string string_t; + bool slowCanonCompare(const DNSName& rhs) const; string_t d_storage; bool d_empty; int d_recurse; @@ -93,6 +95,80 @@ private: size_t hash_value(DNSName const& d); +inline char dns2_tolower(char c) +{ + if(c>='A' && c<='Z') + c+='a'-'A'; + return c; +} + + +inline bool DNSName::canonCompare(const DNSName& rhs) const +{ + // 01234567890abcd + // us: 1a3www4ds9a2nl + // rhs: 3www6online3com + // to compare, we start at the back, is nl < com? no -> done + // + // 0,2,6,a + // 0,4,a + + uint8_t ourpos[64], rhspos[64]; + uint8_t ourcount=0, rhscount=0; + //cout<<"Asked to compare "< { bool operator()(const DNSName&a, const DNSName& b) const diff --git a/pdns/test-dnsname_cc.cc b/pdns/test-dnsname_cc.cc index 5c78943e4..fdfa30160 100644 --- a/pdns/test-dnsname_cc.cc +++ b/pdns/test-dnsname_cc.cc @@ -343,6 +343,9 @@ BOOST_AUTO_TEST_CASE(test_compare_canonical) { DNSName lower("bert.com."), higher("alpha.nl."); BOOST_CHECK(lower.canonCompare(higher)); + BOOST_CHECK(DNSName("bert.com").canonCompare(DNSName("www.bert.com"))); + BOOST_CHECK(DNSName("BeRt.com").canonCompare(DNSName("WWW.berT.com"))); + BOOST_CHECK(!DNSName("www.BeRt.com").canonCompare(DNSName("WWW.berT.com"))); vector vec; for(const std::string& a : {"bert.com.", "alpha.nl.", "articles.xxx.", @@ -352,7 +355,7 @@ BOOST_AUTO_TEST_CASE(test_compare_canonical) { } sort(vec.begin(), vec.end(), CanonDNSNameCompare()); // for(const auto& v : vec) - // cerr<<'"'< right; for(const auto& a: {"bert.com.", "Aleph1.powerdns.com.", @@ -364,6 +367,7 @@ BOOST_AUTO_TEST_CASE(test_compare_canonical) { "yyy.XXX."}) right.push_back(DNSName(a)); + BOOST_CHECK(vec==right); } -- 2.40.0