/[pcre]/code/trunk/doc/pcrestack.3
ViewVC logotype

Diff of /code/trunk/doc/pcrestack.3

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 174 by ph10, Tue Jun 5 11:38:06 2007 UTC revision 861 by ph10, Tue Jan 10 14:54:28 2012 UTC
# Line 4  PCRE - Perl-compatible regular expressio Line 4  PCRE - Perl-compatible regular expressio
4  .SH "PCRE DISCUSSION OF STACK USAGE"  .SH "PCRE DISCUSSION OF STACK USAGE"
5  .rs  .rs
6  .sp  .sp
7  When you call \fBpcre_exec()\fP, it makes use of an internal function called  When you call \fBpcre[16]_exec()\fP, it makes use of an internal function
8  \fBmatch()\fP. This calls itself recursively at branch points in the pattern,  called \fBmatch()\fP. This calls itself recursively at branch points in the
9  in order to remember the state of the match so that it can back up and try a  pattern, in order to remember the state of the match so that it can back up and
10  different alternative if the first one fails. As matching proceeds deeper and  try a different alternative if the first one fails. As matching proceeds deeper
11  deeper into the tree of possibilities, the recursion depth increases.  and deeper into the tree of possibilities, the recursion depth increases. The
12    \fBmatch()\fP function is also called in other circumstances, for example,
13    whenever a parenthesized sub-pattern is entered, and in certain cases of
14    repetition.
15  .P  .P
16  Not all calls of \fBmatch()\fP increase the recursion depth; for an item such  Not all calls of \fBmatch()\fP increase the recursion depth; for an item such
17  as a* it may be called several times at the same level, after matching  as a* it may be called several times at the same level, after matching
# Line 16  different numbers of a's. Furthermore, i Line 19  different numbers of a's. Furthermore, i
19  the recursive call would immediately be passed back as the result of the  the recursive call would immediately be passed back as the result of the
20  current call (a "tail recursion"), the function is just restarted instead.  current call (a "tail recursion"), the function is just restarted instead.
21  .P  .P
22  The \fBpcre_dfa_exec()\fP function operates in an entirely different way, and  The above comments apply when \fBpcre[16]_exec()\fP is run in its normal
23  hardly uses recursion at all. The limit on its complexity is the amount of  interpretive manner. If the pattern was studied with the
24  workspace it is given. The comments that follow do NOT apply to  PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was successful, and
25  \fBpcre_dfa_exec()\fP; they are relevant only for \fBpcre_exec()\fP.  the options passed to \fBpcre[16]_exec()\fP were not incompatible, the matching
26  .P  process uses the JIT-compiled code instead of the \fBmatch()\fP function. In
27  You can set limits on the number of times that \fBmatch()\fP is called, both in  this case, the memory requirements are handled entirely differently. See the
 total and recursively. If the limit is exceeded, an error occurs. For details,  
 see the  
 .\" HTML <a href="pcreapi.html#extradata">  
 .\" </a>  
 section on extra data for \fBpcre_exec()\fP  
 .\"  
 in the  
