in reply to Optimize a pluggable regex matching mechanism

For regexes with variables in them, it rebuilds the machine every time it uses the regex.

IIRC it's long since this is not the case anymore. Perl's checking a regex-cache to see if it has already compiled the pattern. That's why the /o flag is almost useless now (Beware: it can produce ugly side effects if not used with care)

(Though I couldn't find it stated in the perldocs :-( ... I think I should have read it in Friedl's regex "bible")

As Ikegami already suggested, stick with qr is certainly the cleanest approach to control compilation time, even with nested regexes, so better just forget /o! ¹

Cheers Rolf

UPDATE:

(1) or better only try to remember it as a possible optimization AFTER profiling shows a bottleneck!

  • Comment on Re: Optimize a pluggable regex matching mechanism (/o practically deprecated)

Replies are listed 'Best First'.
Re^2: Optimize a pluggable regex matching mechanism (/o practically deprecated)
by ikegami (Patriarch) on Nov 10, 2009 at 16:02 UTC

    Perl's checking a regex-cache to see if it has already compiled the pattern.

    No, the cache is not nearly so general. Each op simply remembers the last pattern it compiled.

    >perl -Mre=debug -e"/$_/ for qw( a a b a )" 2>&1 | find "Compiling" Compiling REx "a" Compiling REx "b" Compiling REx "a"
    >perl -Mre=debug -e"/a/; /a/;" 2>&1 | find "Compiling" Compiling REx "a" Compiling REx "a"

    Nothing's stopping you from making your own such cache, though.

    my $compiled_pat = $compiled_pats{$pat} ||= qr/$pat/;
      For completeness, the following code stresses out that the last compilation per code position is reused if possible.

      perl -Mre=debug -e '@x=qw/a b a a c a/; while (($a,$b,@x)=@x) { /$a/;/ +$b/ }' 2>&1|grep Compiling Compiling REx "a" Compiling REx "b" Compiling REx "a" Compiling REx "c"

      Each op remembers the last pattern it compiled.

      Yeah, that's kind of what I expected (was too lazy for details), but a general cache would be indeed real overkill.

      Both ways there is not much use left for /o, IMHO the perlre and perlretut should be updated with at least footnote...

      Cheers Rolf