feat: PCRE2 JIT

This commit is contained in:
wfjsw 2022-08-25 02:38:05 +08:00
parent 7b094ea84e
commit 0d81b636be
4 changed files with 55 additions and 9 deletions

View File

@ -36,6 +36,8 @@ namespace operators {
VerifyCC::~VerifyCC() {
#if WITH_PCRE2
pcre2_code_free(m_pc);
pcre2_match_context_free(m_pmc);
pcre2_jit_stack_free(m_pcjs);
#else
if (m_pc != NULL) {
pcre_free(m_pc);
@ -105,6 +107,11 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
if (m_pc == NULL) {
return false;
}
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
m_pmc = pcre2_match_context_create(NULL);
m_pcjs = pcre2_jit_stack_create(32*1024, 512*1024, NULL);
pcre2_jit_stack_assign(m_pmc, NULL, m_pcjs);
#else
const char *errptr = NULL;
int erroffset = 0;
@ -142,8 +149,14 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
int ret;
for (offset = 0; offset < target_length; offset++) {
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
if (m_pcje == 0) {
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, m_pmc);
} else {
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, m_pmc);
}
/* If there was no match, then we are done. */
if (ret < 0) {

View File

@ -53,6 +53,9 @@ class VerifyCC : public Operator {
private:
#if WITH_PCRE2
pcre2_code *m_pc;
pcre2_match_context *m_pmc;
int m_pcje;
pcre2_jit_stack *m_pcjs;
#else
pcre *m_pc;
pcre_extra *m_pce;

View File

@ -73,6 +73,11 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
PCRE2_SIZE erroroffset = 0;
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
pcre2_options, &errornumber, &erroroffset, NULL);
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
m_pmc = pcre2_match_context_create(NULL);
m_pcjs = pcre2_jit_stack_create(32*1024, 512*1024, NULL);
pcre2_jit_stack_assign(m_pmc, NULL, m_pcjs);
#else
const char *errptr = NULL;
int erroffset;
@ -92,6 +97,8 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
Regex::~Regex() {
#if WITH_PCRE2
pcre2_code_free(m_pc);
pcre2_match_context_free(m_pmc);
pcre2_jit_stack_free(m_pcjs);
#else
if (m_pc != NULL) {
pcre_free(m_pc);
@ -118,8 +125,13 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
do {
rc = pcre2_match(m_pc, pcre2_s, s.length(),
offset, 0, match_data, NULL);
if (m_pcje == 0) {
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(),
offset, 0, match_data, m_pmc);
} else {
rc = pcre2_match(m_pc, pcre2_s, s.length(),
offset, 0, match_data, m_pmc);
}
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else
const char *subject = s.c_str();
@ -159,7 +171,12 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
#ifdef WITH_PCRE2
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
int rc;
if (m_pcje == 0) {
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
} else {
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
}
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else
const char *subject = s.c_str();
@ -198,7 +215,7 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
}
int rc = pcre2_match(m_pc, pcre2_s, s.length(),
startOffset, pcre2_options, match_data, NULL);
startOffset, pcre2_options, match_data, m_pmc);
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else
@ -270,9 +287,14 @@ int Regex::search(const std::string& s, SMatch *match) const {
#ifdef WITH_PCRE2
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
int ret = pcre2_match(m_pc, pcre2_s, s.length(),
0, 0, match_data, NULL) > 0;
int ret;
if (m_pcje == 0) {
ret = pcre2_match(m_pc, pcre2_s, s.length(),
0, 0, match_data, m_pmc) > 0;
} else {
ret = pcre2_match(m_pc, pcre2_s, s.length(),
0, 0, match_data, m_pmc) > 0;
}
if (ret > 0) { // match
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else
@ -297,7 +319,12 @@ int Regex::search(const std::string& s) const {
#ifdef WITH_PCRE2
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
int rc;
if (m_pcje == 0) {
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
} else {
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
}
pcre2_match_data_free(match_data);
if (rc > 0) {
return 1; // match

View File

@ -85,6 +85,9 @@ class Regex {
private:
#if WITH_PCRE2
pcre2_code *m_pc;
pcre2_match_context *m_pmc;
int m_pcje;
pcre2_jit_stack *m_pcjs;
#else
pcre *m_pc = NULL;
pcre_extra *m_pce = NULL;