XML technologies / Regular expressions / PHP / Efficiency ⟦U+0002⟧ considerations
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The preg functions in PHP use PCRE, an optimized NFA regex engine, so most techniques from How regex engines work through The art of writing regular expressions apply directly. This includes benchmarking critical code to see what is fast in practice, not just in theory. The art of writing regular expressions includes a PHP example (see PHP benchmarks).
For particularly critical code, remember that callbacks are generally faster than the e modifier (see Callback or e modifier?), and named captures can trigger expensive copying with very long strings.
Regexes are compiled when encountered at runtime, but PHP uses a large cache of 4096 entries (see Compilation caching), so in practice each regex string is compiled only once.
The S modifier merits more discussion: it analyzes the regex further to find additional optimizations. This differs from the identically named study operator in Perl, which optimizes the subject string. The S modifier optimizes the regex; see The study function.
The S modifier
Using the S modifier makes the preg engine spend a little (note: really just a little—even for long, complex regexes, extra optimization takes less than a hundred-thousandth of a second on an average machine) more time optimizing, hoping to recover the cost during matching. There may be no net gain, but some cases speed up by orders of magnitude.
In the current implementation, the situations benefiting from the S modifier are well defined: it applies “first-character optimization”.
First, note that this optimization helps only when applying a regex to large amounts of text.
Ordinary optimization without S
Consider a simple regex such as ˹<(\w+)˼. Every possible match must begin with ›<‹. An engine can exploit this, as preg does, by finding that character first and trying the full regex only there. If a match must begin with ˹<˼, trying other characters is pointless.
A simple string search selects these positions much faster than a full regex search, especially if the character is rare. Complex regexes also make this advantage greater. For example, it helps more with ˹<i>|</i>|<b>|</b>˼ than with ˹<(\w+)˼, since the engine would otherwise try all four branches in the first case.
Improved optimization with S
Preg usually performs this optimization automatically when every match must start with one character. The S modifier requests deeper analysis, applying the optimization when several starting characters are possible too.
Here are some expressions where the S modifier forces this optimization:
regex | Possible starting characters |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
When does S not help?
Cases where the S modifier has no effect are instructive too:
Regexes with a start-of-line anchor,
˹^˼and˹\b˼, or top-level alternation with an anchored branch. For˹\b˼, this is a limitation of the current implementation; the optimization could in principle apply.Expressions that can match an empty string, such as
˹\s*˼.Expressions that can start with any or very many characters, such as
˹(?:[^()]++|\((?R)\))*˼from the example under Parsing text with nested parentheses. A match can begin with any character except ›)‹, so preselecting starting characters adds almost nothing.Expressions with exactly one possible starting character: these are optimized automatically without the S modifier.
When should S be used?
Extra analysis costs little, and for large subject texts it is negligible beside total search time. If you think the regex could benefit, it may well pay off.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln