Skip to content

Commit

Permalink
v0.11.0-a1 性能优化:
Browse files Browse the repository at this point in the history
- 预编译和缓存部分正则
- 降低 processRemark 的时间复杂度,O(n^2) ~ O(n^3) 降为 O(n)
- 降低 groupGenerate 的时间复杂度,O(n^2) 降为 O(n)
不兼容的功能更改:
- addEmoji 以前当遇到某个 emoji 为空时,会跳过并继续下一个正则匹配,现在
  不会跳过,当匹配成功时会直接停止后续匹配,不添加 emoji
  • Loading branch information
zsokami committed Nov 18, 2024
1 parent 75aa05e commit 27d8267
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 106 deletions.
48 changes: 48 additions & 0 deletions src/config/regmatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,60 @@
#define REGMATCH_H_INCLUDED

#include "def.h"
#include <jpcre2.hpp>
using jp = jpcre2::select<char>;

class RegexWrapper {
private:
std::optional<jp::Regex> _reg;
std::optional<jp::Regex> _reg_non_multiline;
std::optional<jp::Regex> _reg_full_match;
public:
const std::string pattern;
RegexWrapper(const std::string &pattern) : pattern(pattern) {}
const jp::Regex &reg() {
if (!_reg) {
_reg = jp::Regex();
_reg->setPattern(pattern).addModifier("m").addPcre2Option(PCRE2_UTF|PCRE2_ALT_BSUX).compile();
}
return *_reg;
};
const jp::Regex &reg_non_multiline() {
if (!_reg_non_multiline) {
_reg_non_multiline = jp::Regex();
_reg_non_multiline->setPattern(pattern).addPcre2Option(PCRE2_UTF|PCRE2_ALT_BSUX).compile();
}
return *_reg_non_multiline;
};
const jp::Regex &reg_full_match() {
if (!_reg_full_match) {
_reg_full_match = jp::Regex();
_reg_full_match->setPattern(pattern).addModifier("m").addPcre2Option(PCRE2_ANCHORED|PCRE2_ENDANCHORED|PCRE2_UTF).compile();
}
return *_reg_full_match;
};
bool empty() const {
return pattern.empty();
}
};

struct RegexMatchConfig
{
String Match;
String Replace;
String Script;

std::optional<RegexWrapper> target;
std::optional<RegexWrapper> real_rule;
RegexWrapper &reg_wrapper() {
if (!real_rule) {
real_rule = RegexWrapper(Match);
}
return *real_rule;
}
bool empty() {
return reg_wrapper().empty();
}
};

using RegexMatchConfigs = std::vector<RegexMatchConfig>;
Expand Down
43 changes: 21 additions & 22 deletions src/generator/config/nodemanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

extern Settings global;

bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &node);
// bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &node);

int explodeConf(const std::string &filepath, std::vector<Proxy> &nodes)
{
Expand All @@ -35,8 +35,8 @@ void copyNodes(std::vector<Proxy> &source, std::vector<Proxy> &dest)
int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_settings &parse_set)
{
std::string &proxy = *parse_set.proxy, &subInfo = *parse_set.sub_info;
string_array &exclude_remarks = *parse_set.exclude_remarks;
string_array &include_remarks = *parse_set.include_remarks;
RegexMatchConfigs &exclude_remarks = *parse_set.exclude_remarks;
RegexMatchConfigs &include_remarks = *parse_set.include_remarks;
RegexMatchConfigs &stream_rules = *parse_set.stream_rules;
RegexMatchConfigs &time_rules = *parse_set.time_rules;
string_icase_map *request_headers = parse_set.request_header;
Expand Down Expand Up @@ -231,33 +231,31 @@ int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_
return 0;
}

