1 |
.TH PCREJIT 3
|
2 |
.SH NAME
|
3 |
PCRE - Perl-compatible regular expressions
|
4 |
.SH "PCRE JUST-IN-TIME COMPILER SUPPORT"
|
5 |
.rs
|
6 |
.sp
|
7 |
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
|
9 |
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
|
11 |
\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
|
13 |
\fBpcre_exec()\fP. If the subject string is very long, it may still pay to use
|
14 |
JIT for one-off matches.
|
15 |
.P
|
16 |
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.
|
18 |
The code for this support was written by Zoltan Herczeg.
|
19 |
.
|
20 |
.
|
21 |
.SH "AVAILABILITY OF JIT SUPPORT"
|
22 |
.rs
|
23 |
.sp
|
24 |
JIT support is an optional feature of PCRE. The "configure" option --enable-jit
|
25 |
(or equivalent CMake option) must be set when PCRE is built if you want to use
|
26 |
JIT. The support is limited to the following hardware platforms:
|
27 |
.sp
|
28 |
ARM v5, v7, and Thumb2
|
29 |
Intel x86 32-bit and 64-bit
|
30 |
MIPS 32-bit
|
31 |
Power PC 32-bit and 64-bit
|
32 |
.sp
|
33 |
If --enable-jit is set on an unsupported platform, compilation fails.
|
34 |
.P
|
35 |
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
|
37 |
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
|
39 |
code if JIT is not available.
|
40 |
.
|
41 |
.
|
42 |
.SH "SIMPLE USE OF JIT"
|
43 |
.rs
|
44 |
.sp
|
45 |
You have to do two things to make use of the JIT support in the simplest way:
|
46 |
.sp
|
47 |
(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
|
49 |
\fBpcre_exec()\fP.
|
50 |
.sp
|
51 |
(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
|
53 |
ensures that any JIT data is also freed.
|
54 |
.sp
|
55 |
In some circumstances you may need to call additional functions. These are
|
56 |
described in the section entitled
|
57 |
.\" HTML <a href="#stackcontrol">
|
58 |
.\" </a>
|
59 |
"Controlling the JIT stack"
|
60 |
.\"
|
61 |
below.
|
62 |
.P
|
63 |
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,
|
65 |
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
|
67 |
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.
|
69 |
.P
|
70 |
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
|
72 |
are given below. In both cases, execution automatically falls back to the
|
73 |
interpretive code.
|
74 |
.P
|
75 |
If the JIT compiler finds an unsupported item, no JIT data is generated. You
|
76 |
can find out if JIT execution is available after studying a pattern by calling
|
77 |
\fBpcre_fullinfo()\fP with the PCRE_INFO_JIT option. A result of 1 means that
|
78 |
JIT compilation was successful. A result of 0 means that JIT support is not
|
79 |
available, or the pattern was not studied with PCRE_STUDY_JIT_COMPILE, or the
|
80 |
JIT compiler was not able to handle the pattern.
|
81 |
.P
|
82 |
Once a pattern has been studied, with or without JIT, it can be used as many
|
83 |
times as you like for matching different subject strings.
|
84 |
.
|
85 |
.
|
86 |
.SH "UNSUPPORTED OPTIONS AND PATTERN ITEMS"
|
87 |
.rs
|
88 |
.sp
|
89 |
The only \fBpcre_exec()\fP options that are supported for JIT execution are
|
90 |
PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and
|
91 |
PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not
|
92 |
supported.
|
93 |
.P
|
94 |
The unsupported pattern items are:
|
95 |
.sp
|
96 |
\eC match a single byte, even in UTF-8 mode
|
97 |
(?Cn) callouts
|
98 |
(?(<name>)... conditional test on setting of a named subpattern
|
99 |
(?(R)... conditional test on whole pattern recursion
|
100 |
(?(Rn)... conditional test on recursion, by number
|
101 |
(?(R&name)... conditional test on recursion, by name
|
102 |
(*COMMIT) )
|
103 |
(*MARK) )
|
104 |
(*PRUNE) ) the backtracking control verbs
|
105 |
(*SKIP) )
|
106 |
(*THEN) )
|
107 |
.sp
|
108 |
Support for some of these may be added in future.
|
109 |
.
|
110 |
.
|
111 |
.SH "RETURN VALUES FROM JIT EXECUTION"
|
112 |
.rs
|
113 |
.sp
|
114 |
When a pattern is matched using JIT execution, the return values are the same
|
115 |
as those given by the interpretive \fBpcre_exec()\fP code, with the addition of
|
116 |
one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used
|
117 |
for the JIT stack was insufficient. See
|
118 |
.\" HTML <a href="#stackcontrol">
|
119 |
.\" </a>
|
120 |
"Controlling the JIT stack"
|
121 |
.\"
|
122 |
below for a discussion of JIT stack usage. For compatibility with the
|
123 |
interpretive \fBpcre_exec()\fP code, no more than two-thirds of the
|
124 |
\fIovector\fP argument is used for passing back captured substrings.
|
125 |
.P
|
126 |
The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a
|
127 |
very large pattern tree goes on for too long, as it is in the same circumstance
|
128 |
when JIT is not used, but the details of exactly what is counted are not the
|
129 |
same. The PCRE_ERROR_RECURSIONLIMIT error code is never returned by JIT
|
130 |
execution.
|
131 |
.
|
132 |
.
|
133 |
.SH "SAVING AND RESTORING COMPILED PATTERNS"
|
134 |
.rs
|
135 |
.sp
|
136 |
The code that is generated by the JIT compiler is architecture-specific, and is
|
137 |
also position dependent. For those reasons it cannot be saved (in a file or
|
138 |
database) and restored later like the bytecode and other data of a compiled
|
139 |
pattern. Saving and restoring compiled patterns is not something many people
|
140 |
do. More detail about this facility is given in the
|
141 |
.\" HREF
|
142 |
\fBpcreprecompile\fP
|
143 |
.\"
|
144 |
documentation. It should be possible to run \fBpcre_study()\fP on a saved and
|
145 |
restored pattern, and thereby recreate the JIT data, but because JIT
|
146 |
compilation uses significant resources, it is probably not worth doing this;
|
147 |
you might as well recompile the original pattern.
|
148 |
.
|
149 |
.
|
150 |
.\" HTML <a name="stackcontrol"></a>
|
151 |
.SH "CONTROLLING THE JIT STACK"
|
152 |
.rs
|
153 |
.sp
|
154 |
When the compiled JIT code runs, it needs a block of memory to use as a stack.
|
155 |
By default, it uses 32K on the machine stack. However, some large or
|
156 |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT
|
157 |
is given when there is not enough stack. Three functions are provided for
|
158 |
managing blocks of memory for use as JIT stacks.
|
159 |
.P
|
160 |
The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments
|
161 |
are a starting size and a maximum size, and it returns a pointer to an opaque
|
162 |
structure of type \fBpcre_jit_stack\fP, or NULL if there is an error. The
|
163 |
\fBpcre_jit_stack_free()\fP function can be used to free a stack that is no
|
164 |
longer needed. (For the technically minded: the address space is allocated by
|
165 |
mmap or VirtualAlloc.)
|
166 |
.P
|
167 |
JIT uses far less memory for recursion than the interpretive code,
|
168 |
and a maximum stack size of 512K to 1M should be more than enough for any
|
169 |
pattern.
|
170 |
.P
|
171 |
The \fBpcre_assign_jit_stack()\fP function specifies which stack JIT code
|
172 |
should use. Its arguments are as follows:
|
173 |
.sp
|
174 |
pcre_extra *extra
|
175 |
pcre_jit_callback callback
|
176 |
void *data
|
177 |
.sp
|
178 |
The \fIextra\fP argument must be the result of studying a pattern with
|
179 |
PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two
|
180 |
options:
|
181 |
.sp
|
182 |
(1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block
|
183 |
on the machine stack is used.
|
184 |
.sp
|
185 |
(2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be
|
186 |
a valid JIT stack, the result of calling \fBpcre_jit_stack_alloc()\fP.
|
187 |
.sp
|
188 |
(3) If \fIcallback\fP not NULL, it must point to a function that is called
|
189 |
with \fIdata\fP as an argument at the start of matching, in order to
|
190 |
set up a JIT stack. If the result is NULL, the internal 32K stack
|
191 |
is used; otherwise the return value must be a valid JIT stack,
|
192 |
the result of calling \fBpcre_jit_stack_alloc()\fP.
|
193 |
.sp
|
194 |
You may safely assign the same JIT stack to more than one pattern, as long as
|
195 |
they are all matched sequentially in the same thread. In a multithread
|
196 |
application, each thread must use its own JIT stack.
|
197 |
.P
|
198 |
Strictly speaking, even more is allowed. You can assign the same stack to any
|
199 |
number of patterns as long as they are not used for matching by multiple
|
200 |
threads at the same time. For example, you can assign the same stack to all
|
201 |
compiled patterns, and use a global mutex in the callback to wait until the
|
202 |
stack is available for use. However, this is an inefficient solution, and
|
203 |
not recommended.
|
204 |
.P
|
205 |
This is a suggestion for how a typical multithreaded program might operate:
|
206 |
.sp
|
207 |
During thread initalization
|
208 |
thread_local_var = pcre_jit_stack_alloc(...)
|
209 |
.sp
|
210 |
During thread exit
|
211 |
pcre_jit_stack_free(thread_local_var)
|
212 |
.sp
|
213 |
Use a one-line callback function
|
214 |
return thread_local_var
|
215 |
.sp
|
216 |
All the functions described in this section do nothing if JIT is not available,
|
217 |
and \fBpcre_assign_jit_stack()\fP does nothing unless the \fBextra\fP argument
|
218 |
is non-NULL and points to a \fBpcre_extra\fP block that is the result of a
|
219 |
successful study with PCRE_STUDY_JIT_COMPILE.
|
220 |
.
|
221 |
.
|
222 |
.SH "EXAMPLE CODE"
|
223 |
.rs
|
224 |
.sp
|
225 |
This is a single-threaded example that specifies a JIT stack without using a
|
226 |
callback.
|
227 |
.sp
|
228 |
int rc;
|
229 |
int ovector[30];
|
230 |
pcre *re;
|
231 |
pcre_extra *extra;
|
232 |
pcre_jit_stack *jit_stack;
|
233 |
.sp
|
234 |
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
|
235 |
/* Check for errors */
|
236 |
extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
|
237 |
jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
|
238 |
/* Check for error (NULL) */
|
239 |
pcre_assign_jit_stack(extra, NULL, jit_stack);
|
240 |
rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
|
241 |
/* Check results */
|
242 |
pcre_free(re);
|
243 |
pcre_free_study(extra);
|
244 |
pcre_jit_stack_free(jit_stack);
|
245 |
.sp
|
246 |
.
|
247 |
.
|
248 |
.SH "SEE ALSO"
|
249 |
.rs
|
250 |
.sp
|
251 |
\fBpcreapi\fP(3)
|
252 |
.
|
253 |
.
|
254 |
.SH AUTHOR
|
255 |
.rs
|
256 |
.sp
|
257 |
.nf
|
258 |
Philip Hazel
|
259 |
University Computing Service
|
260 |
Cambridge CB2 3QH, England.
|
261 |
.fi
|
262 |
.
|
263 |
.
|
264 |
.SH REVISION
|
265 |
.rs
|
266 |
.sp
|
267 |
.nf
|
268 |
Last updated: 23 September 2011
|
269 |
Copyright (c) 1997-2011 University of Cambridge.
|
270 |
.fi
|