1PCREJIT(3)                 Library Functions Manual                 PCREJIT(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE JUST-IN-TIME COMPILER SUPPORT

9
10       Just-in-time  compiling  is a heavyweight optimization that can greatly
11       speed up pattern matching. However, it comes at the cost of extra  pro‐
12       cessing before the match is performed. Therefore, it is of most benefit
13       when the same pattern is going to be matched many times. This does  not
14       necessarily  mean  many calls of a matching function; if the pattern is
15       not anchored, matching attempts may take place many  times  at  various
16       positions  in  the  subject, even for a single call.  Therefore, if the
17       subject string is very long, it may still pay to use  JIT  for  one-off
18       matches.
19
20       JIT  support  applies  only to the traditional Perl-compatible matching
21       function.  It does not apply when the DFA matching  function  is  being
22       used. The code for this support was written by Zoltan Herczeg.
23

8-BIT, 16-BIT AND 32-BIT SUPPORT

25
26       JIT  support  is available for all of the 8-bit, 16-bit and 32-bit PCRE
27       libraries. To keep this documentation simple, only the 8-bit  interface
28       is described in what follows. If you are using the 16-bit library, sub‐
29       stitute the  16-bit  functions  and  16-bit  structures  (for  example,
30       pcre16_jit_stack  instead  of  pcre_jit_stack).  If  you  are using the
31       32-bit library, substitute the 32-bit functions and  32-bit  structures
32       (for example, pcre32_jit_stack instead of pcre_jit_stack).
33

AVAILABILITY OF JIT SUPPORT

35
36       JIT  support  is  an  optional  feature of PCRE. The "configure" option
37       --enable-jit (or equivalent CMake option) must  be  set  when  PCRE  is
38       built  if  you want to use JIT. The support is limited to the following
39       hardware platforms:
40
41         ARM v5, v7, and Thumb2
42         Intel x86 32-bit and 64-bit
43         MIPS 32-bit
44         Power PC 32-bit and 64-bit
45         SPARC 32-bit (experimental)
46
47       If --enable-jit is set on an unsupported platform, compilation fails.
48
49       A program that is linked with PCRE 8.20 or later can tell if  JIT  sup‐
50       port  is  available  by  calling pcre_config() with the PCRE_CONFIG_JIT
51       option. The result is 1 when JIT is available, and  0  otherwise.  How‐
52       ever, a simple program does not need to check this in order to use JIT.
53       The normal API is implemented in a way that falls back to the interpre‐
54       tive code if JIT is not available. For programs that need the best pos‐
55       sible performance, there is also a "fast path"  API  that  is  JIT-spe‐
56       cific.
57
58       If  your program may sometimes be linked with versions of PCRE that are
59       older than 8.20, but you want to use JIT when it is available, you  can
60       test the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT
61       macro such as PCRE_CONFIG_JIT, for compile-time control of your code.
62

SIMPLE USE OF JIT

64
65       You have to do two things to make use of the JIT support  in  the  sim‐
66       plest way:
67
68         (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for
69             each compiled pattern, and pass the resulting pcre_extra block to
70             pcre_exec().
71
72         (2) Use pcre_free_study() to free the pcre_extra block when it is
73             no  longer  needed,  instead  of  just  freeing it yourself. This
74       ensures that
75             any JIT data is also freed.
76
77       For a program that may be linked with pre-8.20 versions  of  PCRE,  you
78       can insert
79
80         #ifndef PCRE_STUDY_JIT_COMPILE
81         #define PCRE_STUDY_JIT_COMPILE 0
82         #endif
83
84       so  that  no  option  is passed to pcre_study(), and then use something
85       like this to free the study data:
86
87         #ifdef PCRE_CONFIG_JIT
88             pcre_free_study(study_ptr);
89         #else
90             pcre_free(study_ptr);
91         #endif
92
93       PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate  code  for
94       complete  matches.  If  you  want  to  run  partial  matches  using the
95       PCRE_PARTIAL_HARD or  PCRE_PARTIAL_SOFT  options  of  pcre_exec(),  you
96       should  set  one  or  both  of the following options in addition to, or
97       instead of, PCRE_STUDY_JIT_COMPILE when you call pcre_study():
98
99         PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
100         PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
101
102       The JIT compiler generates different optimized code  for  each  of  the
103       three  modes  (normal, soft partial, hard partial). When pcre_exec() is
104       called, the appropriate code is run if it is available. Otherwise,  the
105       pattern is matched using interpretive code.
106
107       In  some circumstances you may need to call additional functions. These
108       are described in the  section  entitled  "Controlling  the  JIT  stack"
109       below.
110
111       If  JIT  support  is  not  available,  PCRE_STUDY_JIT_COMPILE  etc. are
112       ignored, and no JIT data is created. Otherwise, the compiled pattern is
113       passed  to the JIT compiler, which turns it into machine code that exe‐
114       cutes much faster than the normal interpretive code.  When  pcre_exec()
115       is  passed  a  pcre_extra block containing a pointer to JIT code of the
116       appropriate mode (normal or hard/soft  partial),  it  obeys  that  code
117       instead  of  running  the interpreter. The result is identical, but the
118       compiled JIT code runs much faster.
119
120       There are some pcre_exec() options that are not supported for JIT  exe‐
121       cution.  There  are  also  some  pattern  items that JIT cannot handle.
122       Details are given below. In both cases, execution  automatically  falls
123       back  to  the  interpretive  code.  If you want to know whether JIT was
124       actually used for a particular match, you  should  arrange  for  a  JIT
125       callback  function  to  be  set up as described in the section entitled
126       "Controlling the JIT stack" below, even if you do not need to supply  a
127       non-default  JIT stack. Such a callback function is called whenever JIT
128       code is about to be obeyed. If the execution options are not right  for
129       JIT execution, the callback function is not obeyed.
130
131       If  the  JIT  compiler finds an unsupported item, no JIT data is gener‐
132       ated. You can find out if JIT execution is available after  studying  a
133       pattern  by  calling  pcre_fullinfo()  with the PCRE_INFO_JIT option. A
134       result of 1 means that JIT compilation was successful. A  result  of  0
135       means that JIT support is not available, or the pattern was not studied
136       with PCRE_STUDY_JIT_COMPILE etc., or the JIT compiler was not  able  to
137       handle the pattern.
138
139       Once a pattern has been studied, with or without JIT, it can be used as
140       many times as you like for matching different subject strings.
141

UNSUPPORTED OPTIONS AND PATTERN ITEMS

143
144       The only pcre_exec() options that are supported for JIT  execution  are
145       PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK, PCRE_NO_UTF32_CHECK, PCRE_NOT‐
146       BOL,  PCRE_NOTEOL,  PCRE_NOTEMPTY,   PCRE_NOTEMPTY_ATSTART,   PCRE_PAR‐
147       TIAL_HARD, and PCRE_PARTIAL_SOFT.
148
149       The unsupported pattern items are:
150
151         \C             match a single byte; not supported in UTF-8 mode
152         (?Cn)          callouts
153         (*PRUNE)       )
154         (*SKIP)        ) backtracking control verbs
155         (*THEN)        )
156
157       Support for some of these may be added in future.
158