bool chkIgnore(const Proxy &node, string_array &exclude_remarks, string_array &include_remarks)
bool chkIgnore(const Proxy &node, RegexMatchConfigs &exclude_remarks, RegexMatchConfigs &include_remarks)
{
bool excluded = false, included = false;
//std::string remarks = UTF8ToACP(node.remarks);
//std::string remarks = node.remarks;
//writeLog(LOG_TYPE_INFO, "Comparing exclude remarks...");
excluded = std::any_of(exclude_remarks.cbegin(), exclude_remarks.cend(), [&node](const auto &x)
excluded = std::any_of(exclude_remarks.begin(), exclude_remarks.end(), [&node](const auto &x)
{
std::string real_rule;
if(applyMatcher(x, real_rule, node))
if(applyMatcher(x, node))
{
if(real_rule.empty()) return true;
return regFind(node.Remark, real_rule);
if(x.empty()) return true;
return regFind(node.Remark, x);
}
else
return false;
});
if(include_remarks.size() != 0)
{
//writeLog(LOG_TYPE_INFO, "Comparing include remarks...");
included = std::any_of(include_remarks.cbegin(), include_remarks.cend(), [&node](const auto &x)
included = std::any_of(include_remarks.begin(), include_remarks.end(), [&node](const auto &x)
{
std::string real_rule;
if(applyMatcher(x, real_rule, node))
if(applyMatcher(x, node))
{
if(real_rule.empty()) return true;
return regFind(node.Remark, real_rule);
if(x.empty()) return true;
return regFind(node.Remark, x);
}
else
return false;
Expand All @@ -271,7 +269,7 @@ bool chkIgnore(const Proxy &node, string_array &exclude_remarks, string_array &i
return excluded || !included;
}

void filterNodes(std::vector<Proxy> &nodes, string_array &exclude_remarks, string_array &include_remarks, int groupID)
void filterNodes(std::vector<Proxy> &nodes, RegexMatchConfigs &exclude_remarks, RegexMatchConfigs &include_remarks, int groupID)
{
int node_index = 0;
std::vector<Proxy>::iterator iter = nodes.begin();
Expand Down Expand Up @@ -377,7 +375,7 @@ void filterNodes(std::vector<Proxy> &nodes, string_array &exclude_remarks, strin

void nodeRename(Proxy &node, const RegexMatchConfigs &rename_array, extra_settings &ext)
{
std::string &remark = node.Remark, original_remark = node.Remark, returned_remark, real_rule;
std::string &remark = node.Remark, original_remark = node.Remark, returned_remark;

for(const RegexMatchConfig &x : rename_array)
{
Expand All @@ -403,8 +401,8 @@ void nodeRename(Proxy &node, const RegexMatchConfigs &rename_array, extra_settin
}, global.scriptCleanContext);
continue;
}
if(applyMatcher(x.Match, real_rule, node) && real_rule.size())
remark = regReplace(remark, real_rule, x.Replace);
if(applyMatcher(x, node) && !x.empty())
remark = regReplace(remark, x);
}
if(remark.empty())
remark = original_remark;
Expand All @@ -429,7 +427,7 @@ std::string removeEmoji(const std::string &orig_remark)

std::string addEmoji(const Proxy &node, const RegexMatchConfigs &emoji_array, extra_settings &ext)
{
std::string real_rule, ret;
std::string ret;

for(const RegexMatchConfig &x : emoji_array)
{
Expand Down Expand Up @@ -458,10 +456,11 @@ std::string addEmoji(const Proxy &node, const RegexMatchConfigs &emoji_array, ex
return result;
continue;
}
if(x.Replace.empty())
continue;
if(applyMatcher(x.Match, real_rule, node) && real_rule.size() && regFind(node.Remark, real_rule))
if(applyMatcher(x, node) && !x.empty() && regFind(node.Remark, x)) {
if(x.Replace.empty())
return node.Remark;
return x.Replace + " " + node.Remark;
}
}
return node.Remark;
}
Expand Down
9 changes: 5 additions & 4 deletions src/generator/config/nodemanip.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
struct parse_settings
{
std::string *proxy = nullptr;
string_array *exclude_remarks = nullptr;
string_array *include_remarks = nullptr;
RegexMatchConfigs *exclude_remarks = nullptr;
RegexMatchConfigs *include_remarks = nullptr;
RegexMatchConfigs *stream_rules = nullptr;
RegexMatchConfigs *time_rules = nullptr;
std::string *sub_info = nullptr;
Expand All @@ -31,8 +31,9 @@ struct parse_settings
};

int addNodes(std::string link, std::vector<Proxy> &allNodes, int groupID, parse_settings &parse_set);
void filterNodes(std::vector<Proxy> &nodes, string_array &exclude_remarks, string_array &include_remarks, int groupID);
bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &node);
void filterNodes(std::vector<Proxy> &nodes, RegexMatchConfigs &exclude_remarks, RegexMatchConfigs &include_remarks, int groupID);
// bool applyMatcher(const std::string &rule, std::string &real_rule, const Proxy &node);
bool applyMatcher(RegexMatchConfig &config, const Proxy &node);
void preprocessNodes(std::vector<Proxy> &nodes, extra_settings &ext);

#endif // NODEMANIP_H_INCLUDED
Loading

0 comments on commit 27d8267

Please sign in to comment.