28  .\" HREF  .\" HREF
29  \fBpcreapi\fP  \fBpcrejit\fP
30  .\"  .\"
31  documentation.  documentation for details.
32  .P  .P
33    The \fBpcre[16]_dfa_exec()\fP function operates in an entirely different way,
34    and uses recursion only when there is a regular expression recursion or
35    subroutine call in the pattern. This includes the processing of assertion and
36    "once-only" subpatterns, which are handled like subroutine calls. Normally,
37    these are never very deep, and the limit on the complexity of
38    \fBpcre[16]_dfa_exec()\fP is controlled by the amount of workspace it is given.
39    However, it is possible to write patterns with runaway infinite recursions;
40    such patterns will cause \fBpcre[16]_dfa_exec()\fP to run out of stack. At
41    present, there is no protection against this.
42    .P
43    The comments that follow do NOT apply to \fBpcre[16]_dfa_exec()\fP; they are
44    relevant only for \fBpcre[16]_exec()\fP without the JIT optimization.
45    .
46    .
47    .SS "Reducing \fBpcre[16]_exec()\fP's stack usage"
48    .rs
49    .sp
50  Each time that \fBmatch()\fP is actually called recursively, it uses memory  Each time that \fBmatch()\fP is actually called recursively, it uses memory
51  from the process stack. For certain kinds of pattern and data, very large  from the process stack. For certain kinds of pattern and data, very large
52  amounts of stack may be needed, despite the recognition of "tail recursion".  amounts of stack may be needed, despite the recognition of "tail recursion".
# Line 64  stack usage. Line 77  stack usage.
77  This example shows that one way of avoiding stack problems when matching long  This example shows that one way of avoiding stack problems when matching long
78  subject strings is to write repeated parenthesized subpatterns to match more  subject strings is to write repeated parenthesized subpatterns to match more
79  than one character whenever possible.  than one character whenever possible.
80  .P  .
81    .
82    .SS "Compiling PCRE to use heap instead of stack for \fBpcre[16]_exec()\fP"
83    .rs
84    .sp
85  In environments where stack memory is constrained, you might want to compile  In environments where stack memory is constrained, you might want to compile
86  PCRE to use heap memory instead of stack for remembering back-up points. This  PCRE to use heap memory instead of stack for remembering back-up points when
87  makes it run a lot more slowly, however. Details of how to do this are given in  \fBpcre[16]_exec()\fP is running. This makes it run a lot more slowly, however.
88  the  Details of how to do this are given in the
89  .\" HREF  .\" HREF
90  \fBpcrebuild\fP  \fBpcrebuild\fP
91  .\"  .\"
92  documentation. When built in this way, instead of using the stack, PCRE obtains  documentation. When built in this way, instead of using the stack, PCRE obtains
93  and frees memory by calling the functions that are pointed to by the  and frees memory by calling the functions that are pointed to by the
94  \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP variables. By default, these  \fBpcre[16]_stack_malloc\fP and \fBpcre[16]_stack_free\fP variables. By
95  point to \fBmalloc()\fP and \fBfree()\fP, but you can replace the pointers to  default, these point to \fBmalloc()\fP and \fBfree()\fP, but you can replace
96  cause PCRE to use your own functions. Since the block sizes are always the  the pointers to cause PCRE to use your own functions. Since the block sizes are
97  same, and are always freed in reverse order, it may be possible to implement  always the same, and are always freed in reverse order, it may be possible to
98  customized memory handlers that are more efficient than the standard functions.  implement customized memory handlers that are more efficient than the standard
99    functions.
100    .
101    .
102    .SS "Limiting \fBpcre[16]_exec()\fP's stack usage"
103    .rs
104    .sp
105    You can set limits on the number of times that \fBmatch()\fP is called, both in
106    total and recursively. If a limit is exceeded, \fBpcre[16]_exec()\fP returns an
107    error code. Setting suitable limits should prevent it from running out of
108    stack. The default values of the limits are very large, and unlikely ever to
109    operate. They can be changed when PCRE is built, and they can also be set when
110    \fBpcre[16]_exec()\fP is called. For details of these interfaces, see the
111    .\" HREF
112    \fBpcrebuild\fP
113    .\"
114    documentation and the
115    .\" HTML <a href="pcreapi.html#extradata">
116    .\" </a>
117    section on extra data for \fBpcre[16]_exec()\fP
118    .\"
119    in the
120    .\" HREF
121    \fBpcreapi\fP
122    .\"
123    documentation.
124  .P  .P
125    As a very rough rule of thumb, you should reckon on about 500 bytes per
126    recursion. Thus, if you want to limit your stack usage to 8Mb, you
127    should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can
128    support around 128000 recursions.
129    .P
130    In Unix-like environments, the \fBpcretest\fP test program has a command line
131    option (\fB-S\fP) that can be used to increase the size of its stack. As long
132    as the stack is large enough, another option (\fB-M\fP) can be used to find the
133    smallest limits that allow a particular pattern to match a given subject
134    string. This is done by calling \fBpcre[16]_exec()\fP repeatedly with different
135    limits.
136    .
137    .
138    .SS "Changing stack size in Unix-like systems"
139    .rs
140    .sp
141  In Unix-like environments, there is not often a problem with the stack unless  In Unix-like environments, there is not often a problem with the stack unless
142  very long strings are involved, though the default limit on stack size varies  very long strings are involved, though the default limit on stack size varies
143  from system to system. Values from 8Mb to 64Mb are common. You can find your  from system to system. Values from 8Mb to 64Mb are common. You can find your
# Line 98  limit on stack size by code such as this Line 156  limit on stack size by code such as this
156  .sp  .sp
157  This reads the current limits (soft and hard) using \fBgetrlimit()\fP, then  This reads the current limits (soft and hard) using \fBgetrlimit()\fP, then
158  attempts to increase the soft limit to 100Mb using \fBsetrlimit()\fP. You must  attempts to increase the soft limit to 100Mb using \fBsetrlimit()\fP. You must
159  do this before calling \fBpcre_exec()\fP.  do this before calling \fBpcre[16]_exec()\fP.
160  .P  .
161  PCRE has an internal counter that can be used to limit the depth of recursion,  .
162  and thus cause \fBpcre_exec()\fP to give an error code before it runs out of  .SS "Changing stack size in Mac OS X"
163  stack. By default, the limit is very large, and unlikely ever to operate. It  .rs
164  can be changed when PCRE is built, and it can also be set when  .sp
165  \fBpcre_exec()\fP is called. For details of these interfaces, see the  Using \fBsetrlimit()\fP, as described above, should also work on Mac OS X. It
166  .\" HREF  is also possible to set a stack size when linking a program. There is a
167  \fBpcrebuild\fP  discussion about stack sizes in Mac OS X at this web site:
168  .\"  .\" HTML <a href="http://developer.apple.com/qa/qa2005/qa1419.html">
169  and  .\" </a>
170  .\" HREF  http://developer.apple.com/qa/qa2005/qa1419.html.
 \fBpcreapi\fP  
171  .\"  .\"
 documentation.  
 .P  
 As a very rough rule of thumb, you should reckon on about 500 bytes per  
 recursion. Thus, if you want to limit your stack usage to 8Mb, you  
 should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can  
 support around 128000 recursions. The \fBpcretest\fP test program has a command  
 line option (\fB-S\fP) that can be used to increase the size of its stack.  
172  .  .
173  .  .
174  .SH AUTHOR  .SH AUTHOR
# Line 135  Cambridge CB2 3QH, England. Line 185  Cambridge CB2 3QH, England.
185  .rs  .rs
186  .sp  .sp
187  .nf  .nf
188  Last updated: 05 June 2007  Last updated: 10 January 2012
189  Copyright (c) 1997-2007 University of Cambridge.  Copyright (c) 1997-2012 University of Cambridge.
190  .fi  .fi

Legend:
Removed from v.174  
changed lines
  Added in v.861

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12