1PCREJIT(3) Library Functions Manual PCREJIT(3)
2
3
4
6 PCRE - Perl-compatible regular expressions
7
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
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
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 op‐
51 tion. The result is 1 when JIT is available, and 0 otherwise. However,
52 a simple program does not need to check this in order to use JIT. The
53 normal API is implemented in a way that falls back to the interpretive
54 code if JIT is not available. For programs that need the best possible
55 performance, there is also a "fast path" API that is JIT-specific.
56
57 If your program may sometimes be linked with versions of PCRE that are
58 older than 8.20, but you want to use JIT when it is available, you can
59 test the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT
60 macro such as PCRE_CONFIG_JIT, for compile-time control of your code.
61 Also beware that the pcre_jit_exec() function was not available at all
62 before 8.32, and may not be available at all if PCRE isn't compiled
63 with --enable-jit. See the "JIT FAST PATH API" section below for de‐
64 tails.
65
67
68 You have to do two things to make use of the JIT support in the sim‐
69 plest way:
70
71 (1) Call pcre_study() with the PCRE_STUDY_JIT_COMPILE option for
72 each compiled pattern, and pass the resulting pcre_extra block to
73 pcre_exec().
74
75 (2) Use pcre_free_study() to free the pcre_extra block when it is
76 no longer needed, instead of just freeing it yourself. This en‐
77 sures that
78 any JIT data is also freed.
79
80 For a program that may be linked with pre-8.20 versions of PCRE, you
81 can insert
82
83 #ifndef PCRE_STUDY_JIT_COMPILE
84 #define PCRE_STUDY_JIT_COMPILE 0
85 #endif
86
87 so that no option is passed to pcre_study(), and then use something
88 like this to free the study data:
89
90 #ifdef PCRE_CONFIG_JIT
91 pcre_free_study(study_ptr);
92 #else
93 pcre_free(study_ptr);
94 #endif
95
96 PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate code for
97 complete matches. If you want to run partial matches using the
98 PCRE_PARTIAL_HARD or PCRE_PARTIAL_SOFT options of pcre_exec(), you
99 should set one or both of the following options in addition to, or in‐
100 stead of, PCRE_STUDY_JIT_COMPILE when you call pcre_study():
101
102 PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE
103 PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE
104
105 If using pcre_jit_exec() and supporting a pre-8.32 version of PCRE, you
106 can insert:
107
108 #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
109 pcre_jit_exec(...);
110 #else
111 pcre_exec(...)
112 #endif
113
114 but as described in the "JIT FAST PATH API" section below this assumes
115 version 8.32 and later are compiled with --enable-jit, which may break.
116
117 The JIT compiler generates different optimized code for each of the
118 three modes (normal, soft partial, hard partial). When pcre_exec() is
119 called, the appropriate code is run if it is available. Otherwise, the
120 pattern is matched using interpretive code.
121
122 In some circumstances you may need to call additional functions. These
123 are described in the section entitled "Controlling the JIT stack" be‐
124 low.
125
126 If JIT support is not available, PCRE_STUDY_JIT_COMPILE etc. are ig‐
127 nored, and no JIT data is created. Otherwise, the compiled pattern is
128 passed to the JIT compiler, which turns it into machine code that exe‐
129 cutes much faster than the normal interpretive code. When pcre_exec()
130 is passed a pcre_extra block containing a pointer to JIT code of the
131 appropriate mode (normal or hard/soft partial), it obeys that code in‐
132 stead of running the interpreter. The result is identical, but the com‐
133 piled JIT code runs much faster.
134
135 There are some pcre_exec() options that are not supported for JIT exe‐
136 cution. There are also some pattern items that JIT cannot handle. De‐
137 tails are given below. In both cases, execution automatically falls
138 back to the interpretive code. If you want to know whether JIT was ac‐
139 tually used for a particular match, you should arrange for a JIT call‐
140 back function to be set up as described in the section entitled "Con‐
141 trolling the JIT stack" below, even if you do not need to supply a non-
142 default JIT stack. Such a callback function is called whenever JIT code
143 is about to be obeyed. If the execution options are not right for JIT
144 execution, the callback function is not obeyed.
145
146 If the JIT compiler finds an unsupported item, no JIT data is gener‐
147 ated. You can find out if JIT execution is available after studying a
148 pattern by calling pcre_fullinfo() with the PCRE_INFO_JIT option. A re‐
149 sult of 1 means that JIT compilation was successful. A result of 0
150 means that JIT support is not available, or the pattern was not studied
151 with PCRE_STUDY_JIT_COMPILE etc., or the JIT compiler was not able to
152 handle the pattern.
153
154 Once a pattern has been studied, with or without JIT, it can be used as
155 many times as you like for matching different subject strings.
156
158
159 The only pcre_exec() options that are supported for JIT execution are
160 PCRE_NO_UTF8_CHECK, PCRE_NO_UTF16_CHECK, PCRE_NO_UTF32_CHECK, PCRE_NOT‐
161 BOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, PCRE_PAR‐
162 TIAL_HARD, and PCRE_PARTIAL_SOFT.
163
164 The only unsupported pattern items are \C (match a single data unit)
165 when running in a UTF mode, and a callout immediately before an asser‐
166 tion condition in a conditional group.
167
169
170 When a pattern is matched using JIT execution, the return values are
171 the same as those given by the interpretive pcre_exec() code, with the
172 addition of one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means
173 that the memory used for the JIT stack was insufficient. See "Control‐
174 ling the JIT stack" below for a discussion of JIT stack usage. For com‐
175 patibility with the interpretive pcre_exec() code, no more than two-
176 thirds of the ovector argument is used for passing back captured sub‐
177 strings.
178
179 The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if
180 searching a very large pattern tree goes on for too long, as it is in
181 the same circumstance when JIT is not used, but the details of exactly
182 what is counted are not the same. The PCRE_ERROR_RECURSIONLIMIT error
183 code is never returned by JIT execution.
184
186
187 The code that is generated by the JIT compiler is architecture-spe‐
188 cific, and is also position dependent. For those reasons it cannot be
189 saved (in a file or database) and restored later like the bytecode and
190 other data of a compiled pattern. Saving and restoring compiled pat‐
191 terns is not something many people do. More detail about this facility
192 is given in the pcreprecompile documentation. It should be possible to
193 run pcre_study() on a saved and restored pattern, and thereby recreate
194 the JIT data, but because JIT compilation uses significant resources,
195 it is probably not worth doing this; you might as well recompile the
196 original pattern.
197
199
200 When the compiled JIT code runs, it needs a block of memory to use as a
201 stack. By default, it uses 32K on the machine stack. However, some
202 large or complicated patterns need more than this. The error PCRE_ER‐
203 ROR_JIT_STACKLIMIT is given when there is not enough stack. Three func‐
204 tions are provided for managing blocks of memory for use as JIT stacks.
205 There is further discussion about the use of JIT stacks in the section
206 entitled "JIT stack FAQ" below.
207
208 The pcre_jit_stack_alloc() function creates a JIT stack. Its arguments
209 are a starting size and a maximum size, and it returns a pointer to an
210 opaque structure of type pcre_jit_stack, or NULL if there is an error.
211 The pcre_jit_stack_free() function can be used to free a stack that is
212 no longer needed. (For the technically minded: the address space is al‐
213 located by mmap or VirtualAlloc.)
214
215 JIT uses far less memory for recursion than the interpretive code, and
216 a maximum stack size of 512K to 1M should be more than enough for any
217 pattern.
218
219 The pcre_assign_jit_stack() function specifies which stack JIT code
220 should use. Its arguments are as follows:
221
222 pcre_extra *extra
223 pcre_jit_callback callback
224 void *data
225
226 The extra argument must be the result of studying a pattern with
227 PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the
228 other two options:
229
230 (1) If callback is NULL and data is NULL, an internal 32K block
231 on the machine stack is used.
232
233 (2) If callback is NULL and data is not NULL, data must be
234 a valid JIT stack, the result of calling pcre_jit_stack_alloc().
235
236 (3) If callback is not NULL, it must point to a function that is
237 called with data as an argument at the start of matching, in
238 order to set up a JIT stack. If the return from the callback
239 function is NULL, the internal 32K stack is used; otherwise the
240 return value must be a valid JIT stack, the result of calling
241 pcre_jit_stack_alloc().
242
243 A callback function is obeyed whenever JIT code is about to be run; it
244 is not obeyed when pcre_exec() is called with options that are incom‐
245 patible for JIT execution. A callback function can therefore be used to
246 determine whether a match operation was executed by JIT or by the in‐
247 terpreter.
248
249 You may safely use the same JIT stack for more than one pattern (either
250 by assigning directly or by callback), as long as the patterns are all
251 matched sequentially in the same thread. In a multithread application,
252 if you do not specify a JIT stack, or if you assign or pass back NULL
253 from a callback, that is thread-safe, because each thread has its own
254 machine stack. However, if you assign or pass back a non-NULL JIT
255 stack, this must be a different stack for each thread so that the ap‐
256 plication is thread-safe.
257
258 Strictly speaking, even more is allowed. You can assign the same non-
259 NULL stack to any number of patterns as long as they are not used for
260 matching by multiple threads at the same time. For example, you can as‐
261 sign the same stack to all compiled patterns, and use a global mutex in
262 the callback to wait until the stack is available for use. However,
263 this is an inefficient solution, and not recommended.
264
265 This is a suggestion for how a multithreaded program that needs to set
266 up non-default JIT stacks might operate:
267
268 During thread initialization
269 thread_local_var = pcre_jit_stack_alloc(...)
270
271 During thread exit
272 pcre_jit_stack_free(thread_local_var)
273
274 Use a one-line callback function
275 return thread_local_var
276
277 All the functions described in this section do nothing if JIT is not
278 available, and pcre_assign_jit_stack() does nothing unless the extra
279 argument is non-NULL and points to a pcre_extra block that is the re‐
280 sult of a successful study with PCRE_STUDY_JIT_COMPILE etc.
281
283
284 (1) Why do we need JIT stacks?
285
286 PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack
287 where the local data of the current node is pushed before checking its
288 child nodes. Allocating real machine stack on some platforms is diffi‐
289 cult. For example, the stack chain needs to be updated every time if we
290 extend the stack on PowerPC. Although it is possible, its updating
291 time overhead decreases performance. So we do the recursion in memory.
292
293 (2) Why don't we simply allocate blocks of memory with malloc()?
294
295 Modern operating systems have a nice feature: they can reserve an ad‐
296 dress space instead of allocating memory. We can safely allocate memory
297 pages inside this address space, so the stack could grow without moving
298 memory data (this is important because of pointers). Thus we can allo‐
299 cate 1M address space, and use only a single memory page (usually 4K)
300 if that is enough. However, we can still grow up to 1M anytime if
301 needed.
302
303 (3) Who "owns" a JIT stack?
304
305 The owner of the stack is the user program, not the JIT studied pattern
306 or anything else. The user program must ensure that if a stack is used
307 by pcre_exec(), (that is, it is assigned to the pattern currently run‐
308 ning), that stack must not be used by any other threads (to avoid over‐
309 writing the same memory area). The best practice for multithreaded pro‐
310 grams is to allocate a stack for each thread, and return this stack
311 through the JIT callback function.
312
313 (4) When should a JIT stack be freed?
314
315 You can free a JIT stack at any time, as long as it will not be used by
316 pcre_exec() again. When you assign the stack to a pattern, only a
317 pointer is set. There is no reference counting or any other magic. You
318 can free the patterns and stacks in any order, anytime. Just do not
319 call pcre_exec() with a pattern pointing to an already freed stack, as
320 that will cause SEGFAULT. (Also, do not free a stack currently used by
321 pcre_exec() in another thread). You can also replace the stack for a
322 pattern at any time. You can even free the previous stack before as‐
323 signing a replacement.
324
325 (5) Should I allocate/free a stack every time before/after calling
326 pcre_exec()?
327
328 No, because this is too costly in terms of resources. However, you
329 could implement some clever idea which release the stack if it is not
330 used in let's say two minutes. The JIT callback can help to achieve
331 this without keeping a list of the currently JIT studied patterns.
332
333 (6) OK, the stack is for long term memory allocation. But what happens
334 if a pattern causes stack overflow with a stack of 1M? Is that 1M kept
335 until the stack is freed?
336
337 Especially on embedded sytems, it might be a good idea to release mem‐
338 ory sometimes without freeing the stack. There is no API for this at
339 the moment. Probably a function call which returns with the currently
340 allocated memory for any stack and another which allows releasing mem‐
341 ory (shrinking the stack) would be a good idea if someone needs this.
342
343 (7) This is too much of a headache. Isn't there any better solution for
344 JIT stack handling?
345
346 No, thanks to Windows. If POSIX threads were used everywhere, we could
347 throw out this complicated API.
348
350
351 This is a single-threaded example that specifies a JIT stack without
352 using a callback.
353
354 int rc;
355 int ovector[30];
356 pcre *re;
357 pcre_extra *extra;
358 pcre_jit_stack *jit_stack;
359
360 re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
361 /* Check for errors */
362 extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
363 jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
364 /* Check for error (NULL) */
365 pcre_assign_jit_stack(extra, NULL, jit_stack);
366 rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
367 /* Check results */
368 pcre_free(re);
369 pcre_free_study(extra);
370 pcre_jit_stack_free(jit_stack);
371
372
374
375 Because the API described above falls back to interpreted execution
376 when JIT is not available, it is convenient for programs that are writ‐
377 ten for general use in many environments. However, calling JIT via
378 pcre_exec() does have a performance impact. Programs that are written
379 for use where JIT is known to be available, and which need the best
380 possible performance, can instead use a "fast path" API to call JIT ex‐
381 ecution directly instead of calling pcre_exec() (obviously only for
382 patterns that have been successfully studied by JIT).
383
384 The fast path function is called pcre_jit_exec(), and it takes exactly
385 the same arguments as pcre_exec(), plus one additional argument that
386 must point to a JIT stack. The JIT stack arrangements described above
387 do not apply. The return values are the same as for pcre_exec().
388
389 When you call pcre_exec(), as well as testing for invalid options, a
390 number of other sanity checks are performed on the arguments. For exam‐
391 ple, if the subject pointer is NULL, or its length is negative, an im‐
392 mediate error is given. Also, unless PCRE_NO_UTF[8|16|32] is set, a UTF
393 subject string is tested for validity. In the interests of speed, these
394 checks do not happen on the JIT fast path, and if invalid data is
395 passed, the result is undefined.
396
397 Bypassing the sanity checks and the pcre_exec() wrapping can give
398 speedups of more than 10%.
399
400 Note that the pcre_jit_exec() function is not available in versions of
401 PCRE before 8.32 (released in November 2012). If you need to support
402 versions that old you must either use the slower pcre_exec(), or switch
403 between the two codepaths by checking the values of PCRE_MAJOR and
404 PCRE_MINOR.
405
406 Due to an unfortunate implementation oversight, even in versions 8.32
407 and later there will be no pcre_jit_exec() stub function defined when
408 PCRE is compiled with --disable-jit, which is the default, and there's
409 no way to detect whether PCRE was compiled with --enable-jit via a
410 macro.
411
412 If you need to support versions older than 8.32, or versions that may
413 not build with --enable-jit, you must either use the slower
414 pcre_exec(), or switch between the two codepaths by checking the values
415 of PCRE_MAJOR and PCRE_MINOR.
416
417 Switching between the two by checking the version assumes that all the
418 versions being targeted are built with --enable-jit. To also support
419 builds that may use --disable-jit either pcre_exec() must be used, or a
420 compile-time check for JIT via pcre_config() (which assumes the runtime
421 environment will be the same), or as the Git project decided to do,
422 simply assume that pcre_jit_exec() is present in 8.32 or later unless a
423 compile-time flag is provided, see the "grep: un-break building with
424 PCRE >= 8.32 without --enable-jit" commit in git.git for an example of
425 that.
426
428
429 pcreapi(3)
430
432
433 Philip Hazel (FAQ by Zoltan Herczeg)
434 University Computing Service
435 Cambridge CB2 3QH, England.
436
438
439 Last updated: 05 July 2017
440 Copyright (c) 1997-2017 University of Cambridge.
441
442
443
444PCRE 8.41 05 July 2017 PCREJIT(3)