Subject: | Regex Slowdown: XPath.pm:195 Uses ($`) |
On line 195 in XML::XPath within the setNodeText() function:
my $parent_path = $`;
This one line causes the ENTIRE perl regular expression engine to slow
down dramatically. Furthermore, any libraries that simply "use
XML::XPath" experience this same slowdown -- even if the the
setNodeText() function is never called.
This slowdown is due to the native Perl interpreter -- as stated in
"perldoc perlre":
WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in
the program, it has to provide them for every pattern match. This may
substantially slow your program. Perl uses the same mechanism to pro-
duce $1, $2, etc, so you also pay a price for each pattern that con-
tains capturing parentheses. (To avoid this cost while retaining the
grouping behaviour, use the extended regular expression "(?: ... )"
instead.) But if you never use $&, $` or $', then patterns without
capturing parentheses will not be penalized. So avoid $&, $', and $`
if you can, but if you can't (and some algorithms really appreciate
them), once you've used them once, use them at will, because you've
already paid the price. As of 5.005, $& is not so costly as the other
two.
If there is ANY way to alter line 195 such that $&, $`, and $' are never
used, then XPath and all subsequent dependencies will obtain a huge
performance boost (within the regular expression engine).