pcre2perform.3 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. .TH PCRE2PERFORM 3 "03 February 2019" "PCRE2 10.33"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "PCRE2 PERFORMANCE"
  5. .rs
  6. .sp
  7. Two aspects of performance are discussed below: memory usage and processing
  8. time. The way you express your pattern as a regular expression can affect both
  9. of them.
  10. .
  11. .SH "COMPILED PATTERN MEMORY USAGE"
  12. .rs
  13. .sp
  14. Patterns are compiled by PCRE2 into a reasonably efficient interpretive code,
  15. so that most simple patterns do not use much memory for storing the compiled
  16. version. However, there is one case where the memory usage of a compiled
  17. pattern can be unexpectedly large. If a parenthesized group has a quantifier
  18. with a minimum greater than 1 and/or a limited maximum, the whole group is
  19. repeated in the compiled code. For example, the pattern
  20. .sp
  21. (abc|def){2,4}
  22. .sp
  23. is compiled as if it were
  24. .sp
  25. (abc|def)(abc|def)((abc|def)(abc|def)?)?
  26. .sp
  27. (Technical aside: It is done this way so that backtrack points within each of
  28. the repetitions can be independently maintained.)
  29. .P
  30. For regular expressions whose quantifiers use only small numbers, this is not
  31. usually a problem. However, if the numbers are large, and particularly if such
  32. repetitions are nested, the memory usage can become an embarrassment. For
  33. example, the very simple pattern
  34. .sp
  35. ((ab){1,1000}c){1,3}
  36. .sp
  37. uses over 50KiB when compiled using the 8-bit library. When PCRE2 is
  38. compiled with its default internal pointer size of two bytes, the size limit on
  39. a compiled pattern is 65535 code units in the 8-bit and 16-bit libraries, and
  40. this is reached with the above pattern if the outer repetition is increased
  41. from 3 to 4. PCRE2 can be compiled to use larger internal pointers and thus
  42. handle larger compiled patterns, but it is better to try to rewrite your
  43. pattern to use less memory if you can.
  44. .P
  45. One way of reducing the memory usage for such patterns is to make use of
  46. PCRE2's
  47. .\" HTML <a href="pcre2pattern.html#subpatternsassubroutines">
  48. .\" </a>
  49. "subroutine"
  50. .\"
  51. facility. Re-writing the above pattern as
  52. .sp
  53. ((ab)(?2){0,999}c)(?1){0,2}
  54. .sp
  55. reduces the memory requirements to around 16KiB, and indeed it remains under
  56. 20KiB even with the outer repetition increased to 100. However, this kind of
  57. pattern is not always exactly equivalent, because any captures within
  58. subroutine calls are lost when the subroutine completes. If this is not a
  59. problem, this kind of rewriting will allow you to process patterns that PCRE2
  60. cannot otherwise handle. The matching performance of the two different versions
  61. of the pattern are roughly the same. (This applies from release 10.30 - things
  62. were different in earlier releases.)
  63. .
  64. .
  65. .SH "STACK AND HEAP USAGE AT RUN TIME"
  66. .rs
  67. .sp
  68. From release 10.30, the interpretive (non-JIT) version of \fBpcre2_match()\fP
  69. uses very little system stack at run time. In earlier releases recursive
  70. function calls could use a great deal of stack, and this could cause problems,
  71. but this usage has been eliminated. Backtracking positions are now explicitly
  72. remembered in memory frames controlled by the code. An initial 20KiB vector of
  73. frames is allocated on the system stack (enough for about 100 frames for small
  74. patterns), but if this is insufficient, heap memory is used. The amount of heap
  75. memory can be limited; if the limit is set to zero, only the initial stack
  76. vector is used. Rewriting patterns to be time-efficient, as described below,
  77. may also reduce the memory requirements.
  78. .P
  79. In contrast to \fBpcre2_match()\fP, \fBpcre2_dfa_match()\fP does use recursive
  80. function calls, but only for processing atomic groups, lookaround assertions,
  81. and recursion within the pattern. The original version of the code used to
  82. allocate quite large internal workspace vectors on the stack, which caused some
  83. problems for some patterns in environments with small stacks. From release
  84. 10.32 the code for \fBpcre2_dfa_match()\fP has been re-factored to use heap
  85. memory when necessary for internal workspace when recursing, though recursive
  86. function calls are still used.
  87. .P
  88. The "match depth" parameter can be used to limit the depth of function
  89. recursion, and the "match heap" parameter to limit heap memory in
  90. \fBpcre2_dfa_match()\fP.
  91. .
  92. .
  93. .SH "PROCESSING TIME"
  94. .rs
  95. .sp
  96. Certain items in regular expression patterns are processed more efficiently
  97. than others. It is more efficient to use a character class like [aeiou] than a
  98. set of single-character alternatives such as (a|e|i|o|u). In general, the
  99. simplest construction that provides the required behaviour is usually the most
  100. efficient. Jeffrey Friedl's book contains a lot of useful general discussion
  101. about optimizing regular expressions for efficient performance. This document
  102. contains a few observations about PCRE2.
  103. .P
  104. Using Unicode character properties (the \ep, \eP, and \eX escapes) is slow,
  105. because PCRE2 has to use a multi-stage table lookup whenever it needs a
  106. character's property. If you can find an alternative pattern that does not use
  107. character properties, it will probably be faster.
  108. .P
  109. By default, the escape sequences \eb, \ed, \es, and \ew, and the POSIX
  110. character classes such as [:alpha:] do not use Unicode properties, partly for
  111. backwards compatibility, and partly for performance reasons. However, you can
  112. set the PCRE2_UCP option or start the pattern with (*UCP) if you want Unicode
  113. character properties to be used. This can double the matching time for items
  114. such as \ed, when matched with \fBpcre2_match()\fP; the performance loss is
  115. less with a DFA matching function, and in both cases there is not much
  116. difference for \eb.
  117. .P
  118. When a pattern begins with .* not in atomic parentheses, nor in parentheses
  119. that are the subject of a backreference, and the PCRE2_DOTALL option is set,
  120. the pattern is implicitly anchored by PCRE2, since it can match only at the
  121. start of a subject string. If the pattern has multiple top-level branches, they
  122. must all be anchorable. The optimization can be disabled by the
  123. PCRE2_NO_DOTSTAR_ANCHOR option, and is automatically disabled if the pattern
  124. contains (*PRUNE) or (*SKIP).
  125. .P
  126. If PCRE2_DOTALL is not set, PCRE2 cannot make this optimization, because the
  127. dot metacharacter does not then match a newline, and if the subject string
  128. contains newlines, the pattern may match from the character immediately
  129. following one of them instead of from the very start. For example, the pattern
  130. .sp
  131. .*second
  132. .sp
  133. matches the subject "first\enand second" (where \en stands for a newline
  134. character), with the match starting at the seventh character. In order to do
  135. this, PCRE2 has to retry the match starting after every newline in the subject.
  136. .P
  137. If you are using such a pattern with subject strings that do not contain
  138. newlines, the best performance is obtained by setting PCRE2_DOTALL, or starting
  139. the pattern with ^.* or ^.*? to indicate explicit anchoring. That saves PCRE2
  140. from having to scan along the subject looking for a newline to restart at.
  141. .P
  142. Beware of patterns that contain nested indefinite repeats. These can take a
  143. long time to run when applied to a string that does not match. Consider the
  144. pattern fragment
  145. .sp
  146. ^(a+)*
  147. .sp
  148. This can match "aaaa" in 16 different ways, and this number increases very
  149. rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
  150. times, and for each of those cases other than 0 or 4, the + repeats can match
  151. different numbers of times.) When the remainder of the pattern is such that the
  152. entire match is going to fail, PCRE2 has in principle to try every possible
  153. variation, and this can take an extremely long time, even for relatively short
  154. strings.
  155. .P
  156. An optimization catches some of the more simple cases such as
  157. .sp
  158. (a+)*b
  159. .sp
  160. where a literal character follows. Before embarking on the standard matching
  161. procedure, PCRE2 checks that there is a "b" later in the subject string, and if
  162. there is not, it fails the match immediately. However, when there is no
  163. following literal this optimization cannot be used. You can see the difference
  164. by comparing the behaviour of
  165. .sp
  166. (a+)*\ed
  167. .sp
  168. with the pattern above. The former gives a failure almost instantly when
  169. applied to a whole line of "a" characters, whereas the latter takes an
  170. appreciable time with strings longer than about 20 characters.
  171. .P
  172. In many cases, the solution to this kind of performance issue is to use an
  173. atomic group or a possessive quantifier. This can often reduce memory
  174. requirements as well. As another example, consider this pattern:
  175. .sp
  176. ([^<]|<(?!inet))+
  177. .sp
  178. It matches from wherever it starts until it encounters "<inet" or the end of
  179. the data, and is the kind of pattern that might be used when processing an XML
  180. file. Each iteration of the outer parentheses matches either one character that
  181. is not "<" or a "<" that is not followed by "inet". However, each time a
  182. parenthesis is processed, a backtracking position is passed, so this
  183. formulation uses a memory frame for each matched character. For a long string,
  184. a lot of memory is required. Consider now this rewritten pattern, which matches
  185. exactly the same strings:
  186. .sp
  187. ([^<]++|<(?!inet))+
  188. .sp
  189. This runs much faster, because sequences of characters that do not contain "<"
  190. are "swallowed" in one item inside the parentheses, and a possessive quantifier
  191. is used to stop any backtracking into the runs of non-"<" characters. This
  192. version also uses a lot less memory because entry to a new set of parentheses
  193. happens only when a "<" character that is not followed by "inet" is encountered
  194. (and we assume this is relatively rare).
  195. .P
  196. This example shows that one way of optimizing performance when matching long
  197. subject strings is to write repeated parenthesized subpatterns to match more
  198. than one character whenever possible.
  199. .
  200. .
  201. .SS "SETTING RESOURCE LIMITS"
  202. .rs
  203. .sp
  204. You can set limits on the amount of processing that takes place when matching,
  205. and on the amount of heap memory that is used. The default values of the limits
  206. are very large, and unlikely ever to operate. They can be changed when PCRE2 is
  207. built, and they can also be set when \fBpcre2_match()\fP or
  208. \fBpcre2_dfa_match()\fP is called. For details of these interfaces, see the
  209. .\" HREF
  210. \fBpcre2build\fP
  211. .\"
  212. documentation and the section entitled
  213. .\" HTML <a href="pcre2api.html#matchcontext">
  214. .\" </a>
  215. "The match context"
  216. .\"
  217. in the
  218. .\" HREF
  219. \fBpcre2api\fP
  220. .\"
  221. documentation.
  222. .P
  223. The \fBpcre2test\fP test program has a modifier called "find_limits" which, if
  224. applied to a subject line, causes it to find the smallest limits that allow a
  225. pattern to match. This is done by repeatedly matching with different limits.
  226. .
  227. .
  228. .SH AUTHOR
  229. .rs
  230. .sp
  231. .nf
  232. Philip Hazel
  233. University Computing Service
  234. Cambridge, England.
  235. .fi
  236. .
  237. .
  238. .SH REVISION
  239. .rs
  240. .sp
  241. .nf
  242. Last updated: 03 February 2019
  243. Copyright (c) 1997-2019 University of Cambridge.
  244. .fi