From 0fcdfc498b0f98d250c9afd1e858644a8502b008 Mon Sep 17 00:00:00 2001 From: Peter Snyder Date: Sun, 24 Feb 2019 17:38:26 -0800 Subject: [PATCH] remove final malloc / new in matching process --- etld/internal/public_suffix_rule_set.cc | 52 ++++++++++--------------- etld/internal/public_suffix_rule_set.h | 14 +++---- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/etld/internal/public_suffix_rule_set.cc b/etld/internal/public_suffix_rule_set.cc index 6616d54..cf61468 100644 --- a/etld/internal/public_suffix_rule_set.cc +++ b/etld/internal/public_suffix_rule_set.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include "etld/internal/public_suffix_rule.h" #include "etld/internal/public_suffix_rule_set.h" #include "etld/domain.h" @@ -20,14 +21,14 @@ namespace internal { PublicSuffixRuleSet::PublicSuffixRuleSet() {} -PublicSuffixRuleSet::PublicSuffixRuleSet(const PublicSuffixRuleSet &rule_set) { +PublicSuffixRuleSet::PublicSuffixRuleSet(const PublicSuffixRuleSet& rule_set) { for (auto &elm : rule_set.rules_) { AddRule(*elm); } } PublicSuffixRuleSet::PublicSuffixRuleSet( - const std::vector &rules) { + const std::vector& rules) { for (auto &elm : rules) { AddRule(elm); } @@ -37,7 +38,7 @@ PublicSuffixRuleSet::~PublicSuffixRuleSet() { delete root_; } -bool PublicSuffixRuleSet::Equal(const PublicSuffixRuleSet &rule_set) const { +bool PublicSuffixRuleSet::Equal(const PublicSuffixRuleSet& rule_set) const { if (rules_.size() != rule_set.rules_.size()) { return false; } @@ -88,7 +89,7 @@ SerializationResult PublicSuffixRuleSet::Serialize() const { } PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match( - const Domain &domain) const { + const Domain& domain) const { if (root_ == nullptr) { return {false, nullptr}; } @@ -98,46 +99,31 @@ PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match( return {false, nullptr}; } - std::vector* matches = new std::vector(); - MatchRecursions(domain, domain_length - 1, matches, root_); + const PublicSuffixRule* match = nullptr; + MatchRecursions(domain, domain_length - 1, &match, root_); - if (matches->size() == 0) { - delete matches; + if (match == nullptr) { return {false, nullptr}; } - size_t longest_length = 0; - size_t found_length; - const PublicSuffixRule* longest_match_result = nullptr; - for (auto &elm : *matches) { - found_length = elm->Length(); - if (found_length > longest_length) { - longest_match_result = elm; - longest_length = found_length; - } - } - - delete matches; - if (longest_match_result == nullptr) { - return {false, nullptr}; - } - - return {true, longest_match_result}; + return {true, match}; } -void PublicSuffixRuleSet::MatchRecursions(const Domain &domain, - const size_t label_index, std::vector* matches, - PublicSuffixRuleMapNode * node) const { +void PublicSuffixRuleSet::MatchRecursions(const Domain& domain, + const size_t label_index, const PublicSuffixRule** match, + PublicSuffixRuleMapNode* node) const { const Label& current_label = domain.Get(label_index); const auto label_result = node->children->find(current_label); if (label_result != node->children->end()) { PublicSuffixRuleMapNode* child_node = node->children->at(current_label); if (child_node->rule != nullptr) { - matches->push_back(child_node->rule); + if (*match == nullptr || child_node->rule->Length() > (*match)->Length()) { + *match = child_node->rule; + } } if (label_index > 0) { - MatchRecursions(domain, label_index - 1, matches, child_node); + MatchRecursions(domain, label_index - 1, match, child_node); } } @@ -145,10 +131,12 @@ void PublicSuffixRuleSet::MatchRecursions(const Domain &domain, if (wildcard_result != node->children->end()) { PublicSuffixRuleMapNode* child_node = node->children->at("*"); if (child_node->rule != nullptr) { - matches->push_back(child_node->rule); + if (*match == nullptr || child_node->rule->Length() > (*match)->Length()) { + *match = child_node->rule; + } } if (label_index > 0) { - MatchRecursions(domain, label_index - 1, matches, child_node); + MatchRecursions(domain, label_index - 1, match, child_node); } } } diff --git a/etld/internal/public_suffix_rule_set.h b/etld/internal/public_suffix_rule_set.h index 40e6d35..38d7218 100644 --- a/etld/internal/public_suffix_rule_set.h +++ b/etld/internal/public_suffix_rule_set.h @@ -44,21 +44,19 @@ class PublicSuffixRuleSet { public: PublicSuffixRuleSet(); ~PublicSuffixRuleSet(); - PublicSuffixRuleSet(const std::vector &rules); - PublicSuffixRuleSet(const PublicSuffixRuleSet &rule_set); + PublicSuffixRuleSet(const std::vector& rules); + PublicSuffixRuleSet(const PublicSuffixRuleSet& rule_set); SerializationResult Serialize() const; - bool Equal(const PublicSuffixRuleSet &rule_set) const; - PublicSuffixRuleSetMatchResult Match(const Domain &domain) const; + bool Equal(const PublicSuffixRuleSet& rule_set) const; + PublicSuffixRuleSetMatchResult Match(const Domain& domain) const; void AddRule(const PublicSuffixRule& rule); private: void AddRule(const PublicSuffixRule& rule, const size_t label_index, PublicSuffixRuleMapNode* node); - void MatchRecursions(const Domain &domain, - const size_t label_index, - std::vector* matches, - PublicSuffixRuleMapNode * node) const; + void MatchRecursions(const Domain &domain, const size_t label_index, + const PublicSuffixRule** match, PublicSuffixRuleMapNode* node) const; PublicSuffixRuleMapNode* root_ = nullptr; std::vector rules_;