RETURN VALUES FROM JIT EXECUTION

160
161       When  a  pattern  is matched using JIT execution, the return values are
162       the same as those given by the interpretive pcre_exec() code, with  the
163       addition  of  one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means
164       that the memory used for the JIT stack was insufficient. See  "Control‐
165       ling the JIT stack" below for a discussion of JIT stack usage. For com‐
166       patibility with the interpretive pcre_exec() code, no  more  than  two-
167       thirds  of  the ovector argument is used for passing back captured sub‐
168       strings.
169
170       The error code PCRE_ERROR_MATCHLIMIT is returned by  the  JIT  code  if
171       searching  a  very large pattern tree goes on for too long, as it is in
172       the same circumstance when JIT is not used, but the details of  exactly
173       what  is  counted are not the same. The PCRE_ERROR_RECURSIONLIMIT error
174       code is never returned by JIT execution.
175

SAVING AND RESTORING COMPILED PATTERNS

177
178       The code that is generated by the  JIT  compiler  is  architecture-spe‐
179       cific,  and  is also position dependent. For those reasons it cannot be
180       saved (in a file or database) and restored later like the bytecode  and
181       other  data  of  a compiled pattern. Saving and restoring compiled pat‐
182       terns is not something many people do. More detail about this  facility
183       is  given in the pcreprecompile documentation. It should be possible to
184       run pcre_study() on a saved and restored pattern, and thereby  recreate
185       the  JIT  data, but because JIT compilation uses significant resources,
186       it is probably not worth doing this; you might as  well  recompile  the
187       original pattern.
188

CONTROLLING THE JIT STACK

