/[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 91 by nigel, Sat Feb 24 21:41:34 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 52  frame for each matched character. For a Line 65  frame for each matched character. For a
65  required. Consider now this rewritten pattern, which matches exactly the same  required. Consider now this rewritten pattern, which matches exactly the same
66  strings:  strings:
67  .sp  .sp
68    ([^<]++|<(?!inet))    ([^<]++|<(?!inet))+
69  .sp  .sp
70  This uses very much less stack, because runs of characters that do not contain  This uses very much less stack, because runs of characters that do not contain
71  "<" are "swallowed" in one item inside the parentheses. Recursion happens only  "<" are "swallowed" in one item inside the parentheses. Recursion happens only
# Line 61  assume this is relatively rare). A posse Line 74  assume this is relatively rare). A posse
74  backtracking into the runs of non-"<" characters, but that is not related to  backtracking into the runs of non-"<" characters, but that is not related to
75  stack usage.  stack usage.
76  .P  .P
77    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
79    than one character whenever possible.
80    .
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
93    and frees memory by calling the functions that are pointed to by the
94    \fBpcre[16]_stack_malloc\fP and \fBpcre[16]_stack_free\fP variables. By
95    default, these point to \fBmalloc()\fP and \fBfree()\fP, but you can replace
96    the pointers to cause PCRE to use your own functions. Since the block sizes are
97    always the same, and are always freed in reverse order, it may be possible to
98    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.  documentation.
124  .P  .P
125  In Unix-like environments, there is not often a problem with the stack, though  As a very rough rule of thumb, you should reckon on about 500 bytes per
126  the default limit on stack size varies from system to system. Values from 8Mb  recursion. Thus, if you want to limit your stack usage to 8Mb, you
127  to 64Mb are common. You can find your default limit by running the command:  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
142    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
144    default limit by running the command:
145  .sp  .sp
146    ulimit -s    ulimit -s
147  .sp  .sp
148  The effect of running out of stack is often SIGSEGV, though sometimes an error  Unfortunately, the effect of running out of stack is often SIGSEGV, though
149  message is given. You can normally increase the limit on stack size by code  sometimes a more explicit error message is given. You can normally increase the
150  such as this:  limit on stack size by code such as this:
151  .sp  .sp
152    struct rlimit rlim;    struct rlimit rlim;
153    getrlimit(RLIMIT_STACK, &rlim);    getrlimit(RLIMIT_STACK, &rlim);
# Line 87  such as this: Line 156  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  .\"  .\"
172  documentation.  .
173  .P  .
174  As a very rough rule of thumb, you should reckon on about 500 bytes per  .SH AUTHOR
175  recursion. Thus, if you want to limit your stack usage to 8Mb, you  .rs
176  should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can  .sp
177  support around 128000 recursions. The \fBpcretest\fP test program has a command  .nf
178  line option (\fB-S\fP) that can be used to increase its stack.  Philip Hazel
179  .P  University Computing Service
180  .in 0  Cambridge CB2 3QH, England.
181  Last updated: 29 June 2006  .fi
182  .br  .
183  Copyright (c) 1997-2006 University of Cambridge.  .
184    .SH REVISION
185    .rs
186    .sp
187    .nf
188    Last updated: 10 January 2012
189    Copyright (c) 1997-2012 University of Cambridge.
190    .fi

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12