ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 93 - (show annotations)
Sat Feb 24 21:41:42 2007 UTC (14 years, 8 months ago) by nigel
File size: 5100 byte(s)
Error occurred while calculating annotation data.
Load pcre-7.0 into code/trunk.
3 PCRE - Perl-compatible regular expressions
5 .rs
6 .sp
7 When you call \fBpcre_exec()\fP, it makes use of an internal function called
8 \fBmatch()\fP. This calls itself recursively at branch points in the pattern,
9 in order to remember the state of the match so that it can back up and try a
10 different alternative if the first one fails. As matching proceeds deeper and
11 deeper into the tree of possibilities, the recursion depth increases.
12 .P
13 Not all calls of \fBmatch()\fP increase the recursion depth; for an item such
14 as a* it may be called several times at the same level, after matching
15 different numbers of a's. Furthermore, in a number of cases where the result of
16 the recursive call would immediately be passed back as the result of the
17 current call (a "tail recursion"), the function is just restarted instead.
18 .P
19 The \fBpcre_dfa_exec()\fP function operates in an entirely different way, and
20 hardly uses recursion at all. The limit on its complexity is the amount of
21 workspace it is given. The comments that follow do NOT apply to
22 \fBpcre_dfa_exec()\fP; they are relevant only for \fBpcre_exec()\fP.
23 .P
24 You can set limits on the number of times that \fBmatch()\fP is called, both in
25 total and recursively. If the limit is exceeded, an error occurs. For details,
26 see the
27 .\" HTML <a href="pcreapi.html#extradata">
28 .\" </a>
29 section on extra data for \fBpcre_exec()\fP
30 .\"
31 in the
32 .\" HREF
33 \fBpcreapi\fP
34 .\"
35 documentation.
36 .P
37 Each time that \fBmatch()\fP is actually called recursively, it uses memory
38 from the process stack. For certain kinds of pattern and data, very large
39 amounts of stack may be needed, despite the recognition of "tail recursion".
40 You can often reduce the amount of recursion, and therefore the amount of stack
41 used, by modifying the pattern that is being matched. Consider, for example,
42 this pattern:
43 .sp
44 ([^<]|<(?!inet))+
45 .sp
46 It matches from wherever it starts until it encounters "<inet" or the end of
47 the data, and is the kind of pattern that might be used when processing an XML
48 file. Each iteration of the outer parentheses matches either one character that
49 is not "<" or a "<" that is not followed by "inet". However, each time a
50 parenthesis is processed, a recursion occurs, so this formulation uses a stack
51 frame for each matched character. For a long string, a lot of stack is
52 required. Consider now this rewritten pattern, which matches exactly the same
53 strings:
54 .sp
55 ([^<]++|<(?!inet))
56 .sp
57 This uses very much less stack, because runs of characters that do not contain
58 "<" are "swallowed" in one item inside the parentheses. Recursion happens only
59 when a "<" character that is not followed by "inet" is encountered (and we
60 assume this is relatively rare). A possessive quantifier is used to stop any
61 backtracking into the runs of non-"<" characters, but that is not related to
62 stack usage.
63 .P
64 This example shows that one way of avoiding stack problems when matching long
65 subject strings is to write repeated parenthesized subpatterns to match more
66 than one character whenever possible.
67 .P
68 In environments where stack memory is constrained, you might want to compile
69 PCRE to use heap memory instead of stack for remembering back-up points. This
70 makes it run a lot more slowly, however. Details of how to do this are given in
71 the
72 .\" HREF
73 \fBpcrebuild\fP
74 .\"
75 documentation.
76 .P
77 In Unix-like environments, there is not often a problem with the stack unless
78 very long strings are involved, though the default limit on stack size varies
79 from system to system. Values from 8Mb to 64Mb are common. You can find your
80 default limit by running the command:
81 .sp
82 ulimit -s
83 .sp
84 Unfortunately, the effect of running out of stack is often SIGSEGV, though
85 sometimes a more explicit error message is given. You can normally increase the
86 limit on stack size by code such as this:
87 .sp
88 struct rlimit rlim;
89 getrlimit(RLIMIT_STACK, &rlim);
90 rlim.rlim_cur = 100*1024*1024;
91 setrlimit(RLIMIT_STACK, &rlim);
92 .sp
93 This reads the current limits (soft and hard) using \fBgetrlimit()\fP, then
94 attempts to increase the soft limit to 100Mb using \fBsetrlimit()\fP. You must
95 do this before calling \fBpcre_exec()\fP.
96 .P
97 PCRE has an internal counter that can be used to limit the depth of recursion,
98 and thus cause \fBpcre_exec()\fP to give an error code before it runs out of
99 stack. By default, the limit is very large, and unlikely ever to operate. It
100 can be changed when PCRE is built, and it can also be set when
101 \fBpcre_exec()\fP is called. For details of these interfaces, see the
102 .\" HREF
103 \fBpcrebuild\fP
104 .\"
105 and
106 .\" HREF
107 \fBpcreapi\fP
108 .\"
109 documentation.
110 .P
111 As a very rough rule of thumb, you should reckon on about 500 bytes per
112 recursion. Thus, if you want to limit your stack usage to 8Mb, you
113 should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can
114 support around 128000 recursions. The \fBpcretest\fP test program has a command
115 line option (\fB-S\fP) that can be used to increase the size of its stack.
116 .P
117 .in 0
118 Last updated: 14 September 2006
119 .br
120 Copyright (c) 1997-2006 University of Cambridge.

  ViewVC Help
Powered by ViewVC 1.1.5