190
191       When the compiled JIT code runs, it needs a block of memory to use as a
192       stack.  By default, it uses 32K on the  machine  stack.  However,  some
193       large   or   complicated  patterns  need  more  than  this.  The  error
194       PCRE_ERROR_JIT_STACKLIMIT is given when  there  is  not  enough  stack.
195       Three  functions  are provided for managing blocks of memory for use as
196       JIT stacks. There is further discussion about the use of JIT stacks  in
197       the section entitled "JIT stack FAQ" below.
198
199       The  pcre_jit_stack_alloc() function creates a JIT stack. Its arguments
200       are a starting size and a maximum size, and it returns a pointer to  an
201       opaque  structure of type pcre_jit_stack, or NULL if there is an error.
202       The pcre_jit_stack_free() function can be used to free a stack that  is
203       no  longer  needed.  (For  the technically minded: the address space is
204       allocated by mmap or VirtualAlloc.)
205
206       JIT uses far less memory for recursion than the interpretive code,  and
207       a  maximum  stack size of 512K to 1M should be more than enough for any
208       pattern.
209
210       The pcre_assign_jit_stack() function specifies  which  stack  JIT  code
211       should use. Its arguments are as follows:
212
213         pcre_extra         *extra
214         pcre_jit_callback  callback
215         void               *data
216
217       The  extra  argument  must  be  the  result  of studying a pattern with
218       PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the
219       other two options:
220
221         (1) If callback is NULL and data is NULL, an internal 32K block
222             on the machine stack is used.
223
224         (2) If callback is NULL and data is not NULL, data must be
225             a valid JIT stack, the result of calling pcre_jit_stack_alloc().
226
227         (3) If callback is not NULL, it must point to a function that is
228             called with data as an argument at the start of matching, in
229             order to set up a JIT stack. If the return from the callback
230             function is NULL, the internal 32K stack is used; otherwise the
231             return value must be a valid JIT stack, the result of calling
232             pcre_jit_stack_alloc().
233
234       A  callback function is obeyed whenever JIT code is about to be run; it
235       is not obeyed when pcre_exec() is called with options that  are  incom‐
236       patible for JIT execution. A callback function can therefore be used to
237       determine whether a match operation was  executed  by  JIT  or  by  the
238       interpreter.
239
240       You may safely use the same JIT stack for more than one pattern (either
241       by assigning directly or by callback), as long as the patterns are  all
242       matched  sequentially in the same thread. In a multithread application,
243       if you do not specify a JIT stack, or if you assign or pass  back  NULL
244       from  a  callback, that is thread-safe, because each thread has its own
245       machine stack. However, if you assign  or  pass  back  a  non-NULL  JIT
246       stack,  this  must  be  a  different  stack for each thread so that the
247       application is thread-safe.
248
249       Strictly speaking, even more is allowed. You can assign the  same  non-
250       NULL  stack  to any number of patterns as long as they are not used for
251       matching by multiple threads at the same time.  For  example,  you  can
252       assign  the same stack to all compiled patterns, and use a global mutex
253       in the callback to wait until the stack is available for use.  However,
254       this is an inefficient solution, and not recommended.
255
256       This  is a suggestion for how a multithreaded program that needs to set
257       up non-default JIT stacks might operate:
258
259         During thread initalization
260           thread_local_var = pcre_jit_stack_alloc(...)
261
262         During thread exit
263           pcre_jit_stack_free(thread_local_var)
264
265         Use a one-line callback function
266           return thread_local_var
267
268       All the functions described in this section do nothing if  JIT  is  not
269       available,  and  pcre_assign_jit_stack()  does nothing unless the extra
270       argument is non-NULL and points to  a  pcre_extra  block  that  is  the
271       result of a successful study with PCRE_STUDY_JIT_COMPILE etc.
272

JIT STACK FAQ

