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

Commit

Permalink
remove final malloc / new in matching process
Browse files Browse the repository at this point in the history
  • Loading branch information
pes10k committed Feb 25, 2019
1 parent c0b8b20 commit 0fcdfc4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 40 deletions.
52 changes: 20 additions & 32 deletions etld/internal/public_suffix_rule_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sstream>
#include <vector>
#include <map>
#include <iostream>
#include "etld/internal/public_suffix_rule.h"
#include "etld/internal/public_suffix_rule_set.h"
#include "etld/domain.h"
Expand All @@ -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<PublicSuffixRule> &rules) {
const std::vector<PublicSuffixRule>& rules) {
for (auto &elm : rules) {
AddRule(elm);
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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};
}
Expand All @@ -98,57 +99,44 @@ PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match(
return {false, nullptr};
}

std::vector<const PublicSuffixRule*>* matches = new std::vector<const PublicSuffixRule*>();
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<const PublicSuffixRule*>* 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);
}
}

const auto wildcard_result = node->children->find("*");
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);
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions etld/internal/public_suffix_rule_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,19 @@ class PublicSuffixRuleSet {
public:
PublicSuffixRuleSet();
~PublicSuffixRuleSet();
PublicSuffixRuleSet(const std::vector<PublicSuffixRule> &rules);
PublicSuffixRuleSet(const PublicSuffixRuleSet &rule_set);
PublicSuffixRuleSet(const std::vector<PublicSuffixRule>& 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<const PublicSuffixRule*>* 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<PublicSuffixRule*> rules_;
Expand Down

0 comments on commit 0fcdfc4

Please sign in to comment.