ViewVC logotype

Contents of /code/trunk/doc/pcrejit.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1765 - (show annotations)
Mon Jun 14 11:21:20 2021 UTC (3 months, 1 week ago) by ph10
File size: 20125 byte(s)
Error occurred while calculating annotation data.
Fix a few small bugs and typos and update docs for a final release.
1 .TH PCREJIT 3 "05 July 2017" "PCRE 8.41"
3 PCRE - Perl-compatible regular expressions
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 a
11 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.
13 Therefore, if the subject string is very long, it may still pay to use JIT for
14 one-off matches.
15 .P
16 JIT support applies only to the traditional Perl-compatible matching function.
17 It does not apply when the DFA matching function is being used. The code for
18 this support was written by Zoltan Herczeg.
19 .
20 .
22 .rs
23 .sp
24 JIT support is available for all of the 8-bit, 16-bit and 32-bit PCRE
25 libraries. To keep this documentation simple, only the 8-bit interface is
26 described in what follows. If you are using the 16-bit library, substitute the
27 16-bit functions and 16-bit structures (for example, \fIpcre16_jit_stack\fP
28 instead of \fIpcre_jit_stack\fP). If you are using the 32-bit library,
29 substitute the 32-bit functions and 32-bit structures (for example,
30 \fIpcre32_jit_stack\fP instead of \fIpcre_jit_stack\fP).
31 .
32 .
34 .rs
35 .sp
36 JIT support is an optional feature of PCRE. The "configure" option --enable-jit
37 (or equivalent CMake option) must be set when PCRE is built if you want to use
38 JIT. The support is limited to the following hardware platforms:
39 .sp
40 ARM v5, v7, and Thumb2
41 Intel x86 32-bit and 64-bit
42 MIPS 32-bit
43 Power PC 32-bit and 64-bit
44 SPARC 32-bit (experimental)
45 .sp
46 If --enable-jit is set on an unsupported platform, compilation fails.
47 .P
48 A program that is linked with PCRE 8.20 or later can tell if JIT support is
49 available by calling \fBpcre_config()\fP with the PCRE_CONFIG_JIT option. The
50 result is 1 when JIT is available, and 0 otherwise. However, a simple program
51 does not need to check this in order to use JIT. The normal API is implemented
52 in a way that falls back to the interpretive code if JIT is not available. For
53 programs that need the best possible performance, there is also a "fast path"
54 API that is JIT-specific.
55 .P
56 If your program may sometimes be linked with versions of PCRE that are older
57 than 8.20, but you want to use JIT when it is available, you can test the
58 values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT macro such as
59 PCRE_CONFIG_JIT, for compile-time control of your code. Also beware that the
60 \fBpcre_jit_exec()\fP function was not available at all before 8.32,
61 and may not be available at all if PCRE isn't compiled with
62 --enable-jit. See the "JIT FAST PATH API" section below for details.
63 .
64 .
66 .rs
67 .sp
68 You have to do two things to make use of the JIT support in the simplest way:
69 .sp
70 (1) Call \fBpcre_study()\fP with the PCRE_STUDY_JIT_COMPILE option for
71 each compiled pattern, and pass the resulting \fBpcre_extra\fP block to
72 \fBpcre_exec()\fP.
73 .sp
74 (2) Use \fBpcre_free_study()\fP to free the \fBpcre_extra\fP block when it is
75 no longer needed, instead of just freeing it yourself. This ensures that
76 any JIT data is also freed.
77 .sp
78 For a program that may be linked with pre-8.20 versions of PCRE, you can insert
79 .sp
82 #endif
83 .sp
84 so that no option is passed to \fBpcre_study()\fP, and then use something like
85 this to free the study data:
86 .sp
88 pcre_free_study(study_ptr);
89 #else
90 pcre_free(study_ptr);
91 #endif
92 .sp
93 PCRE_STUDY_JIT_COMPILE requests the JIT compiler to generate code for complete
94 matches. If you want to run partial matches using the PCRE_PARTIAL_HARD or
95 PCRE_PARTIAL_SOFT options of \fBpcre_exec()\fP, you should set one or both of
96 the following options in addition to, or instead of, PCRE_STUDY_JIT_COMPILE
97 when you call \fBpcre_study()\fP:
98 .sp
101 .sp
102 If using \fBpcre_jit_exec()\fP and supporting a pre-8.32 version of
103 PCRE, you can insert:
104 .sp
105 #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
106 pcre_jit_exec(...);
107 #else
108 pcre_exec(...)
109 #endif
110 .sp
111 but as described in the "JIT FAST PATH API" section below this assumes
112 version 8.32 and later are compiled with --enable-jit, which may
113 break.
114 .sp
115 The JIT compiler generates different optimized code for each of the three
116 modes (normal, soft partial, hard partial). When \fBpcre_exec()\fP is called,
117 the appropriate code is run if it is available. Otherwise, the pattern is
118 matched using interpretive code.
119 .P
120 In some circumstances you may need to call additional functions. These are
121 described in the section entitled
122 .\" HTML <a href="#stackcontrol">
123 .\" </a>
124 "Controlling the JIT stack"
125 .\"
126 below.
127 .P
128 If JIT support is not available, PCRE_STUDY_JIT_COMPILE etc. are ignored, and
129 no JIT data is created. Otherwise, the compiled pattern is passed to the JIT
130 compiler, which turns it into machine code that executes much faster than the
131 normal interpretive code. When \fBpcre_exec()\fP is passed a \fBpcre_extra\fP
132 block containing a pointer to JIT code of the appropriate mode (normal or
133 hard/soft partial), it obeys that code instead of running the interpreter. The
134 result is identical, but the compiled JIT code runs much faster.
135 .P
136 There are some \fBpcre_exec()\fP options that are not supported for JIT
137 execution. There are also some pattern items that JIT cannot handle. Details
138 are given below. In both cases, execution automatically falls back to the
139 interpretive code. If you want to know whether JIT was actually used for a
140 particular match, you should arrange for a JIT callback function to be set up
141 as described in the section entitled
142 .\" HTML <a href="#stackcontrol">
143 .\" </a>
144 "Controlling the JIT stack"
145 .\"
146 below, even if you do not need to supply a non-default JIT stack. Such a
147 callback function is called whenever JIT code is about to be obeyed. If the
148 execution options are not right for JIT execution, the callback function is not
149 obeyed.
150 .P
151 If the JIT compiler finds an unsupported item, no JIT data is generated. You
152 can find out if JIT execution is available after studying a pattern by calling
153 \fBpcre_fullinfo()\fP with the PCRE_INFO_JIT option. A result of 1 means that
154 JIT compilation was successful. A result of 0 means that JIT support is not
155 available, or the pattern was not studied with PCRE_STUDY_JIT_COMPILE etc., or
156 the JIT compiler was not able to handle the pattern.
157 .P
158 Once a pattern has been studied, with or without JIT, it can be used as many
159 times as you like for matching different subject strings.
160 .
161 .
163 .rs
164 .sp
165 The only \fBpcre_exec()\fP options that are supported for JIT execution are
169 .P
170 The only unsupported pattern items are \eC (match a single data unit) when
171 running in a UTF mode, and a callout immediately before an assertion condition
172 in a conditional group.
173 .
174 .
176 .rs
177 .sp
178 When a pattern is matched using JIT execution, the return values are the same
179 as those given by the interpretive \fBpcre_exec()\fP code, with the addition of
180 one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used
181 for the JIT stack was insufficient. See
182 .\" HTML <a href="#stackcontrol">
183 .\" </a>
184 "Controlling the JIT stack"
185 .\"
186 below for a discussion of JIT stack usage. For compatibility with the
187 interpretive \fBpcre_exec()\fP code, no more than two-thirds of the
188 \fIovector\fP argument is used for passing back captured substrings.
189 .P
190 The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a
191 very large pattern tree goes on for too long, as it is in the same circumstance
192 when JIT is not used, but the details of exactly what is counted are not the
193 same. The PCRE_ERROR_RECURSIONLIMIT error code is never returned by JIT
194 execution.
195 .
196 .
198 .rs
199 .sp
200 The code that is generated by the JIT compiler is architecture-specific, and is
201 also position dependent. For those reasons it cannot be saved (in a file or
202 database) and restored later like the bytecode and other data of a compiled
203 pattern. Saving and restoring compiled patterns is not something many people
204 do. More detail about this facility is given in the
205 .\" HREF
206 \fBpcreprecompile\fP
207 .\"
208 documentation. It should be possible to run \fBpcre_study()\fP on a saved and
209 restored pattern, and thereby recreate the JIT data, but because JIT
210 compilation uses significant resources, it is probably not worth doing this;
211 you might as well recompile the original pattern.
212 .
213 .
214 .\" HTML <a name="stackcontrol"></a>
216 .rs
217 .sp
218 When the compiled JIT code runs, it needs a block of memory to use as a stack.
219 By default, it uses 32K on the machine stack. However, some large or
220 complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT
221 is given when there is not enough stack. Three functions are provided for
222 managing blocks of memory for use as JIT stacks. There is further discussion
223 about the use of JIT stacks in the section entitled
224 .\" HTML <a href="#stackcontrol">
225 .\" </a>
226 "JIT stack FAQ"
227 .\"
228 below.
229 .P
230 The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments
231 are a starting size and a maximum size, and it returns a pointer to an opaque
232 structure of type \fBpcre_jit_stack\fP, or NULL if there is an error. The
233 \fBpcre_jit_stack_free()\fP function can be used to free a stack that is no
234 longer needed. (For the technically minded: the address space is allocated by
235 mmap or VirtualAlloc.)
236 .P
237 JIT uses far less memory for recursion than the interpretive code,
238 and a maximum stack size of 512K to 1M should be more than enough for any
239 pattern.
240 .P
241 The \fBpcre_assign_jit_stack()\fP function specifies which stack JIT code
242 should use. Its arguments are as follows:
243 .sp
244 pcre_extra *extra
245 pcre_jit_callback callback
246 void *data
247 .sp
248 The \fIextra\fP argument must be the result of studying a pattern with
249 PCRE_STUDY_JIT_COMPILE etc. There are three cases for the values of the other
250 two options:
251 .sp
252 (1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block
253 on the machine stack is used.
254 .sp
255 (2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be
256 a valid JIT stack, the result of calling \fBpcre_jit_stack_alloc()\fP.
257 .sp
258 (3) If \fIcallback\fP is not NULL, it must point to a function that is
259 called with \fIdata\fP as an argument at the start of matching, in
260 order to set up a JIT stack. If the return from the callback
261 function is NULL, the internal 32K stack is used; otherwise the
262 return value must be a valid JIT stack, the result of calling
263 \fBpcre_jit_stack_alloc()\fP.
264 .sp
265 A callback function is obeyed whenever JIT code is about to be run; it is not
266 obeyed when \fBpcre_exec()\fP is called with options that are incompatible for
267 JIT execution. A callback function can therefore be used to determine whether a
268 match operation was executed by JIT or by the interpreter.
269 .P
270 You may safely use the same JIT stack for more than one pattern (either by
271 assigning directly or by callback), as long as the patterns are all matched
272 sequentially in the same thread. In a multithread application, if you do not
273 specify a JIT stack, or if you assign or pass back NULL from a callback, that
274 is thread-safe, because each thread has its own machine stack. However, if you
275 assign or pass back a non-NULL JIT stack, this must be a different stack for
276 each thread so that the application is thread-safe.
277 .P
278 Strictly speaking, even more is allowed. You can assign the same non-NULL stack
279 to any number of patterns as long as they are not used for matching by multiple
280 threads at the same time. For example, you can assign the same stack to all
281 compiled patterns, and use a global mutex in the callback to wait until the
282 stack is available for use. However, this is an inefficient solution, and not
283 recommended.
284 .P
285 This is a suggestion for how a multithreaded program that needs to set up
286 non-default JIT stacks might operate:
287 .sp
288 During thread initialization
289 thread_local_var = pcre_jit_stack_alloc(...)
290 .sp
291 During thread exit
292 pcre_jit_stack_free(thread_local_var)
293 .sp
294 Use a one-line callback function
295 return thread_local_var
296 .sp
297 All the functions described in this section do nothing if JIT is not available,
298 and \fBpcre_assign_jit_stack()\fP does nothing unless the \fBextra\fP argument
299 is non-NULL and points to a \fBpcre_extra\fP block that is the result of a
300 successful study with PCRE_STUDY_JIT_COMPILE etc.
301 .
302 .
303 .\" HTML <a name="stackfaq"></a>
305 .rs
306 .sp
307 (1) Why do we need JIT stacks?
308 .sp
309 PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack where
310 the local data of the current node is pushed before checking its child nodes.
311 Allocating real machine stack on some platforms is difficult. For example, the
312 stack chain needs to be updated every time if we extend the stack on PowerPC.
313 Although it is possible, its updating time overhead decreases performance. So
314 we do the recursion in memory.
315 .P
316 (2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP?
317 .sp
318 Modern operating systems have a nice feature: they can reserve an address space
319 instead of allocating memory. We can safely allocate memory pages inside this
320 address space, so the stack could grow without moving memory data (this is
321 important because of pointers). Thus we can allocate 1M address space, and use
322 only a single memory page (usually 4K) if that is enough. However, we can still
323 grow up to 1M anytime if needed.
324 .P
325 (3) Who "owns" a JIT stack?
326 .sp
327 The owner of the stack is the user program, not the JIT studied pattern or
328 anything else. The user program must ensure that if a stack is used by
329 \fBpcre_exec()\fP, (that is, it is assigned to the pattern currently running),
330 that stack must not be used by any other threads (to avoid overwriting the same
331 memory area). The best practice for multithreaded programs is to allocate a
332 stack for each thread, and return this stack through the JIT callback function.
333 .P
334 (4) When should a JIT stack be freed?
335 .sp
336 You can free a JIT stack at any time, as long as it will not be used by
337 \fBpcre_exec()\fP again. When you assign the stack to a pattern, only a pointer
338 is set. There is no reference counting or any other magic. You can free the
339 patterns and stacks in any order, anytime. Just \fIdo not\fP call
340 \fBpcre_exec()\fP with a pattern pointing to an already freed stack, as that
341 will cause SEGFAULT. (Also, do not free a stack currently used by
342 \fBpcre_exec()\fP in another thread). You can also replace the stack for a
343 pattern at any time. You can even free the previous stack before assigning a
344 replacement.
345 .P
346 (5) Should I allocate/free a stack every time before/after calling
347 \fBpcre_exec()\fP?
348 .sp
349 No, because this is too costly in terms of resources. However, you could
350 implement some clever idea which release the stack if it is not used in let's
351 say two minutes. The JIT callback can help to achieve this without keeping a
352 list of the currently JIT studied patterns.
353 .P
354 (6) OK, the stack is for long term memory allocation. But what happens if a
355 pattern causes stack overflow with a stack of 1M? Is that 1M kept until the
356 stack is freed?
357 .sp
358 Especially on embedded sytems, it might be a good idea to release memory
359 sometimes without freeing the stack. There is no API for this at the moment.
360 Probably a function call which returns with the currently allocated memory for
361 any stack and another which allows releasing memory (shrinking the stack) would
362 be a good idea if someone needs this.
363 .P
364 (7) This is too much of a headache. Isn't there any better solution for JIT
365 stack handling?
366 .sp
367 No, thanks to Windows. If POSIX threads were used everywhere, we could throw
368 out this complicated API.
369 .
370 .
372 .rs
373 .sp
374 This is a single-threaded example that specifies a JIT stack without using a
375 callback.
376 .sp
377 int rc;
378 int ovector[30];
379 pcre *re;
380 pcre_extra *extra;
381 pcre_jit_stack *jit_stack;
382 .sp
383 re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
384 /* Check for errors */
385 extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
386 jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
387 /* Check for error (NULL) */
388 pcre_assign_jit_stack(extra, NULL, jit_stack);
389 rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
390 /* Check results */
391 pcre_free(re);
392 pcre_free_study(extra);
393 pcre_jit_stack_free(jit_stack);
394 .sp
395 .
396 .
398 .rs
399 .sp
400 Because the API described above falls back to interpreted execution when JIT is
401 not available, it is convenient for programs that are written for general use
402 in many environments. However, calling JIT via \fBpcre_exec()\fP does have a
403 performance impact. Programs that are written for use where JIT is known to be
404 available, and which need the best possible performance, can instead use a
405 "fast path" API to call JIT execution directly instead of calling
406 \fBpcre_exec()\fP (obviously only for patterns that have been successfully
407 studied by JIT).
408 .P
409 The fast path function is called \fBpcre_jit_exec()\fP, and it takes exactly
410 the same arguments as \fBpcre_exec()\fP, plus one additional argument that
411 must point to a JIT stack. The JIT stack arrangements described above do not
412 apply. The return values are the same as for \fBpcre_exec()\fP.
413 .P
414 When you call \fBpcre_exec()\fP, as well as testing for invalid options, a
415 number of other sanity checks are performed on the arguments. For example, if
416 the subject pointer is NULL, or its length is negative, an immediate error is
417 given. Also, unless PCRE_NO_UTF[8|16|32] is set, a UTF subject string is tested
418 for validity. In the interests of speed, these checks do not happen on the JIT
419 fast path, and if invalid data is passed, the result is undefined.
420 .P
421 Bypassing the sanity checks and the \fBpcre_exec()\fP wrapping can give
422 speedups of more than 10%.
423 .P
424 Note that the \fBpcre_jit_exec()\fP function is not available in versions of
425 PCRE before 8.32 (released in November 2012). If you need to support versions
426 that old you must either use the slower \fBpcre_exec()\fP, or switch between
427 the two codepaths by checking the values of PCRE_MAJOR and PCRE_MINOR.
428 .P
429 Due to an unfortunate implementation oversight, even in versions 8.32
430 and later there will be no \fBpcre_jit_exec()\fP stub function defined
431 when PCRE is compiled with --disable-jit, which is the default, and
432 there's no way to detect whether PCRE was compiled with --enable-jit
433 via a macro.
434 .P
435 If you need to support versions older than 8.32, or versions that may
436 not build with --enable-jit, you must either use the slower
437 \fBpcre_exec()\fP, or switch between the two codepaths by checking the
438 values of PCRE_MAJOR and PCRE_MINOR.
439 .P
440 Switching between the two by checking the version assumes that all the
441 versions being targeted are built with --enable-jit. To also support
442 builds that may use --disable-jit either \fBpcre_exec()\fP must be
443 used, or a compile-time check for JIT via \fBpcre_config()\fP (which
444 assumes the runtime environment will be the same), or as the Git
445 project decided to do, simply assume that \fBpcre_jit_exec()\fP is
446 present in 8.32 or later unless a compile-time flag is provided, see
447 the "grep: un-break building with PCRE >= 8.32 without --enable-jit"
448 commit in git.git for an example of that.
449 .
450 .
451 .SH "SEE ALSO"
452 .rs
453 .sp
454 \fBpcreapi\fP(3)
455 .
456 .
458 .rs
459 .sp
460 .nf
461 Philip Hazel (FAQ by Zoltan Herczeg)
462 University Computing Service
463 Cambridge CB2 3QH, England.
464 .fi
465 .
466 .
468 .rs
469 .sp
470 .nf
471 Last updated: 05 July 2017
472 Copyright (c) 1997-2017 University of Cambridge.
473 .fi

  ViewVC Help
Powered by ViewVC 1.1.5