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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (hide annotations) (download)
Sat Feb 24 21:40:37 2007 UTC (7 years, 8 months ago) by nigel
File size: 3491 byte(s)
Load pcre-5.0 into code/trunk.

1 nigel 63 .TH PCRE 3
2     .SH NAME
3     PCRE - Perl-compatible regular expressions
4 nigel 75 .SH "PCRE PERFORMANCE"
5 nigel 63 .rs
6     .sp
7     Certain items that may appear in regular expression patterns are more efficient
8     than others. It is more efficient to use a character class like [aeiou] than a
9     set of alternatives such as (a|e|i|o|u). In general, the simplest construction
10     that provides the required behaviour is usually the most efficient. Jeffrey
11 nigel 75 Friedl's book contains a lot of useful general discussion about optimizing
12     regular expressions for efficient performance. This document contains a few
13     observations about PCRE.
14     .P
15     Using Unicode character properties (the \ep, \eP, and \eX escapes) is slow,
16     because PCRE has to scan a structure that contains data for over fifteen
17     thousand characters whenever it needs a character's property. If you can find
18     an alternative pattern that does not use character properties, it will probably
19     be faster.
20     .P
21 nigel 63 When a pattern begins with .* not in parentheses, or in parentheses that are
22     not the subject of a backreference, and the PCRE_DOTALL option is set, the
23     pattern is implicitly anchored by PCRE, since it can match only at the start of
24     a subject string. However, if PCRE_DOTALL is not set, PCRE cannot make this
25     optimization, because the . metacharacter does not then match a newline, and if
26     the subject string contains newlines, the pattern may match from the character
27     immediately following one of them instead of from the very start. For example,
28     the pattern
29 nigel 75 .sp
30 nigel 63 .*second
31 nigel 75 .sp
32     matches the subject "first\enand second" (where \en stands for a newline
33 nigel 63 character), with the match starting at the seventh character. In order to do
34     this, PCRE has to retry the match starting after every newline in the subject.
35 nigel 75 .P
36 nigel 63 If you are using such a pattern with subject strings that do not contain
37     newlines, the best performance is obtained by setting PCRE_DOTALL, or starting
38     the pattern with ^.* to indicate explicit anchoring. That saves PCRE from
39     having to scan along the subject looking for a newline to restart at.
40 nigel 75 .P
41 nigel 63 Beware of patterns that contain nested indefinite repeats. These can take a
42     long time to run when applied to a string that does not match. Consider the
43     pattern fragment
44 nigel 75 .sp
45 nigel 63 (a+)*
46 nigel 75 .sp
47 nigel 63 This can match "aaaa" in 33 different ways, and this number increases very
48     rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
49     times, and for each of those cases other than 0, the + repeats can match
50     different numbers of times.) When the remainder of the pattern is such that the
51     entire match is going to fail, PCRE has in principle to try every possible
52     variation, and this can take an extremely long time.
53 nigel 75 .P
54 nigel 63 An optimization catches some of the more simple cases such as
55 nigel 75 .sp
56 nigel 63 (a+)*b
57 nigel 75 .sp
58 nigel 63 where a literal character follows. Before embarking on the standard matching
59     procedure, PCRE checks that there is a "b" later in the subject string, and if
60     there is not, it fails the match immediately. However, when there is no
61     following literal this optimization cannot be used. You can see the difference
62     by comparing the behaviour of
63 nigel 75 .sp
64     (a+)*\ed
65     .sp
66 nigel 63 with the pattern above. The former gives a failure almost instantly when
67     applied to a whole line of "a" characters, whereas the latter takes an
68     appreciable time with strings longer than about 20 characters.
69 nigel 75 .P
70     In many cases, the solution to this kind of performance issue is to use an
71     atomic group or a possessive quantifier.
72     .P
73 nigel 63 .in 0
74 nigel 75 Last updated: 09 September 2004
75 nigel 63 .br
76 nigel 75 Copyright (c) 1997-2004 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12