github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
11 KiB

  1. .TH PCRE2PERFORM 3 "27 July 2022" "PCRE2 10.41"
  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.
  73. .P
  74. The size of each frame depends on the size of pointer variables and the number
  75. of capturing parenthesized groups in the pattern being matched. On a 64-bit
  76. system the frame size for a pattern with no captures is 128 bytes. For each
  77. capturing group the size increases by 16 bytes.
  78. .P
  79. Until release 10.41, an initial 20KiB frames vector was allocated on the system
  80. stack, but this still caused some issues for multi-thread applications where
  81. each thread has a very small stack. From release 10.41 backtracking memory
  82. frames are always held in heap memory. An initial heap allocation is obtained
  83. the first time any match data block is passed to \fBpcre2_match()\fP. This is
  84. remembered with the match data block and re-used if that block is used for
  85. another match. It is freed when the match data block itself is freed.
  86. .P
  87. The size of the initial block is the larger of 20KiB or ten times the pattern's
  88. frame size, unless the heap limit is less than this, in which case the heap
  89. limit is used. If the initial block proves to be too small during matching, it
  90. is replaced by a larger block, subject to the heap limit. The heap limit is
  91. checked only when a new block is to be allocated. Reducing the heap limit
  92. between calls to \fBpcre2_match()\fP with the same match data block does not
  93. affect the saved block.
  94. .P
  95. In contrast to \fBpcre2_match()\fP, \fBpcre2_dfa_match()\fP does use recursive
  96. function calls, but only for processing atomic groups, lookaround assertions,
  97. and recursion within the pattern. The original version of the code used to
  98. allocate quite large internal workspace vectors on the stack, which caused some
  99. problems for some patterns in environments with small stacks. From release
  100. 10.32 the code for \fBpcre2_dfa_match()\fP has been re-factored to use heap
  101. memory when necessary for internal workspace when recursing, though recursive
  102. function calls are still used.
  103. .P
  104. The "match depth" parameter can be used to limit the depth of function
  105. recursion, and the "match heap" parameter to limit heap memory in
  106. \fBpcre2_dfa_match()\fP.
  107. .
  108. .
  109. .SH "PROCESSING TIME"
  110. .rs
  111. .sp
  112. Certain items in regular expression patterns are processed more efficiently
  113. than others. It is more efficient to use a character class like [aeiou] than a
  114. set of single-character alternatives such as (a|e|i|o|u). In general, the
  115. simplest construction that provides the required behaviour is usually the most
  116. efficient. Jeffrey Friedl's book contains a lot of useful general discussion
  117. about optimizing regular expressions for efficient performance. This document
  118. contains a few observations about PCRE2.
  119. .P
  120. Using Unicode character properties (the \ep, \eP, and \eX escapes) is slow,
  121. because PCRE2 has to use a multi-stage table lookup whenever it needs a
  122. character's property. If you can find an alternative pattern that does not use
  123. character properties, it will probably be faster.
  124. .P
  125. By default, the escape sequences \eb, \ed, \es, and \ew, and the POSIX
  126. character classes such as [:alpha:] do not use Unicode properties, partly for
  127. backwards compatibility, and partly for performance reasons. However, you can
  128. set the PCRE2_UCP option or start the pattern with (*UCP) if you want Unicode
  129. character properties to be used. This can double the matching time for items
  130. such as \ed, when matched with \fBpcre2_match()\fP; the performance loss is
  131. less with a DFA matching function, and in both cases there is not much
  132. difference for \eb.
  133. .P
  134. When a pattern begins with .* not in atomic parentheses, nor in parentheses
  135. that are the subject of a backreference, and the PCRE2_DOTALL option is set,
  136. the pattern is implicitly anchored by PCRE2, since it can match only at the
  137. start of a subject string. If the pattern has multiple top-level branches, they
  138. must all be anchorable. The optimization can be disabled by the
  139. PCRE2_NO_DOTSTAR_ANCHOR option, and is automatically disabled if the pattern
  140. contains (*PRUNE) or (*SKIP).
  141. .P
  142. If PCRE2_DOTALL is not set, PCRE2 cannot make this optimization, because the
  143. dot metacharacter does not then match a newline, and if the subject string
  144. contains newlines, the pattern may match from the character immediately
  145. following one of them instead of from the very start. For example, the pattern
  146. .sp
  147. .*second
  148. .sp
  149. matches the subject "first\enand second" (where \en stands for a newline
  150. character), with the match starting at the seventh character. In order to do
  151. this, PCRE2 has to retry the match starting after every newline in the subject.
  152. .P
  153. If you are using such a pattern with subject strings that do not contain
  154. newlines, the best performance is obtained by setting PCRE2_DOTALL, or starting
  155. the pattern with ^.* or ^.*? to indicate explicit anchoring. That saves PCRE2
  156. from having to scan along the subject looking for a newline to restart at.
  157. .P
  158. Beware of patterns that contain nested indefinite repeats. These can take a
  159. long time to run when applied to a string that does not match. Consider the
  160. pattern fragment
  161. .sp
  162. ^(a+)*
  163. .sp
  164. This can match "aaaa" in 16 different ways, and this number increases very
  165. rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4
  166. times, and for each of those cases other than 0 or 4, the + repeats can match
  167. different numbers of times.) When the remainder of the pattern is such that the
  168. entire match is going to fail, PCRE2 has in principle to try every possible
  169. variation, and this can take an extremely long time, even for relatively short
  170. strings.
  171. .P
  172. An optimization catches some of the more simple cases such as
  173. .sp
  174. (a+)*b
  175. .sp
  176. where a literal character follows. Before embarking on the standard matching
  177. procedure, PCRE2 checks that there is a "b" later in the subject string, and if
  178. there is not, it fails the match immediately. However, when there is no
  179. following literal this optimization cannot be used. You can see the difference
  180. by comparing the behaviour of
  181. .sp
  182. (a+)*\ed
  183. .sp
  184. with the pattern above. The former gives a failure almost instantly when
  185. applied to a whole line of "a" characters, whereas the latter takes an
  186. appreciable time with strings longer than about 20 characters.
  187. .P
  188. In many cases, the solution to this kind of performance issue is to use an
  189. atomic group or a possessive quantifier. This can often reduce memory
  190. requirements as well. As another example, consider this pattern:
  191. .sp
  192. ([^<]|<(?!inet))+
  193. .sp
  194. It matches from wherever it starts until it encounters "<inet" or the end of
  195. the data, and is the kind of pattern that might be used when processing an XML
  196. file. Each iteration of the outer parentheses matches either one character that
  197. is not "<" or a "<" that is not followed by "inet". However, each time a
  198. parenthesis is processed, a backtracking position is passed, so this
  199. formulation uses a memory frame for each matched character. For a long string,
  200. a lot of memory is required. Consider now this rewritten pattern, which matches
  201. exactly the same strings:
  202. .sp
  203. ([^<]++|<(?!inet))+
  204. .sp
  205. This runs much faster, because sequences of characters that do not contain "<"
  206. are "swallowed" in one item inside the parentheses, and a possessive quantifier
  207. is used to stop any backtracking into the runs of non-"<" characters. This
  208. version also uses a lot less memory because entry to a new set of parentheses
  209. happens only when a "<" character that is not followed by "inet" is encountered
  210. (and we assume this is relatively rare).
  211. .P
  212. This example shows that one way of optimizing performance when matching long
  213. subject strings is to write repeated parenthesized subpatterns to match more
  214. than one character whenever possible.
  215. .
  216. .
  217. .SS "SETTING RESOURCE LIMITS"
  218. .rs
  219. .sp
  220. You can set limits on the amount of processing that takes place when matching,
  221. and on the amount of heap memory that is used. The default values of the limits
  222. are very large, and unlikely ever to operate. They can be changed when PCRE2 is
  223. built, and they can also be set when \fBpcre2_match()\fP or
  224. \fBpcre2_dfa_match()\fP is called. For details of these interfaces, see the
  225. .\" HREF
  226. \fBpcre2build\fP
  227. .\"
  228. documentation and the section entitled
  229. .\" HTML <a href="pcre2api.html#matchcontext">
  230. .\" </a>
  231. "The match context"
  232. .\"
  233. in the
  234. .\" HREF
  235. \fBpcre2api\fP
  236. .\"
  237. documentation.
  238. .P
  239. The \fBpcre2test\fP test program has a modifier called "find_limits" which, if
  240. applied to a subject line, causes it to find the smallest limits that allow a
  241. pattern to match. This is done by repeatedly matching with different limits.
  242. .
  243. .
  244. .SH AUTHOR
  245. .rs
  246. .sp
  247. .nf
  248. Philip Hazel
  249. Retired from University Computing Service
  250. Cambridge, England.
  251. .fi
  252. .
  253. .
  254. .SH REVISION
  255. .rs
  256. .sp
  257. .nf
  258. Last updated: 27 July 2022
  259. Copyright (c) 1997-2022 University of Cambridge.
  260. .fi