[perl #49190] Don’t prematurely optimise s/foo/bar$baz/
authorFather Chrysostomos <sprout@cpan.org>
Thu, 11 Oct 2012 21:38:31 +0000 (14:38 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Fri, 12 Oct 2012 06:07:36 +0000 (23:07 -0700)
commitbb933b9ba3a34a454c83adeaf265bd1d4eb466a1
tree4e9aaa05b3cb4776e4ae4ab925e395309f9cfe8b
parent52c47e1631d20f2f6b5ebaf188f61d5470d887f3
[perl #49190] Don’t prematurely optimise s/foo/bar$baz/

$baz could be aliased to a package variable, so we do need to recon-
catenate for every iteration of s///g.  For s/// without /g, only one
more op will be executed, so the speed difference is negligible.

The only cases we can optimise in terms of skipping the evaluation of
the ops on the rhs (by eliminating the substconst op) are s//constant/
and s//$single_variable/.  Anything more complicated causes bugs.

A recent commit made s/foo/$bar/g re-stringify $bar for each iteration
(though without having to reevaluate the ops that return $bar).  So we
no longer have to special-case match vars at compile time.

This means that s/foo/bar$baz/g will be slower (and less buggy), but
s/foo/$1/g will be faster.

This also caused an existing taint but in pp_subst to surface.  If
get-magic turns off taint on a replacement string, it should not be
considered tainted.  So the taint check on the replacement should come
*after* the stringification.  This applies to the constant replacement
optimisation.  pp_substcont was already doing this correctly.
ext/B/t/optree_misc.t
ext/B/t/walkoptree.t
op.c
pp_hot.c
t/re/subst.t