/[pcre]/code/trunk/doc/html/pcrejit.html
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 708 - (show annotations)
Fri Sep 23 11:03:03 2011 UTC (7 years, 11 months ago) by ph10
File MIME type: text/html
File size: 11806 byte(s)
Error occurred while calculating annotation data.
File tidies for 8.20-RC2 release.
1 <html>
2 <head>
3 <title>pcrejit specification</title>
4 </head>
5 <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6 <h1>pcrejit man page</h1>
7 <p>
8 Return to the <a href="index.html">PCRE index page</a>.
9 </p>
10 <p>
11 This page is part of the PCRE HTML documentation. It was generated automatically
12 from the original man page. If there is any nonsense in it, please consult the
13 man page, in case the conversion went wrong.
14 <br>
15 <ul>
16 <li><a name="TOC1" href="#SEC1">PCRE JUST-IN-TIME COMPILER SUPPORT</a>
17 <li><a name="TOC2" href="#SEC2">AVAILABILITY OF JIT SUPPORT</a>
18 <li><a name="TOC3" href="#SEC3">SIMPLE USE OF JIT</a>
19 <li><a name="TOC4" href="#SEC4">UNSUPPORTED OPTIONS AND PATTERN ITEMS</a>
20 <li><a name="TOC5" href="#SEC5">RETURN VALUES FROM JIT EXECUTION</a>
21 <li><a name="TOC6" href="#SEC6">SAVING AND RESTORING COMPILED PATTERNS</a>
22 <li><a name="TOC7" href="#SEC7">CONTROLLING THE JIT STACK</a>
23 <li><a name="TOC8" href="#SEC8">EXAMPLE CODE</a>
24 <li><a name="TOC9" href="#SEC9">SEE ALSO</a>
25 <li><a name="TOC10" href="#SEC10">AUTHOR</a>
26 <li><a name="TOC11" href="#SEC11">REVISION</a>
27 </ul>
28 <br><a name="SEC1" href="#TOC1">PCRE JUST-IN-TIME COMPILER SUPPORT</a><br>
29 <P>
30 Just-in-time compiling is a heavyweight optimization that can greatly speed up
31 pattern matching. However, it comes at the cost of extra processing before the
32 match is performed. Therefore, it is of most benefit when the same pattern is
33 going to be matched many times. This does not necessarily mean many calls of
34 \fPpcre_exec()\fP; if the pattern is not anchored, matching attempts may take
35 place many times at various positions in the subject, even for a single call to
36 <b>pcre_exec()</b>. If the subject string is very long, it may still pay to use
37 JIT for one-off matches.
38 </P>
39 <P>
40 JIT support applies only to the traditional matching function,
41 <b>pcre_exec()</b>. It does not apply when <b>pcre_dfa_exec()</b> is being used.
42 The code for this support was written by Zoltan Herczeg.
43 </P>
44 <br><a name="SEC2" href="#TOC1">AVAILABILITY OF JIT SUPPORT</a><br>
45 <P>
46 JIT support is an optional feature of PCRE. The "configure" option --enable-jit
47 (or equivalent CMake option) must be set when PCRE is built if you want to use
48 JIT. The support is limited to the following hardware platforms:
49 <pre>
50 ARM v5, v7, and Thumb2
51 Intel x86 32-bit and 64-bit
52 MIPS 32-bit
53 Power PC 32-bit and 64-bit
54 </pre>
55 If --enable-jit is set on an unsupported platform, compilation fails.
56 </P>
57 <P>
58 A program can tell if JIT support is available by calling <b>pcre_config()</b>
59 with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0
60 otherwise. However, a simple program does not need to check this in order to
61 use JIT. The API is implemented in a way that falls back to the ordinary PCRE
62 code if JIT is not available.
63 </P>
64 <br><a name="SEC3" href="#TOC1">SIMPLE USE OF JIT</a><br>
65 <P>
66 You have to do two things to make use of the JIT support in the simplest way:
67 <pre>
68 (1) Call <b>pcre_study()</b> with the PCRE_STUDY_JIT_COMPILE option for
69 each compiled pattern, and pass the resulting <b>pcre_extra</b> block to
70 <b>pcre_exec()</b>.
71
72 (2) Use <b>pcre_free_study()</b> to free the <b>pcre_extra</b> block when it is
73 no longer needed instead of just freeing it yourself. This
74 ensures that any JIT data is also freed.
75 </pre>
76 In some circumstances you may need to call additional functions. These are
77 described in the section entitled
78 <a href="#stackcontrol">"Controlling the JIT stack"</a>
79 below.
80 </P>
81 <P>
82 If JIT support is not available, PCRE_STUDY_JIT_COMPILE is ignored, and no JIT
83 data is set up. Otherwise, the compiled pattern is passed to the JIT compiler,
84 which turns it into machine code that executes much faster than the normal
85 interpretive code. When <b>pcre_exec()</b> is passed a <b>pcre_extra</b> block
86 containing a pointer to JIT code, it obeys that instead of the normal code. The
87 result is identical, but the code runs much faster.
88 </P>
89 <P>
90 There are some <b>pcre_exec()</b> options that are not supported for JIT
91 execution. There are also some pattern items that JIT cannot handle. Details
92 are given below. In both cases, execution automatically falls back to the
93 interpretive code.
94 </P>
95 <P>
96 If the JIT compiler finds an unsupported item, no JIT data is generated. You
97 can find out if JIT execution is available after studying a pattern by calling
98 <b>pcre_fullinfo()</b> with the PCRE_INFO_JIT option. A result of 1 means that
99 JIT compilation was successful. A result of 0 means that JIT support is not
100 available, or the pattern was not studied with PCRE_STUDY_JIT_COMPILE, or the
101 JIT compiler was not able to handle the pattern.
102 </P>
103 <P>
104 Once a pattern has been studied, with or without JIT, it can be used as many
105 times as you like for matching different subject strings.
106 </P>
107 <br><a name="SEC4" href="#TOC1">UNSUPPORTED OPTIONS AND PATTERN ITEMS</a><br>
108 <P>
109 The only <b>pcre_exec()</b> options that are supported for JIT execution are
110 PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, and
111 PCRE_NOTEMPTY_ATSTART. Note in particular that partial matching is not
112 supported.
113 </P>
114 <P>
115 The unsupported pattern items are:
116 <pre>
117 \C match a single byte, even in UTF-8 mode
118 (?Cn) callouts
119 (?(&#60;name&#62;)... conditional test on setting of a named subpattern
120 (?(R)... conditional test on whole pattern recursion
121 (?(Rn)... conditional test on recursion, by number
122 (?(R&name)... conditional test on recursion, by name
123 (*COMMIT) )
124 (*MARK) )
125 (*PRUNE) ) the backtracking control verbs
126 (*SKIP) )
127 (*THEN) )
128 </pre>
129 Support for some of these may be added in future.
130 </P>
131 <br><a name="SEC5" href="#TOC1">RETURN VALUES FROM JIT EXECUTION</a><br>
132 <P>
133 When a pattern is matched using JIT execution, the return values are the same
134 as those given by the interpretive <b>pcre_exec()</b> code, with the addition of
135 one new error code: PCRE_ERROR_JIT_STACKLIMIT. This means that the memory used
136 for the JIT stack was insufficient. See
137 <a href="#stackcontrol">"Controlling the JIT stack"</a>
138 below for a discussion of JIT stack usage. For compatibility with the
139 interpretive <b>pcre_exec()</b> code, no more than two-thirds of the
140 <i>ovector</i> argument is used for passing back captured substrings.
141 </P>
142 <P>
143 The error code PCRE_ERROR_MATCHLIMIT is returned by the JIT code if searching a
144 very large pattern tree goes on for too long, as it is in the same circumstance
145 when JIT is not used, but the details of exactly what is counted are not the
146 same. The PCRE_ERROR_RECURSIONLIMIT error code is never returned by JIT
147 execution.
148 </P>
149 <br><a name="SEC6" href="#TOC1">SAVING AND RESTORING COMPILED PATTERNS</a><br>
150 <P>
151 The code that is generated by the JIT compiler is architecture-specific, and is
152 also position dependent. For those reasons it cannot be saved (in a file or
153 database) and restored later like the bytecode and other data of a compiled
154 pattern. Saving and restoring compiled patterns is not something many people
155 do. More detail about this facility is given in the
156 <a href="pcreprecompile.html"><b>pcreprecompile</b></a>
157 documentation. It should be possible to run <b>pcre_study()</b> on a saved and
158 restored pattern, and thereby recreate the JIT data, but because JIT
159 compilation uses significant resources, it is probably not worth doing this;
160 you might as well recompile the original pattern.
161 <a name="stackcontrol"></a></P>
162 <br><a name="SEC7" href="#TOC1">CONTROLLING THE JIT STACK</a><br>
163 <P>
164 When the compiled JIT code runs, it needs a block of memory to use as a stack.
165 By default, it uses 32K on the machine stack. However, some large or
166 complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT
167 is given when there is not enough stack. Three functions are provided for
168 managing blocks of memory for use as JIT stacks.
169 </P>
170 <P>
171 The <b>pcre_jit_stack_alloc()</b> function creates a JIT stack. Its arguments
172 are a starting size and a maximum size, and it returns a pointer to an opaque
173 structure of type <b>pcre_jit_stack</b>, or NULL if there is an error. The
174 <b>pcre_jit_stack_free()</b> function can be used to free a stack that is no
175 longer needed. (For the technically minded: the address space is allocated by
176 mmap or VirtualAlloc.)
177 </P>
178 <P>
179 JIT uses far less memory for recursion than the interpretive code,
180 and a maximum stack size of 512K to 1M should be more than enough for any
181 pattern.
182 </P>
183 <P>
184 The <b>pcre_assign_jit_stack()</b> function specifies which stack JIT code
185 should use. Its arguments are as follows:
186 <pre>
187 pcre_extra *extra
188 pcre_jit_callback callback
189 void *data
190 </pre>
191 The <i>extra</i> argument must be the result of studying a pattern with
192 PCRE_STUDY_JIT_COMPILE. There are three cases for the values of the other two
193 options:
194 <pre>
195 (1) If <i>callback</i> is NULL and <i>data</i> is NULL, an internal 32K block
196 on the machine stack is used.
197
198 (2) If <i>callback</i> is NULL and <i>data</i> is not NULL, <i>data</i> must be
199 a valid JIT stack, the result of calling <b>pcre_jit_stack_alloc()</b>.
200
201 (3) If <i>callback</i> not NULL, it must point to a function that is called
202 with <i>data</i> as an argument at the start of matching, in order to
203 set up a JIT stack. If the result is NULL, the internal 32K stack
204 is used; otherwise the return value must be a valid JIT stack,
205 the result of calling <b>pcre_jit_stack_alloc()</b>.
206 </pre>
207 You may safely assign the same JIT stack to more than one pattern, as long as
208 they are all matched sequentially in the same thread. In a multithread
209 application, each thread must use its own JIT stack.
210 </P>
211 <P>
212 Strictly speaking, even more is allowed. You can assign the same stack to any
213 number of patterns as long as they are not used for matching by multiple
214 threads at the same time. For example, you can assign the same stack to all
215 compiled patterns, and use a global mutex in the callback to wait until the
216 stack is available for use. However, this is an inefficient solution, and
217 not recommended.
218 </P>
219 <P>
220 This is a suggestion for how a typical multithreaded program might operate:
221 <pre>
222 During thread initalization
223 thread_local_var = pcre_jit_stack_alloc(...)
224
225 During thread exit
226 pcre_jit_stack_free(thread_local_var)
227
228 Use a one-line callback function
229 return thread_local_var
230 </pre>
231 All the functions described in this section do nothing if JIT is not available,
232 and <b>pcre_assign_jit_stack()</b> does nothing unless the <b>extra</b> argument
233 is non-NULL and points to a <b>pcre_extra</b> block that is the result of a
234 successful study with PCRE_STUDY_JIT_COMPILE.
235 </P>
236 <br><a name="SEC8" href="#TOC1">EXAMPLE CODE</a><br>
237 <P>
238 This is a single-threaded example that specifies a JIT stack without using a
239 callback.
240 <pre>
241 int rc;
242 int ovector[30];
243 pcre *re;
244 pcre_extra *extra;
245 pcre_jit_stack *jit_stack;
246
247 re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
248 /* Check for errors */
249 extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
250 jit_stack = pcre_jit_stack_alloc(32*1024, 512*1024);
251 /* Check for error (NULL) */
252 pcre_assign_jit_stack(extra, NULL, jit_stack);
253 rc = pcre_exec(re, extra, subject, length, 0, 0, ovector, 30);
254 /* Check results */
255 pcre_free(re);
256 pcre_free_study(extra);
257 pcre_jit_stack_free(jit_stack);
258
259 </PRE>
260 </P>
261 <br><a name="SEC9" href="#TOC1">SEE ALSO</a><br>
262 <P>
263 <b>pcreapi</b>(3)
264 </P>
265 <br><a name="SEC10" href="#TOC1">AUTHOR</a><br>
266 <P>
267 Philip Hazel
268 <br>
269 University Computing Service
270 <br>
271 Cambridge CB2 3QH, England.
272 <br>
273 </P>
274 <br><a name="SEC11" href="#TOC1">REVISION</a><br>
275 <P>
276 Last updated: 23 September 2011
277 <br>
278 Copyright &copy; 1997-2011 University of Cambridge.
279 <br>
280 <p>
281 Return to the <a href="index.html">PCRE index page</a>.
282 </p>

  ViewVC Help
Powered by ViewVC 1.1.5