[Templates] New text.replace virtual method for v2.15

Josh Rosenbaum josh@infogears.com
Thu, 02 Feb 2006 22:29:43 -0700


Sergey Martynoff wrote:
>> I'm attaching a new test file you can run along with a file of 
>> benchmarks.
> 
> It was a good idea to benchmark the sub. At least it is obvious
> now, that replacement without backreferences should be handles
> separately, the simplest way. I ran the benchmark on my windows
> pc, and get similar (even more contrast) results.
> 
> Totals:
>                                       Rate      replace_andy
> replace_andy                  13224/s           --
> replace_andy_with_patch 20224/s          53%
> replace_josh_new2          27434/s         107%
> 
> 
> I don't think using no strict 'refs' is a big trouble (at least it
> is used quite often in so many modules), so IMO josh_new2 may be a
> good candidate.

Yeah, the main downside of josh_new2 is that we store the matches for 
each match iteration. So if we had a file like:
("a" x 5_000_000 . 'q') x 5
and did a replace like:
s/(a+q)/$1bc/g
then we'd store at least 5 million bytes in the hash causing that much 
of an increase in memory. (Probably more/double with different 
character encodings and such.) With the replace_andy(/paul) versions, 
they pull the data from the string already in memory. It's the classic 
memory vs CPU trade-off. If we're worried about multi-megabyte 
matches, then I think it might be best to go with 
'replace_andy_with_patch' which shows 'decent' average rates 
everywhere without using as much memory.

I do have one other idea I'm going to try out and see if it will work 
or not. I'll post that tomorrow if it pans out.

It's amazing how dog slow replacement gets when implemented in Perl. :P

-- Josh