ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 761 - (show annotations)
Tue Nov 22 12:24:26 2011 UTC (9 years, 3 months ago) by ph10
File size: 13523 byte(s)
Add JIT stack FAQ to JIT documentation.
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
11 \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 \fBpcre_exec()\fP. If the subject string is very long, it may still pay to use
14 JIT for one-off matches.
15 .P
16 JIT support applies only to the traditional matching function,
17 \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 .
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 Intel x86 32-bit and 64-bit
30 MIPS 32-bit
31 Power PC 32-bit and 64-bit (experimental)
32 .sp
33 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 .P
37 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 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 .
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 .sp
53 (2) Use \fBpcre_free_study()\fP to free the \fBpcre_extra\fP block when it is
54 no longer needed instead of just freeing it yourself. This
55 ensures that any JIT data is also freed.
56 .sp
57 In some circumstances you may need to call additional functions. These are
58 described in the section entitled
59 .\" HTML <a href="#stackcontrol">
60 .\" </a>
61 "Controlling the JIT stack"
62 .\"
63 below.
64 .P
65 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 .P
72 There are some \fBpcre_exec()\fP options that are not supported for JIT
73 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 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 JIT compilation was successful. A result of 0 means that JIT support is not
81 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 .P
84 Once a pattern has been studied, with or without JIT, it can be used as many
85 times as you like for matching different subject strings.
86 .
87 .
89 .rs
90 .sp
91 The only \fBpcre_exec()\fP options that are supported for JIT execution are
93 PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not
94 supported.
95 .P
96 The unsupported pattern items are:
97 .sp
98 \eC match a single byte; not supported in UTF-8 mode
99 (?Cn) callouts
100 (*COMMIT) )
101 (*MARK) )
102 (*PRUNE) ) the backtracking control verbs
103 (*SKIP) )
104 (*THEN) )
105 .sp
106 Support for some of these may be added in future.
107 .
108 .
110 .rs
111 .sp
112 When a pattern is matched using JIT execution, the return values are the same
113 as those given by the interpretive \fBpcre_exec()\fP code, with the addition of
114 one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used
115 for the JIT stack was insufficient. See
116 .\" HTML <a href="#stackcontrol">
117 .\" </a>
118 "Controlling the JIT stack"
119 .\"
120 below for a discussion of JIT stack usage. For compatibility with the
121 interpretive \fBpcre_exec()\fP code, no more than two-thirds of the
122 \fIovector\fP argument is used for passing back captured substrings.
123 .P
124 The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a
125 very large pattern tree goes on for too long, as it is in the same circumstance
126 when JIT is not used, but the details of exactly what is counted are not the
127 same. The PCRE_ERROR_RECURSIONLIMIT error code is never returned by JIT
128 execution.
129 .
130 .
132 .rs
133 .sp
134 The code that is generated by the JIT compiler is architecture-specific, and is
135 also position dependent. For those reasons it cannot be saved (in a file or
136 database) and restored later like the bytecode and other data of a compiled
137 pattern. Saving and restoring compiled patterns is not something many people
138 do. More detail about this facility is given in the
139 .\" HREF
140 \fBpcreprecompile\fP
141 .\"
142 documentation. It should be possible to run \fBpcre_study()\fP on a saved and
143 restored pattern, and thereby recreate the JIT data, but because JIT
144 compilation uses significant resources, it is probably not worth doing this;
145 you might as well recompile the original pattern.
146 .
147 .
148 .\" HTML <a name="stackcontrol"></a>
150 .rs
151 .sp
152 When the compiled JIT code runs, it needs a block of memory to use as a stack.
153 By default, it uses 32K on the machine stack. However, some large or
154 complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT
155 is given when there is not enough stack. Three functions are provided for
156 managing blocks of memory for use as JIT stacks. There is further discussion
157 about the use of JIT stacks in the section entitled
158 .\" HTML <a href="#stackcontrol">
159 .\" </a>
160 "JIT stack FAQ"
161 .\"
162 below.
163 .P
164 The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments
165 are a starting size and a maximum size, and it returns a pointer to an opaque
166 structure of type \fBpcre_jit_stack\fP, or NULL if there is an error. The
167 \fBpcre_jit_stack_free()\fP function can be used to free a stack that is no
168 longer needed. (For the technically minded: the address space is allocated by
169 mmap or VirtualAlloc.)
170 .P
171 JIT uses far less memory for recursion than the interpretive code,
172 and a maximum stack size of 512K to 1M should be more than enough for any
173 pattern.
174 .P
175 The \fBpcre_assign_jit_stack()\fP function specifies which stack JIT code
176 should use. Its arguments are as follows:
177 .sp
178 pcre_extra *extra
179 pcre_jit_callback callback
180 void *data
181 .sp
182 The \fIextra\fP argument must be the result of studying a pattern with
183 PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two
184 options:
185 .sp
186 (1) If \fIcallback\fP is NULL and \fIdata\fP is NULL, an internal 32K block
187 on the machine stack is used.
188 .sp
189 (2) If \fIcallback\fP is NULL and \fIdata\fP is not NULL, \fIdata\fP must be
190 a valid JIT stack, the result of calling \fBpcre_jit_stack_alloc()\fP.
191 .sp
192 (3) If \fIcallback\fP not NULL, it must point to a function that is called
193 with \fIdata\fP as an argument at the start of matching, in order to
194 set up a JIT stack. If the result is NULL, the internal 32K stack
195 is used; otherwise the return value must be a valid JIT stack,
196 the result of calling \fBpcre_jit_stack_alloc()\fP.
197 .sp
198 You may safely assign the same JIT stack to more than one pattern, as long as
199 they are all matched sequentially in the same thread. In a multithread
200 application, each thread must use its own JIT stack.
201 .P
202 Strictly speaking, even more is allowed. You can assign the same stack to any
203 number of patterns as long as they are not used for matching by multiple
204 threads at the same time. For example, you can assign the same stack to all
205 compiled patterns, and use a global mutex in the callback to wait until the
206 stack is available for use. However, this is an inefficient solution, and
207 not recommended.
208 .P
209 This is a suggestion for how a typical multithreaded program might operate:
210 .sp
211 During thread initalization
212 thread_local_var = pcre_jit_stack_alloc(...)
213 .sp
214 During thread exit
215 pcre_jit_stack_free(thread_local_var)
216 .sp
217 Use a one-line callback function
218 return thread_local_var
219 .sp
220 All the functions described in this section do nothing if JIT is not available,
221 and \fBpcre_assign_jit_stack()\fP does nothing unless the \fBextra\fP argument
222 is non-NULL and points to a \fBpcre_extra\fP block that is the result of a
223 successful study with PCRE_STUDY_JIT_COMPILE.
224 .
225 .
226 .\" HTML <a name="stackfaq"></a>
228 .rs
229 .sp
230 (1) Why do we need JIT stacks?
231 .sp
232 PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack where
233 the local data of the current node is pushed before checking its child nodes.
234 Allocating real machine stack on some platforms is difficult. For example, the
235 stack chain needs to be updated every time if we extend the stack on PowerPC.
236 Although it is possible, its updating time overhead decreases performance. So
237 we do the recursion in memory.
238 .P
239 (2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP?
240 .sp
241 Modern operating systems have a nice feature: they can reserve an address space
242 instead of allocating memory. We can safely allocate memory pages inside this
243 address space, so the stack could grow without moving memory data (this is
244 important because of pointers). Thus we can allocate 1M address space, and use
245 only a single memory page (usually 4K) if that is enough. However, we can still
246 grow up to 1M anytime if needed.
247 .P
248 (3) Who "owns" a JIT stack?
249 .sp
250 The owner of the stack is the user program, not the JIT studied pattern or
251 anything else. The user program must ensure that if a stack is used by
252 \fBpcre_exec()\fP, (that is, it is assigned to the pattern currently running),
253 that stack must not be used by any other threads (to avoid overwriting the same
254 memory area). The best practice for multithreaded programs is to allocate a
255 stack for each thread, and return this stack through the JIT callback function.
256 .P
257 (4) When should a JIT stack be freed?
258 .sp
259 You can free a JIT stack at any time, as long as it will not be used by
260 \fBpcre_exec()\fP again. When you assign the stack to a pattern, only a pointer
261 is set. There is no reference counting or any other magic. You can free the
262 patterns and stacks in any order, anytime. Just \fIdo not\fP call
263 \fBpcre_exec()\fP with a pattern pointing to an already freed stack, as that
264 will cause SEGFAULT. (Also, do not free a stack currently used by
265 \fBpcre_exec()\fP in another thread). You can also replace the stack for a
266 pattern at any time. You can even free the previous stack before assigning a
267 replacement.
268 .P
269 (5) Should I allocate/free a stack every time before/after calling
270 \fBpcre_exec()\fP?
271 .sp
272 No, because this is too costly in terms of resources. However, you could
273 implement some clever idea which release the stack if it is not used in let's
274 say two minutes. The JIT callback can help to achive this without keeping a
275 list of the currently JIT studied patterns.
276 .P
277 (6) OK, the stack is for long term memory allocation. But what happens if a
278 pattern causes stack overflow with a stack of 1M? Is that 1M kept until the
279 stack is freed?
280 .sp
281 Especially on embedded sytems, it might be a good idea to release
282 memory sometimes without freeing the stack. There is no API for this at the
283 moment. Probably a function call which returns with the currently allocated
284 memory for any stack and another which allows releasing memory (shrinking the
285 stack) would be a good idea if someone needs this.
286 .P
287 (7) This is too much of a headache. Isn't there any better solution for JIT
288 stack handling?
289 .sp
290 No, thanks to Windows. If POSIX threads were used everywhere, we could throw
291 out this complicated API.
292 .
293 .
295 .rs
296 .sp
297 This is a single-threaded example that specifies a JIT stack without using a
298 callback.
299 .sp
300 int rc;
301 int ovector[30];
302 pcre *re;
303 pcre_extra *extra;
304 pcre_jit_stack *jit_stack;
305 .sp
306 re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
307 /* Check for errors */
308 extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
309 jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
310 /* Check for error (NULL) */
311 pcre_assign_jit_stack(extra, NULL, jit_stack);
312 rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
313 /* Check results */
314 pcre_free(re);
315 pcre_free_study(extra);
316 pcre_jit_stack_free(jit_stack);
317 .sp
318 .
319 .
320 .SH "SEE ALSO"
321 .rs
322 .sp
323 \fBpcreapi\fP(3)
324 .
325 .
327 .rs
328 .sp
329 .nf
330 Philip Hazel (FAQ by Zoltan Herczeg)
331 University Computing Service
332 Cambridge CB2 3QH, England.
333 .fi
334 .
335 .
337 .rs
338 .sp
339 .nf
340 Last updated: 22 November 2011
341 Copyright (c) 1997-2011 University of Cambridge.
342 .fi

  ViewVC Help
Powered by ViewVC 1.1.5