Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 0fcdfc4

Browse files
committed
remove final malloc / new in matching process
1 parent c0b8b20 commit 0fcdfc4

2 files changed

Lines changed: 26 additions & 40 deletions

File tree

etld/internal/public_suffix_rule_set.cc

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sstream>
88
#include <vector>
99
#include <map>
10+
#include <iostream>
1011
#include "etld/internal/public_suffix_rule.h"
1112
#include "etld/internal/public_suffix_rule_set.h"
1213
#include "etld/domain.h"
@@ -20,14 +21,14 @@ namespace internal {
2021

2122
PublicSuffixRuleSet::PublicSuffixRuleSet() {}
2223

23-
PublicSuffixRuleSet::PublicSuffixRuleSet(const PublicSuffixRuleSet &rule_set) {
24+
PublicSuffixRuleSet::PublicSuffixRuleSet(const PublicSuffixRuleSet& rule_set) {
2425
for (auto &elm : rule_set.rules_) {
2526
AddRule(*elm);
2627
}
2728
}
2829

2930
PublicSuffixRuleSet::PublicSuffixRuleSet(
30-
const std::vector<PublicSuffixRule> &rules) {
31+
const std::vector<PublicSuffixRule>& rules) {
3132
for (auto &elm : rules) {
3233
AddRule(elm);
3334
}
@@ -37,7 +38,7 @@ PublicSuffixRuleSet::~PublicSuffixRuleSet() {
3738
delete root_;
3839
}
3940

40-
bool PublicSuffixRuleSet::Equal(const PublicSuffixRuleSet &rule_set) const {
41+
bool PublicSuffixRuleSet::Equal(const PublicSuffixRuleSet& rule_set) const {
4142
if (rules_.size() != rule_set.rules_.size()) {
4243
return false;
4344
}
@@ -88,7 +89,7 @@ SerializationResult PublicSuffixRuleSet::Serialize() const {
8889
}
8990

9091
PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match(
91-
const Domain &domain) const {
92+
const Domain& domain) const {
9293
if (root_ == nullptr) {
9394
return {false, nullptr};
9495
}
@@ -98,57 +99,44 @@ PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match(
9899
return {false, nullptr};
99100
}
100101

101-
std::vector<const PublicSuffixRule*>* matches = new std::vector<const PublicSuffixRule*>();
102-
MatchRecursions(domain, domain_length - 1, matches, root_);
102+
const PublicSuffixRule* match = nullptr;
103+
MatchRecursions(domain, domain_length - 1, &match, root_);
103104

104-
if (matches->size() == 0) {
105-
delete matches;
105+
if (match == nullptr) {
106106
return {false, nullptr};
107107
}
108108

109-
size_t longest_length = 0;
110-
size_t found_length;
111-
const PublicSuffixRule* longest_match_result = nullptr;
112-
for (auto &elm : *matches) {
113-
found_length = elm->Length();
114-
if (found_length > longest_length) {
115-
longest_match_result = elm;
116-
longest_length = found_length;
117-
}
118-
}
119-
120-
delete matches;
121-
if (longest_match_result == nullptr) {
122-
return {false, nullptr};
123-
}
124-
125-
return {true, longest_match_result};
109+
return {true, match};
126110
}
127111

128-
void PublicSuffixRuleSet::MatchRecursions(const Domain &domain,
129-
const size_t label_index, std::vector<const PublicSuffixRule*>* matches,
130-
PublicSuffixRuleMapNode * node) const {
112+
void PublicSuffixRuleSet::MatchRecursions(const Domain& domain,
113+
const size_t label_index, const PublicSuffixRule** match,
114+
PublicSuffixRuleMapNode* node) const {
131115
const Label& current_label = domain.Get(label_index);
132116

133117
const auto label_result = node->children->find(current_label);
134118
if (label_result != node->children->end()) {
135119
PublicSuffixRuleMapNode* child_node = node->children->at(current_label);
136120
if (child_node->rule != nullptr) {
137-
matches->push_back(child_node->rule);
121+
if (*match == nullptr || child_node->rule->Length() > (*match)->Length()) {
122+
*match = child_node->rule;
123+
}
138124
}
139125
if (label_index > 0) {
140-
MatchRecursions(domain, label_index - 1, matches, child_node);
126+
MatchRecursions(domain, label_index - 1, match, child_node);
141127
}
142128
}
143129

144130
const auto wildcard_result = node->children->find("*");
145131
if (wildcard_result != node->children->end()) {
146132
PublicSuffixRuleMapNode* child_node = node->children->at("*");
147133
if (child_node->rule != nullptr) {
148-
matches->push_back(child_node->rule);
134+
if (*match == nullptr || child_node->rule->Length() > (*match)->Length()) {
135+
*match = child_node->rule;
136+
}
149137
}
150138
if (label_index > 0) {
151-
MatchRecursions(domain, label_index - 1, matches, child_node);
139+
MatchRecursions(domain, label_index - 1, match, child_node);
152140
}
153141
}
154142
}

etld/internal/public_suffix_rule_set.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,19 @@ class PublicSuffixRuleSet {
4444
public:
4545
PublicSuffixRuleSet();
4646
~PublicSuffixRuleSet();
47-
PublicSuffixRuleSet(const std::vector<PublicSuffixRule> &rules);
48-
PublicSuffixRuleSet(const PublicSuffixRuleSet &rule_set);
47+
PublicSuffixRuleSet(const std::vector<PublicSuffixRule>& rules);
48+
PublicSuffixRuleSet(const PublicSuffixRuleSet& rule_set);
4949

5050
SerializationResult Serialize() const;
51-
bool Equal(const PublicSuffixRuleSet &rule_set) const;
52-
PublicSuffixRuleSetMatchResult Match(const Domain &domain) const;
51+
bool Equal(const PublicSuffixRuleSet& rule_set) const;
52+
PublicSuffixRuleSetMatchResult Match(const Domain& domain) const;
5353
void AddRule(const PublicSuffixRule& rule);
5454

5555
private:
5656
void AddRule(const PublicSuffixRule& rule, const size_t label_index,
5757
PublicSuffixRuleMapNode* node);
58-
void MatchRecursions(const Domain &domain,
59-
const size_t label_index,
60-
std::vector<const PublicSuffixRule*>* matches,
61-
PublicSuffixRuleMapNode * node) const;
58+
void MatchRecursions(const Domain &domain, const size_t label_index,
59+
const PublicSuffixRule** match, PublicSuffixRuleMapNode* node) const;
6260

6361
PublicSuffixRuleMapNode* root_ = nullptr;
6462
std::vector<PublicSuffixRule*> rules_;

0 commit comments

Comments
 (0)