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