| 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 a |
| 11 |
\fPpcre_exec()\fP; if the pattern is not anchored, matching attempts may take |
matching function; 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. |
| 13 |
\fBpcre_exec()\fP. If the subject string is very long, it may still pay to use |
Therefore, if the subject string is very long, it may still pay to use JIT for |
| 14 |
JIT for one-off matches. |
one-off matches. |
| 15 |
.P |
.P |
| 16 |
JIT support applies only to the traditional matching function, |
JIT support applies only to the traditional Perl-compatible matching function. |
| 17 |
\fBpcre_exec()\fP. It does not apply when \fBpcre_dfa_exec()\fP is being used. |
It does not apply when the DFA matching function is being used. The code for |
| 18 |
The code for this support was written by Zoltan Herczeg. |
this support was written by Zoltan Herczeg. |
| 19 |
|
. |
| 20 |
|
. |
| 21 |
|
.SH "8-BIT and 16-BIT SUPPORT" |
| 22 |
|
.rs |
| 23 |
|
.sp |
| 24 |
|
JIT support is available for both the 8-bit and 16-bit PCRE libraries. To keep |
| 25 |
|
this documentation simple, only the 8-bit interface is described in what |
| 26 |
|
follows. If you are using the 16-bit library, substitute the 16-bit functions |
| 27 |
|
and 16-bit structures (for example, \fIpcre16_jit_stack\fP instead of |
| 28 |
|
\fIpcre_jit_stack\fP). |
| 29 |
. |
. |
| 30 |
. |
. |
| 31 |
.SH "AVAILABILITY OF JIT SUPPORT" |
.SH "AVAILABILITY OF JIT SUPPORT" |
| 42 |
.sp |
.sp |
| 43 |
If --enable-jit is set on an unsupported platform, compilation fails. |
If --enable-jit is set on an unsupported platform, compilation fails. |
| 44 |
.P |
.P |
| 45 |
A program can tell if JIT support is available by calling \fBpcre_config()\fP |
A program that is linked with PCRE 8.20 or later can tell if JIT support is |
| 46 |
with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0 |
available by calling \fBpcre_config()\fP with the PCRE_CONFIG_JIT option. The |
| 47 |
otherwise. However, a simple program does not need to check this in order to |
result is 1 when JIT is available, and 0 otherwise. However, a simple program |
| 48 |
use JIT. The API is implemented in a way that falls back to the ordinary PCRE |
does not need to check this in order to use JIT. The API is implemented in a |
| 49 |
code if JIT is not available. |
way that falls back to the interpretive code if JIT is not available. |
| 50 |
|
.P |
| 51 |
|
If your program may sometimes be linked with versions of PCRE that are older |
| 52 |
|
than 8.20, but you want to use JIT when it is available, you can test |
| 53 |
|
the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT macro such |
| 54 |
|
as PCRE_CONFIG_JIT, for compile-time control of your code. |
| 55 |
. |
. |
| 56 |
. |
. |
| 57 |
.SH "SIMPLE USE OF JIT" |
.SH "SIMPLE USE OF JIT" |
| 64 |
\fBpcre_exec()\fP. |
\fBpcre_exec()\fP. |
| 65 |
.sp |
.sp |
| 66 |
(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 |
| 67 |
no longer needed instead of just freeing it yourself. This |
no longer needed, instead of just freeing it yourself. This |
| 68 |
ensures that any JIT data is also freed. |
ensures that any JIT data is also freed. |
| 69 |
.sp |
.sp |
| 70 |
|
For a program that may be linked with pre-8.20 versions of PCRE, you can insert |
| 71 |
|
.sp |
| 72 |
|
#ifndef PCRE_STUDY_JIT_COMPILE |
| 73 |
|
#define PCRE_STUDY_JIT_COMPILE 0 |
| 74 |
|
#endif |
| 75 |
|
.sp |
| 76 |
|
so that no option is passed to \fBpcre_study()\fP, and then use something like |
| 77 |
|
this to free the study data: |
| 78 |
|
.sp |
| 79 |
|
#ifdef PCRE_CONFIG_JIT |
| 80 |
|
pcre_free_study(study_ptr); |
| 81 |
|
#else |
| 82 |
|
pcre_free(study_ptr); |
| 83 |
|
#endif |
| 84 |
|
.sp |
| 85 |
|
PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate code for complete |
| 86 |
|
matches. If you want to run partial matches using the PCRE_PARTIAL_HARD or |
| 87 |
|
PCRE_PARTIAL_SOFT options of \fBpcre_exec()\fP, you should set one or both of |
| 88 |
|
the following options in addition to, or instead of, PCRE_STUDY_JIT_COMPILE |
| 89 |
|
when you call \fBpcre_study()\fP: |
| 90 |
|
.sp |
| 91 |
|
PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE |
| 92 |
|
PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE |
| 93 |
|
.sp |
| 94 |
|
The JIT compiler generates different optimized code for each of the three |
| 95 |
|
modes (normal, soft partial, hard partial). When \fBpcre_exec()\fP is called, |
| 96 |
|
the appropriate code is run if it is available. Otherwise, the pattern is |
| 97 |
|
matched using interpretive code. |
| 98 |
|
.P |
| 99 |
In some circumstances you may need to call additional functions. These are |
In some circumstances you may need to call additional functions. These are |
| 100 |
described in the section entitled |
described in the section entitled |
| 101 |
.\" HTML <a href="#stackcontrol"> |
.\" HTML <a href="#stackcontrol"> |
| 104 |
.\" |
.\" |
| 105 |
below. |
below. |
| 106 |
.P |
.P |
| 107 |
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 etc. are ignored, and |
| 108 |
data is set up. Otherwise, the compiled pattern is passed to the JIT compiler, |
no JIT data is created. Otherwise, the compiled pattern is passed to the JIT |
| 109 |
which turns it into machine code that executes much faster than the normal |
compiler, which turns it into machine code that executes much faster than the |
| 110 |
interpretive code. When \fBpcre_exec()\fP is passed a \fBpcre_extra\fP block |
normal interpretive code. When \fBpcre_exec()\fP is passed a \fBpcre_extra\fP |
| 111 |
containing a pointer to JIT code, it obeys that instead of the normal code. The |
block containing a pointer to JIT code of the appropriate mode (normal or |
| 112 |
result is identical, but the code runs much faster. |
hard/soft partial), it obeys that code instead of running the interpreter. The |
| 113 |
|
result is identical, but the compiled JIT code runs much faster. |
| 114 |
.P |
.P |
| 115 |
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 |
| 116 |
execution. There are also some pattern items that JIT cannot handle. Details |
execution. There are also some pattern items that JIT cannot handle. Details |
| 117 |
are given below. In both cases, execution automatically falls back to the |
are given below. In both cases, execution automatically falls back to the |
| 118 |
interpretive code. |
interpretive code. If you want to know whether JIT was actually used for a |
| 119 |
|
particular match, you should arrange for a JIT callback function to be set up |
| 120 |
|
as described in the section entitled |
| 121 |
|
.\" HTML <a href="#stackcontrol"> |
| 122 |
|
.\" </a> |
| 123 |
|
"Controlling the JIT stack" |
| 124 |
|
.\" |
| 125 |
|
below, even if you do not need to supply a non-default JIT stack. Such a |
| 126 |
|
callback function is called whenever JIT code is about to be obeyed. If the |
| 127 |
|
execution options are not right for JIT execution, the callback function is not |
| 128 |
|
obeyed. |
| 129 |
.P |
.P |
| 130 |
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 |
| 131 |
can find out if JIT execution is available after studying a pattern by calling |
can find out if JIT execution is available after studying a pattern by calling |
| 132 |
\fBpcre_fullinfo()\fP with the PCRE_INFO_JIT option. A result of 1 means that |
\fBpcre_fullinfo()\fP with the PCRE_INFO_JIT option. A result of 1 means that |
| 133 |
JIT compilationw was successful. A result of 0 means that JIT support is not |
JIT compilation was successful. A result of 0 means that JIT support is not |
| 134 |
available, or the pattern was not studied with PCRE_STUDY_JIT_COMPILE, or the |
available, or the pattern was not studied with PCRE_STUDY_JIT_COMPILE etc., or |
| 135 |
JIT compiler was not able to handle the pattern. |
the JIT compiler was not able to handle the pattern. |
| 136 |
|
.P |
| 137 |
|
Once a pattern has been studied, with or without JIT, it can be used as many |
| 138 |
|
times as you like for matching different subject strings. |
| 139 |
. |
. |
| 140 |
. |
. |
| 141 |
.SH "UNSUPPORTED OPTIONS AND PATTERN ITEMS" |
.SH "UNSUPPORTED OPTIONS AND PATTERN ITEMS" |
| 142 |
.rs |
.rs |
| 143 |
.sp |
.sp |
| 144 |
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 |
| 145 |
PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and |
PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, |
| 146 |
PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not |
PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. |
|
supported. |
|
| 147 |
.P |
.P |
| 148 |
The unsupported pattern items are: |
The unsupported pattern items are: |
| 149 |
.sp |
.sp |
| 150 |
\eC match a single byte, even in UTF-8 mode |
\eC match a single byte; not supported in UTF-8 mode |
| 151 |
(?Cn) callouts |
(?Cn) callouts |
|
(?(<name>)... conditional test on setting of a named subpattern |
|
|
(?(R)... conditional test on whole pattern recursion |
|
|
(?(Rn)... conditional test on recursion, by number |
|
|
(?(R&name)... conditional test on recursion, by name |
|
| 152 |
(*COMMIT) ) |
(*COMMIT) ) |
| 153 |
(*MARK) ) |
(*MARK) ) |
| 154 |
(*PRUNE) ) the backtracking control verbs |
(*PRUNE) ) the backtracking control verbs |
| 184 |
.rs |
.rs |
| 185 |
.sp |
.sp |
| 186 |
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 |
| 187 |
also position dependent. For those reasons it cannot be saved and restored like |
also position dependent. For those reasons it cannot be saved (in a file or |
| 188 |
the bytecode and other data of a compiled pattern. You should be able run |
database) and restored later like the bytecode and other data of a compiled |
| 189 |
\fBpcre_study()\fP on a saved and restored pattern, and thereby recreate the |
pattern. Saving and restoring compiled patterns is not something many people |
| 190 |
JIT data, but because JIT compilation uses significant resources, it is |
do. More detail about this facility is given in the |
| 191 |
probably not worth doing this. |
.\" HREF |
| 192 |
|
\fBpcreprecompile\fP |
| 193 |
|
.\" |
| 194 |
|
documentation. It should be possible to run \fBpcre_study()\fP on a saved and |
| 195 |
|
restored pattern, and thereby recreate the JIT data, but because JIT |
| 196 |
|
compilation uses significant resources, it is probably not worth doing this; |
| 197 |
|
you might as well recompile the original pattern. |
| 198 |
. |
. |
| 199 |
. |
. |
| 200 |
.\" HTML <a name="stackcontrol"></a> |
.\" HTML <a name="stackcontrol"></a> |
| 205 |
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 |
| 206 |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
| 207 |
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 |
| 208 |
managing blocks of memory for use as JIT stacks. |
managing blocks of memory for use as JIT stacks. There is further discussion |
| 209 |
|
about the use of JIT stacks in the section entitled |
| 210 |
|
.\" HTML <a href="#stackcontrol"> |
| 211 |
|
.\" </a> |
| 212 |
|
"JIT stack FAQ" |
| 213 |
|
.\" |
| 214 |
|
below. |
| 215 |
.P |
.P |
| 216 |
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 |
| 217 |
are a starting size and a maximum size, and it returns a pointer to an opaque |
are a starting size and a maximum size, and it returns a pointer to an opaque |
| 232 |
void *data |
void *data |
| 233 |
.sp |
.sp |
| 234 |
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 |
| 235 |
PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two |
PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the other |
| 236 |
options: |
two options: |
| 237 |
.sp |
.sp |
| 238 |
(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 |
| 239 |
on the machine stack is used. |
on the machine stack is used. |
| 241 |
(2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be |
(2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be |
| 242 |
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. |
| 243 |
.sp |
.sp |
| 244 |
(3) If \fIcallback\fP not NULL, it must point to a function that is called |
(3) If \fIcallback\fP is not NULL, it must point to a function that is |
| 245 |
with \fIdata\fP as an argument at the start of matching, in order to |
called with \fIdata\fP as an argument at the start of matching, in |
| 246 |
set up a JIT stack. If the result is NULL, the internal 32K stack |
order to set up a JIT stack. If the return from the callback |
| 247 |
is used; otherwise the return value must be a valid JIT stack, |
function is NULL, the internal 32K stack is used; otherwise the |
| 248 |
the result of calling \fBpcre_jit_stack_alloc()\fP. |
return value must be a valid JIT stack, the result of calling |
| 249 |
.sp |
\fBpcre_jit_stack_alloc()\fP. |
| 250 |
|
.sp |
| 251 |
|
A callback function is obeyed whenever JIT code is about to be run; it is not |
| 252 |
|
obeyed when \fBpcre_exec()\fP is called with options that are incompatible for |
| 253 |
|
JIT execution. A callback function can therefore be used to determine whether a |
| 254 |
|
match operation was executed by JIT or by the interpreter. |
| 255 |
|
.P |
| 256 |
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 |
| 257 |
they are all matched sequentially in the same thread. In a multithread |
they are all matched sequentially in the same thread. In a multithread |
| 258 |
application, each thread must use its own JIT stack. |
application, each thread must use its own JIT stack. |
| 278 |
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, |
| 279 |
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 |
| 280 |
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 |
| 281 |
successful study with PCRE_STUDY_JIT_COMPILE. |
successful study with PCRE_STUDY_JIT_COMPILE etc. |
| 282 |
|
. |
| 283 |
|
. |
| 284 |
|
.\" HTML <a name="stackfaq"></a> |
| 285 |
|
.SH "JIT STACK FAQ" |
| 286 |
|
.rs |
| 287 |
|
.sp |
| 288 |
|
(1) Why do we need JIT stacks? |
| 289 |
|
.sp |
| 290 |
|
PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack where |
| 291 |
|
the local data of the current node is pushed before checking its child nodes. |
| 292 |
|
Allocating real machine stack on some platforms is difficult. For example, the |
| 293 |
|
stack chain needs to be updated every time if we extend the stack on PowerPC. |
| 294 |
|
Although it is possible, its updating time overhead decreases performance. So |
| 295 |
|
we do the recursion in memory. |
| 296 |
|
.P |
| 297 |
|
(2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP? |
| 298 |
|
.sp |
| 299 |
|
Modern operating systems have a nice feature: they can reserve an address space |
| 300 |
|
instead of allocating memory. We can safely allocate memory pages inside this |
| 301 |
|
address space, so the stack could grow without moving memory data (this is |
| 302 |
|
important because of pointers). Thus we can allocate 1M address space, and use |
| 303 |
|
only a single memory page (usually 4K) if that is enough. However, we can still |
| 304 |
|
grow up to 1M anytime if needed. |
| 305 |
|
.P |
| 306 |
|
(3) Who "owns" a JIT stack? |
| 307 |
|
.sp |
| 308 |
|
The owner of the stack is the user program, not the JIT studied pattern or |
| 309 |
|
anything else. The user program must ensure that if a stack is used by |
| 310 |
|
\fBpcre_exec()\fP, (that is, it is assigned to the pattern currently running), |
| 311 |
|
that stack must not be used by any other threads (to avoid overwriting the same |
| 312 |
|
memory area). The best practice for multithreaded programs is to allocate a |
| 313 |
|
stack for each thread, and return this stack through the JIT callback function. |
| 314 |
|
.P |
| 315 |
|
(4) When should a JIT stack be freed? |
| 316 |
|
.sp |
| 317 |
|
You can free a JIT stack at any time, as long as it will not be used by |
| 318 |
|
\fBpcre_exec()\fP again. When you assign the stack to a pattern, only a pointer |
| 319 |
|
is set. There is no reference counting or any other magic. You can free the |
| 320 |
|
patterns and stacks in any order, anytime. Just \fIdo not\fP call |
| 321 |
|
\fBpcre_exec()\fP with a pattern pointing to an already freed stack, as that |
| 322 |
|
will cause SEGFAULT. (Also, do not free a stack currently used by |
| 323 |
|
\fBpcre_exec()\fP in another thread). You can also replace the stack for a |
| 324 |
|
pattern at any time. You can even free the previous stack before assigning a |
| 325 |
|
replacement. |
| 326 |
|
.P |
| 327 |
|
(5) Should I allocate/free a stack every time before/after calling |
| 328 |
|
\fBpcre_exec()\fP? |
| 329 |
|
.sp |
| 330 |
|
No, because this is too costly in terms of resources. However, you could |
| 331 |
|
implement some clever idea which release the stack if it is not used in let's |
| 332 |
|
say two minutes. The JIT callback can help to achive this without keeping a |
| 333 |
|
list of the currently JIT studied patterns. |
| 334 |
|
.P |
| 335 |
|
(6) OK, the stack is for long term memory allocation. But what happens if a |
| 336 |
|
pattern causes stack overflow with a stack of 1M? Is that 1M kept until the |
| 337 |
|
stack is freed? |
| 338 |
|
.sp |
| 339 |
|
Especially on embedded sytems, it might be a good idea to release |
| 340 |
|
memory sometimes without freeing the stack. There is no API for this at the |
| 341 |
|
moment. Probably a function call which returns with the currently allocated |
| 342 |
|
memory for any stack and another which allows releasing memory (shrinking the |
| 343 |
|
stack) would be a good idea if someone needs this. |
| 344 |
|
.P |
| 345 |
|
(7) This is too much of a headache. Isn't there any better solution for JIT |
| 346 |
|
stack handling? |
| 347 |
|
.sp |
| 348 |
|
No, thanks to Windows. If POSIX threads were used everywhere, we could throw |
| 349 |
|
out this complicated API. |
| 350 |
. |
. |
| 351 |
. |
. |
| 352 |
.SH "EXAMPLE CODE" |
.SH "EXAMPLE CODE" |
| 385 |
.rs |
.rs |
| 386 |
.sp |
.sp |
| 387 |
.nf |
.nf |
| 388 |
Philip Hazel |
Philip Hazel (FAQ by Zoltan Herczeg) |
| 389 |
University Computing Service |
University Computing Service |
| 390 |
Cambridge CB2 3QH, England. |
Cambridge CB2 3QH, England. |
| 391 |
.fi |
.fi |
| 395 |
.rs |
.rs |
| 396 |
.sp |
.sp |
| 397 |
.nf |
.nf |
| 398 |
Last updated: 06 September 2011 |
Last updated: 22 February 2012 |
| 399 |
Copyright (c) 1997-2011 University of Cambridge. |
Copyright (c) 1997-2012 University of Cambridge. |
| 400 |
.fi |
.fi |