in reply to Slowdown when using Moose::Util::TypeConstraints with Regexp::Common

Useing
my $zip_re = qr/^$RE{zip}{US}{-extended => 'allow'}$/;
compiles your regex at compile time once and uses that precompiled piece of code in all your object-constructions, whereas useing
/^$RE{zip}{US}{-extended => 'allow'}$/
in your Moose-code will have to recompile your regex-code every-time again.

It doesn't explain why Perl on Mac OS X doesn't show as big a slowdown as Strawberry Perl.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Slowdown when using Moose::Util::TypeConstraints with Regexp::Common
by kevbot (Vicar) on Sep 02, 2008 at 20:38 UTC

    I added a couple other tests (AddressC and AddressD) to the mix. Basically, I wanted to take Regexp::Common out of the code to see if a standard regexp showed the same behavior. So, I grabbed the regular expression from Regexp::Common and placed it in the code directly. In AddressC, I put it directly in the subtype (similar to Address A):

    subtype USZipCodeC => as Value => where { $_ =~ /(?-xism:^(?:(?:(?:USA?)-){0,1}(?:(?:(?:[0-9]{3})(?: +[0-9]{2}))(?:(?:-)(?:(?:[0-9]{2})(?:[0-9]{2}))){0,1}))$)/; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeC');

    For AddressD, I compile the regexp (similar to AddressB):

    my $zip_re = qr/(?-xism:^(?:(?:(?:USA?)-){0,1}(?:(?:(?:[0-9]{3})(?:[0- +9]{2}))(?:(?:-)(?:(?:[0-9]{2})(?:[0-9]{2}))){0,1}))$)/; subtype USZipCodeD => as Value => where { $_ =~ $zip_re; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeD');
    So, I would expect that AddressB and AddressD would be the fastest. Here the result of a benchmark on my WinXP/Strawberry Perl 5.10 setup:
    Rate A D C B A 2960/s -- -42% -42% -42% D 5061/s 71% -- -1% -1% C 5099/s 72% 1% -- -0% B 5108/s 73% 1% 0% --

    The method using Regexp::Common directly in the Subtype is the slowest of all the techniques (AddressA). Basically, the other three are tied. So, the compiled regexp doesn't seem to make a difference as long as I avoid Regexp::Common.

      Basically all your versions, except AdressA, now use a single static regex, so they get compiled once at compile time, hence few or no differences. The Regexp::Common version cannot be optimized in that way, unless you force it to become static by using qr//. Such is the price you pay for flexibility.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Actually, using Regexp::Common w/o qr// should result in the regex being compiled the first time that it is used. Each time it is used after that, a string comparison should be used to see if the regex needs to be recompiled. So it should be only compiled once and only an extra string compare will be done each time it is run.

        - tye        

        Thank you for your replies. They have been helpful. It was not obvious to me that the AddressC regexp would be compiled once at compile time...but it makes sense to me now.