Skip to content

Commit 0d81b63

Browse files
committed
feat: PCRE2 JIT
1 parent 7b094ea commit 0d81b63

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/operators/verify_cc.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace operators {
3636
VerifyCC::~VerifyCC() {
3737
#if WITH_PCRE2
3838
pcre2_code_free(m_pc);
39+
pcre2_match_context_free(m_pmc);
40+
pcre2_jit_stack_free(m_pcjs);
3941
#else
4042
if (m_pc != NULL) {
4143
pcre_free(m_pc);
@@ -105,6 +107,11 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
105107
if (m_pc == NULL) {
106108
return false;
107109
}
110+
111+
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
112+
m_pmc = pcre2_match_context_create(NULL);
113+
m_pcjs = pcre2_jit_stack_create(32*1024, 512*1024, NULL);
114+
pcre2_jit_stack_assign(m_pmc, NULL, m_pcjs);
108115
#else
109116
const char *errptr = NULL;
110117
int erroffset = 0;
@@ -142,8 +149,14 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
142149
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
143150
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
144151

152+
int ret;
145153
for (offset = 0; offset < target_length; offset++) {
146-
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
154+
155+
if (m_pcje == 0) {
156+
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, m_pmc);
157+
} else {
158+
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, m_pmc);
159+
}
147160

148161
/* If there was no match, then we are done. */
149162
if (ret < 0) {

src/operators/verify_cc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class VerifyCC : public Operator {
5353
private:
5454
#if WITH_PCRE2
5555
pcre2_code *m_pc;
56+
pcre2_match_context *m_pmc;
57+
int m_pcje;
58+
pcre2_jit_stack *m_pcjs;
5659
#else
5760
pcre *m_pc;
5861
pcre_extra *m_pce;

src/utils/regex.cc

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
7373
PCRE2_SIZE erroroffset = 0;
7474
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
7575
pcre2_options, &errornumber, &erroroffset, NULL);
76+
77+
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
78+
m_pmc = pcre2_match_context_create(NULL);
79+
m_pcjs = pcre2_jit_stack_create(32*1024, 512*1024, NULL);
80+
pcre2_jit_stack_assign(m_pmc, NULL, m_pcjs);
7681
#else
7782
const char *errptr = NULL;
7883
int erroffset;
@@ -92,6 +97,8 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
9297
Regex::~Regex() {
9398
#if WITH_PCRE2
9499
pcre2_code_free(m_pc);
100+
pcre2_match_context_free(m_pmc);
101+
pcre2_jit_stack_free(m_pcjs);
95102
#else
96103
if (m_pc != NULL) {
97104
pcre_free(m_pc);
@@ -118,8 +125,13 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
118125

119126
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
120127
do {
121-
rc = pcre2_match(m_pc, pcre2_s, s.length(),
122-
offset, 0, match_data, NULL);
128+
if (m_pcje == 0) {
129+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(),
130+
offset, 0, match_data, m_pmc);
131+
} else {
132+
rc = pcre2_match(m_pc, pcre2_s, s.length(),
133+
offset, 0, match_data, m_pmc);
134+
}
123135
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
124136
#else
125137
const char *subject = s.c_str();
@@ -159,7 +171,12 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
159171
#ifdef WITH_PCRE2
160172
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
161173
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
162-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
174+
int rc;
175+
if (m_pcje == 0) {
176+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
177+
} else {
178+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
179+
}
163180
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
164181
#else
165182
const char *subject = s.c_str();
@@ -198,7 +215,7 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
198215
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
199216
}
200217
int rc = pcre2_match(m_pc, pcre2_s, s.length(),
201-
startOffset, pcre2_options, match_data, NULL);
218+
startOffset, pcre2_options, match_data, m_pmc);
202219
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
203220

204221
#else
@@ -270,9 +287,14 @@ int Regex::search(const std::string& s, SMatch *match) const {
270287
#ifdef WITH_PCRE2
271288
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
272289
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
273-
int ret = pcre2_match(m_pc, pcre2_s, s.length(),
274-
0, 0, match_data, NULL) > 0;
275-
290+
int ret;
291+
if (m_pcje == 0) {
292+
ret = pcre2_match(m_pc, pcre2_s, s.length(),
293+
0, 0, match_data, m_pmc) > 0;
294+
} else {
295+
ret = pcre2_match(m_pc, pcre2_s, s.length(),
296+
0, 0, match_data, m_pmc) > 0;
297+
}
276298
if (ret > 0) { // match
277299
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
278300
#else
@@ -297,7 +319,12 @@ int Regex::search(const std::string& s) const {
297319
#ifdef WITH_PCRE2
298320
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
299321
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
300-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
322+
int rc;
323+
if (m_pcje == 0) {
324+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
325+
} else {
326+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, m_pmc);
327+
}
301328
pcre2_match_data_free(match_data);
302329
if (rc > 0) {
303330
return 1; // match

src/utils/regex.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Regex {
8585
private:
8686
#if WITH_PCRE2
8787
pcre2_code *m_pc;
88+
pcre2_match_context *m_pmc;
89+
int m_pcje;
90+
pcre2_jit_stack *m_pcjs;
8891
#else
8992
pcre *m_pc = NULL;
9093
pcre_extra *m_pce = NULL;

0 commit comments

Comments
 (0)