5 |
.rs |
.rs |
6 |
.sp |
.sp |
7 |
Just-in-time compiling is a heavyweight optimization that can greatly speed up |
Just-in-time compiling is a heavyweight optimization that can greatly speed up |
8 |
pattern matching. However, it comes at the cost of extra processing before the |
pattern matching. However, it comes at the cost of extra processing before the |
9 |
match is performed. Therefore, it is of most benefit when the same pattern is |
match is performed. Therefore, it is of most benefit when the same pattern is |
10 |
going to be matched many times. This does not necessarily mean many calls of |
going to be matched many times. This does not necessarily mean many calls of |
11 |
\fPpcre_exec()\fP; if the pattern is not anchored, matching attempts may take |
\fPpcre_exec()\fP; if the pattern is not anchored, matching attempts may take |
12 |
place many times at various positions in the subject, even for a single call to |
place many times at various positions in the subject, even for a single call to |
13 |
\fBpcre_exec()\fP. If the subject string is very long, it may still pay to use |
\fBpcre_exec()\fP. If the subject string is very long, it may still pay to use |
14 |
JIT for one-off matches. |
JIT for one-off matches. |
15 |
.P |
.P |
16 |
JIT support applies only to the traditional matching function, |
JIT support applies only to the traditional matching function, |
17 |
\fBpcre_exec()\fP. It does not apply when \fBpcre_dfa_exec()\fP is being used. |
\fBpcre_exec()\fP. It does not apply when \fBpcre_dfa_exec()\fP is being used. |
18 |
The code for this support was written by Zoltan Herczeg. |
The code for this support was written by Zoltan Herczeg. |
19 |
. |
. |
26 |
JIT. The support is limited to the following hardware platforms: |
JIT. The support is limited to the following hardware platforms: |
27 |
.sp |
.sp |
28 |
ARM v5, v7, and Thumb2 |
ARM v5, v7, and Thumb2 |
29 |
|
Intel x86 32-bit and 64-bit |
30 |
MIPS 32-bit |
MIPS 32-bit |
31 |
Power PC 32-bit and 64-bit |
Power PC 32-bit and 64-bit |
32 |
Intel x86 32-bit and 64-bit |
.sp |
|
.sp |
|
33 |
If --enable-jit is set on an unsupported platform, compilation fails. |
If --enable-jit is set on an unsupported platform, compilation fails. |
34 |
.P |
.P |
35 |
A program can tell if JIT support is available by calling \fBpcre_config()\fP |
A program can tell if JIT support is available by calling \fBpcre_config()\fP |
36 |
with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0 |
with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0 |
37 |
otherwise. However, a simple program does not need to check this in order to |
otherwise. However, a simple program does not need to check this in order to |
38 |
use JIT. The API is implemented in a way that falls back to the ordinary PCRE |
use JIT. The API is implemented in a way that falls back to the ordinary PCRE |
39 |
code if JIT is not available. |
code if JIT is not available. |
47 |
(1) Call \fBpcre_study()\fP with the PCRE_STUDY_JIT_COMPILE option for |
(1) Call \fBpcre_study()\fP with the PCRE_STUDY_JIT_COMPILE option for |
48 |
each compiled pattern, and pass the resulting \fBpcre_extra\fP block to |
each compiled pattern, and pass the resulting \fBpcre_extra\fP block to |
49 |
\fBpcre_exec()\fP. |
\fBpcre_exec()\fP. |
50 |
|
|
51 |
(2) Use \fBpcre_free_study()\fP to free the \fBpcre_extra\fP block when it is |
(2) Use \fBpcre_free_study()\fP to free the \fBpcre_extra\fP block when it is |
52 |
no longer needed instead of just freeing it yourself. This ensures that |
no longer needed instead of just freeing it yourself. This |
53 |
any JIT data is also freed. |
ensures that any JIT data is also freed. |
54 |
.sp |
.sp |
55 |
In some circumstances you may need to call additional functions. These are |
In some circumstances you may need to call additional functions. These are |
56 |
described in the section entitled |
described in the section entitled |
57 |
.\" HTML <a href="#stackcontrol"> |
.\" HTML <a href="#stackcontrol"> |
58 |
.\" </a> |
.\" </a> |
60 |
.\" |
.\" |
61 |
below. |
below. |
62 |
.P |
.P |
63 |
If JIT support is not available, PCRE_STUDY_JIT_COMPILE is ignored, and no JIT |
If JIT support is not available, PCRE_STUDY_JIT_COMPILE is ignored, and no JIT |
64 |
data is set up. Otherwise, the compiled pattern is passed to the JIT compiler, |
data is set up. Otherwise, the compiled pattern is passed to the JIT compiler, |
65 |
which turns it into machine code that executes much faster than the normal |
which turns it into machine code that executes much faster than the normal |
66 |
interpretive code. When \fBpcre_exec()\fP is passed a \fBpcre_extra\fP block |
interpretive code. When \fBpcre_exec()\fP is passed a \fBpcre_extra\fP block |
67 |
containing a pointer to JIT code, it obeys that instead of the normal code. The |
containing a pointer to JIT code, it obeys that instead of the normal code. The |
68 |
result is identical, but the code runs much faster. |
result is identical, but the code runs much faster. |
69 |
.P |
.P |
70 |
There are some \fBpcre_exec()\fP options that are not supported for JIT |
There are some \fBpcre_exec()\fP options that are not supported for JIT |
71 |
execution. There are also some pattern items that JIT cannot handle. Details |
execution. There are also some pattern items that JIT cannot handle. Details |
72 |
are given below. In both cases, execution automatically falls back to the |
are given below. In both cases, execution automatically falls back to the |
73 |
interpretive code. |
interpretive code. |
74 |
.P |
.P |
75 |
If the JIT compiler finds an unsupported item, no JIT data is generated. You |
If the JIT compiler finds an unsupported item, no JIT data is generated. You |
84 |
.rs |
.rs |
85 |
.sp |
.sp |
86 |
The only \fBpcre_exec()\fP options that are supported for JIT execution are |
The only \fBpcre_exec()\fP options that are supported for JIT execution are |
87 |
PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and |
PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and |
88 |
PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not |
PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not |
89 |
supported. |
supported. |
90 |
.P |
.P |
91 |
The unsupported pattern items are: |
The unsupported pattern items are: |
93 |
\eC match a single byte, even in UTF-8 mode |
\eC match a single byte, even in UTF-8 mode |
94 |
(?Cn) callouts |
(?Cn) callouts |
95 |
(?(<name>)... conditional test on setting of a named subpattern |
(?(<name>)... conditional test on setting of a named subpattern |
96 |
(?(R)... conditional test on whole pattern recursion |
(?(R)... conditional test on whole pattern recursion |
97 |
(?(Rn)... conditional test on recursion, by number |
(?(Rn)... conditional test on recursion, by number |
98 |
(?(R&name)... conditional test on recursion, by name |
(?(R&name)... conditional test on recursion, by name |
99 |
(*COMMIT) ) |
(*COMMIT) ) |
101 |
(*PRUNE) ) the backtracking control verbs |
(*PRUNE) ) the backtracking control verbs |
102 |
(*SKIP) ) |
(*SKIP) ) |
103 |
(*THEN) ) |
(*THEN) ) |
104 |
.sp |
.sp |
105 |
Support for some of these may be added in future. |
Support for some of these may be added in future. |
106 |
. |
. |
107 |
. |
. |
108 |
.SH "RETURN VALUES FROM JIT EXECUTION" |
.SH "RETURN VALUES FROM JIT EXECUTION" |
109 |
.rs |
.rs |
110 |
.sp |
.sp |
111 |
When a pattern is matched using JIT execution, the return values are the same |
When a pattern is matched using JIT execution, the return values are the same |
112 |
as those given by the interpretive \fBpcre_exec()\fP code, with the addition of |
as those given by the interpretive \fBpcre_exec()\fP code, with the addition of |
113 |
one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used |
one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used |
114 |
for the JIT stack was insufficient. See |
for the JIT stack was insufficient. See |
115 |
.\" HTML <a href="#stackcontrol"> |
.\" HTML <a href="#stackcontrol"> |
116 |
.\" </a> |
.\" </a> |
117 |
"Controlling the JIT stack" |
"Controlling the JIT stack" |
118 |
.\" |
.\" |
119 |
below for a discussion of JIT stack usage. |
below for a discussion of JIT stack usage. For compatibility with the |
120 |
|
interpretive \fBpcre_exec()\fP code, no more than two-thirds of the |
121 |
|
\fIovector\fP argument is used for passing back captured substrings. |
122 |
.P |
.P |
123 |
The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a |
The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a |
124 |
very large pattern tree goes on for too long, as it is in the same circumstance |
very large pattern tree goes on for too long, as it is in the same circumstance |
130 |
.SH "SAVING AND RESTORING COMPILED PATTERNS" |
.SH "SAVING AND RESTORING COMPILED PATTERNS" |
131 |
.rs |
.rs |
132 |
.sp |
.sp |
133 |
The code that is generated by the JIT compiler is architecture-specific, and is |
The code that is generated by the JIT compiler is architecture-specific, and is |
134 |
also position dependent. For those reasons it cannot be saved and restored like |
also position dependent. For those reasons it cannot be saved and restored like |
135 |
the bytecode and other data of a compiled pattern. You should be able run |
the bytecode and other data of a compiled pattern. You should be able run |
136 |
\fBpcre_study()\fP on a saved and restored pattern, and thereby recreate the |
\fBpcre_study()\fP on a saved and restored pattern, and thereby recreate the |
137 |
JIT data, but because JIT compilation uses significant resources, it is |
JIT data, but because JIT compilation uses significant resources, it is |
138 |
probably not worth doing. |
probably not worth doing this. |
139 |
. |
. |
140 |
. |
. |
141 |
.\" HTML <a name="stackcontrol"></a> |
.\" HTML <a name="stackcontrol"></a> |
142 |
.SH "CONTROLLING THE JIT STACK" |
.SH "CONTROLLING THE JIT STACK" |
143 |
.rs |
.rs |
144 |
.sp |
.sp |
145 |
When the compiled JIT code runs, it needs a block of memory to use as a stack. |
When the compiled JIT code runs, it needs a block of memory to use as a stack. |
146 |
By default, it uses 32K on the machine stack. However, some large or |
By default, it uses 32K on the machine stack. However, some large or |
147 |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
148 |
is given when there is not enough stack. Three functions are provided for |
is given when there is not enough stack. Three functions are provided for |
149 |
setting up alternative blocks of memory for use as JIT stacks. |
managing blocks of memory for use as JIT stacks. |
150 |
.P |
.P |
151 |
The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments |
The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments |
152 |
are a starting size and a maximum size, and it returns an opaque value |
are a starting size and a maximum size, and it returns an opaque value |
153 |
of type \fBpcre_jit_stack\fP that represents a JIT stack, or NULL if there is |
of type \fBpcre_jit_stack\fP that represents a JIT stack, or NULL if there is |
154 |
an error. The \fBpcre_jit_stack_free()\fP function can be used to free a stack |
an error. The \fBpcre_jit_stack_free()\fP function can be used to free a stack |
155 |
that is no longer needed. |
that is no longer needed. (For the technically minded: the address space is |
156 |
|
allocated by mmap or VirtualAlloc.) |
157 |
|
.P |
158 |
|
JIT uses far less memory for recursion than the interpretive code, |
159 |
|
and a maximum stack size of 512K to 1M should be more than enough for any |
160 |
|
pattern. |
161 |
.P |
.P |
162 |
The \fBpcre_assign_jit_stack()\fP function specifies which stack JIT code |
The \fBpcre_assign_jit_stack()\fP function specifies which stack JIT code |
163 |
should use. Its arguments are as follows: |
should use. Its arguments are as follows: |
164 |
.sp |
.sp |
165 |
pcre_extra *extra |
pcre_extra *extra |
166 |
pcre_jit_callback callback |
pcre_jit_callback callback |
167 |
void *data |
void *data |
168 |
.sp |
.sp |
169 |
The \fIextra\fP argument must be the result of studying a pattern with |
The \fIextra\fP argument must be the result of studying a pattern with |
170 |
PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two |
PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two |
171 |
options: |
options: |
172 |
.sp |
.sp |
173 |
(1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block |
(1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block |
177 |
a valid JIT stack, the result of calling \fBpcre_jit_stack_alloc()\fP. |
a valid JIT stack, the result of calling \fBpcre_jit_stack_alloc()\fP. |
178 |
.sp |
.sp |
179 |
(3) If \fIcallback\fP not NULL, it must point to a function that is called |
(3) If \fIcallback\fP not NULL, it must point to a function that is called |
180 |
with \fIdata\fP as an argument at the start of matching, in order to |
with \fIdata\fP as an argument at the start of matching, in order to |
181 |
set up a JIT stack. If the result is NULL, the internal 32K stack |
set up a JIT stack. If the result is NULL, the internal 32K stack |
182 |
is used; otherwise the return value must be a valid JIT stack, |
is used; otherwise the return value must be a valid JIT stack, |
183 |
the result of calling \fBpcre_jit_stack_alloc()\fP. |
the result of calling \fBpcre_jit_stack_alloc()\fP. |
184 |
.sp |
.sp |
185 |
You may safely assign the same JIT stack to more than one pattern, as long as |
You may safely assign the same JIT stack to more than one pattern, as long as |
186 |
they are all matched sequentially in the same thread. In a multithread |
they are all matched sequentially in the same thread. In a multithread |
187 |
application, each thread must use its own JIT stack. |
application, each thread must use its own JIT stack. |
188 |
.P |
.P |
189 |
|
Strictly speaking, even more is allowed. You can assign the same stack to any |
190 |
|
number of patterns as long as they are not used for matching by multiple |
191 |
|
threads at the same time. For example, you can assign the same stack to all |
192 |
|
compiled patterns, and use a global mutex in the callback to wait until the |
193 |
|
stack is available for use. However, this is an inefficient solution, and |
194 |
|
not recommended. |
195 |
|
.P |
196 |
|
This is a suggestion for how a typical multithreaded program might operate: |
197 |
|
.sp |
198 |
|
During thread initalization |
199 |
|
thread_local_var = pcre_jit_stack_alloc(...) |
200 |
|
|
201 |
|
During thread exit |
202 |
|
pcre_jit_stack_free(thread_local_var) |
203 |
|
|
204 |
|
Use a one-line callback function |
205 |
|
return thread_local_var |
206 |
|
.sp |
207 |
All the functions described in this section do nothing if JIT is not available, |
All the functions described in this section do nothing if JIT is not available, |
208 |
and \fBpcre_assign_jit_stack()\fP does nothing unless the \fBextra\fP argument |
and \fBpcre_assign_jit_stack()\fP does nothing unless the \fBextra\fP argument |
209 |
is non-NULL and points to a \fBpcre_extra\fP block that is the result of a |
is non-NULL and points to a \fBpcre_extra\fP block that is the result of a |
210 |
successful study with PCRE_STUDY_JIT_COMPILE. |
successful study with PCRE_STUDY_JIT_COMPILE. |
211 |
. |
. |
212 |
. |
. |
213 |
.SH "EXAMPLE CODE" |
.SH "EXAMPLE CODE" |
214 |
.rs |
.rs |
215 |
.sp |
.sp |
216 |
This is a single-threaded example that specifies a JIT stack without using a |
This is a single-threaded example that specifies a JIT stack without using a |
217 |
callback. |
callback. |
218 |
.sp |
.sp |
219 |
int rc; |
int rc; |
220 |
|
int ovector[30]; |
221 |
pcre *re; |
pcre *re; |
222 |
pcre_extra *extra; |
pcre_extra *extra; |
223 |
pcre_jit_stack *jit_stack; |
pcre_jit_stack *jit_stack; |
224 |
.sp |
.sp |
225 |
re = pcre_compile(pattern, 0, &error, &erroffset, NULL); |
re = pcre_compile(pattern, 0, &error, &erroffset, NULL); |
226 |
/* Check for errors */ |
/* Check for errors */ |
227 |
extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error); |
extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error); |
228 |
jit_stack = pcre_jit_stack_alloc(1, 512 * 1024); |
jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024); |
229 |
/* Check for error (NULL) */ |
/* Check for error (NULL) */ |
230 |
pcre_assign_jit_stack(extra, NULL, jit_stack); |
pcre_assign_jit_stack(extra, NULL, jit_stack); |
231 |
rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, ovecsize); |
rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30); |
232 |
/* Check results */ |
/* Check results */ |
233 |
pcre_free(re); |
pcre_free(re); |
234 |
pcre_free_study(extra); |
pcre_free_study(extra); |
235 |
.sp |
.sp |
236 |
. |
. |
237 |
. |
. |
255 |
.rs |
.rs |
256 |
.sp |
.sp |
257 |
.nf |
.nf |
258 |
Last updated: 28 August 2011 |
Last updated: 06 September 2011 |
259 |
Copyright (c) 1997-2011 University of Cambridge. |
Copyright (c) 1997-2011 University of Cambridge. |
260 |
.fi |
.fi |