274
275       (1) Why do we need JIT stacks?
276
277       PCRE  (and JIT) is a recursive, depth-first engine, so it needs a stack
278       where the local data of the current node is pushed before checking  its
279       child nodes.  Allocating real machine stack on some platforms is diffi‐
280       cult. For example, the stack chain needs to be updated every time if we
281       extend  the  stack  on  PowerPC.  Although it is possible, its updating
282       time overhead decreases performance. So we do the recursion in memory.
283
284       (2) Why don't we simply allocate blocks of memory with malloc()?
285
286       Modern operating systems have a  nice  feature:  they  can  reserve  an
287       address space instead of allocating memory. We can safely allocate mem‐
288       ory pages inside this address space, so the stack  could  grow  without
289       moving memory data (this is important because of pointers). Thus we can
290       allocate 1M address space, and use only a single memory  page  (usually
291       4K)  if  that is enough. However, we can still grow up to 1M anytime if
292       needed.
293
294       (3) Who "owns" a JIT stack?
295
296       The owner of the stack is the user program, not the JIT studied pattern
297       or  anything else. The user program must ensure that if a stack is used
298       by pcre_exec(), (that is, it is assigned to the pattern currently  run‐
299       ning), that stack must not be used by any other threads (to avoid over‐
300       writing the same memory area). The best practice for multithreaded pro‐
301       grams  is  to  allocate  a stack for each thread, and return this stack
302       through the JIT callback function.
303
304       (4) When should a JIT stack be freed?
305
306       You can free a JIT stack at any time, as long as it will not be used by
307       pcre_exec()  again.  When  you  assign  the  stack to a pattern, only a
308       pointer is set. There is no reference counting or any other magic.  You
309       can  free  the  patterns  and stacks in any order, anytime. Just do not
310       call pcre_exec() with a pattern pointing to an already freed stack,  as
311       that  will cause SEGFAULT. (Also, do not free a stack currently used by
312       pcre_exec() in another thread). You can also replace the  stack  for  a
313       pattern  at  any  time.  You  can  even  free the previous stack before
314       assigning a replacement.
315
316       (5) Should I allocate/free a  stack  every  time  before/after  calling
317       pcre_exec()?
318
319       No,  because  this  is  too  costly in terms of resources. However, you
320       could implement some clever idea which release the stack if it  is  not
321       used  in  let's  say  two minutes. The JIT callback can help to achieve
322       this without keeping a list of the currently JIT studied patterns.
323
324       (6) OK, the stack is for long term memory allocation. But what  happens
325       if  a pattern causes stack overflow with a stack of 1M? Is that 1M kept
326       until the stack is freed?
327
328       Especially on embedded sytems, it might be a good idea to release  mem‐
329       ory  sometimes  without  freeing the stack. There is no API for this at
330       the moment.  Probably a function call which returns with the  currently
331       allocated  memory for any stack and another which allows releasing mem‐
332       ory (shrinking the stack) would be a good idea if someone needs this.
333
334       (7) This is too much of a headache. Isn't there any better solution for
335       JIT stack handling?
336
337       No,  thanks to Windows. If POSIX threads were used everywhere, we could
338       throw out this complicated API.
339

EXAMPLE CODE

341
342       This is a single-threaded example that specifies a  JIT  stack  without
343       using a callback.
344
345         int rc;
346         int ovector[30];
347         pcre *re;
348         pcre_extra *extra;
349         pcre_jit_stack *jit_stack;
350
351         re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
352         /* Check for errors */
353         extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
354         jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
355         /* Check for error (NULL) */
356         pcre_assign_jit_stack(extra, NULL, jit_stack);
357         rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
358         /* Check results */
359         pcre_free(re);
360         pcre_free_study(extra);
361         pcre_jit_stack_free(jit_stack);
362
363

JIT FAST PATH API

365
366       Because  the  API  described  above falls back to interpreted execution
367       when JIT is not available, it is convenient for programs that are writ‐
368       ten  for  general  use  in  many environments. However, calling JIT via
369       pcre_exec() does have a performance impact. Programs that  are  written
370       for  use  where  JIT  is known to be available, and which need the best
371       possible performance, can instead use a "fast path"  API  to  call  JIT
372       execution  directly  instead of calling pcre_exec() (obviously only for
373       patterns that have been successfully studied by JIT).
374
375       The fast path function is called pcre_jit_exec(), and it takes  exactly
376       the  same  arguments  as pcre_exec(), plus one additional argument that
377       must point to a JIT stack. The JIT stack arrangements  described  above
378       do not apply. The return values are the same as for pcre_exec().
379
380       When  you  call  pcre_exec(), as well as testing for invalid options, a
381       number of other sanity checks are performed on the arguments. For exam‐
382       ple,  if  the  subject  pointer  is NULL, or its length is negative, an
383       immediate error is given. Also, unless PCRE_NO_UTF[8|16|32] is  set,  a
384       UTF  subject  string is tested for validity. In the interests of speed,
385       these checks do not happen on the JIT fast path, and if invalid data is
386       passed, the result is undefined.
387
388       Bypassing  the  sanity  checks  and  the  pcre_exec() wrapping can give
389       speedups of more than 10%.
390

SEE ALSO

392
393       pcreapi(3)
394

AUTHOR

396
397       Philip Hazel (FAQ by Zoltan Herczeg)
398       University Computing Service
399       Cambridge CB2 3QH, England.
400

REVISION

402
403       Last updated: 31 October 2012
404       Copyright (c) 1997-2012 University of Cambridge.
405
406
407
408PCRE 8.32                       31 October 2012                     PCREJIT(3)
Impressum