This is my first post to the monastery, so please take it easy on me :) I use Perl mostly as a hobby, and I have been slowly learning by reading various books and lurking around here. So, I think I finally have a question worth asking (most of the time a Super Search reveals that my question has already been answered). I have started to use Moose for anything object oriented and have found it to be very nice.

I recently wrote an application that creates a few thousand Moose objects. It seemed to run a bit slow so, I used Devel::DProf to help locate the bottleneck in my code. I was convinced that one of my methods would be involved. To my surprise, it seemed to be the combination of using Moose::Util::TypeConstraints with Regexp::Common

I have tried to reduce this down to a simple case based on the example given in Moose::Cookbook::Basics::Recipe4.

If I create the subtype using the technique in the recipe:

subtype USZipCodeA => as Value => where { /^$RE{zip}{US}{-extended => 'allow'}$/ };
it seems to run slower than if I create the subtype like this:
my $zip_re = qr/^$RE{zip}{US}{-extended => 'allow'}$/; subtype USZipCodeB => as Value => where { $_ =~ $zip_re; };
The code that I use for benchmarking and profiling the difference between these two techniques is behind the readmore tags.

In AddressA.pm:
package AddressA; use Moose; use Moose::Util::TypeConstraints; use Regexp::Common 'zip'; subtype USZipCodeA => as Value => where { /^$RE{zip}{US}{-extended => 'allow'}$/ }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeA'); 1;
In AddressB.pm:
package AddressB; use Moose; use Moose::Util::TypeConstraints; use Regexp::Common 'zip'; my $zip_re = qr/^$RE{zip}{US}{-extended => 'allow'}$/; subtype USZipCodeB => as Value => where { $_ =~ $zip_re; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeB'); 1;
Comparing A and B:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); use AddressA; use AddressB; my $zip = 12345; cmpthese (-5, { A => sub { AddressA->new(zip_code => $zip);}, B => sub { AddressB->new(zip_code => $zip);} } ); exit;

I'm using the following:

Typical results of the comparison are:
Rate A B A 2976/s -- -43% B 5187/s 74% --

I made a couple simple programs to create 10,000 objects using either technique. Then I used Devel::DProf to profile the code. The two highest values of 'ExclSec' for the creation of 10,000 AddressA objects were from these methods:

Regexp::Common::_decache Regexp::Common::new
and for AddressB objects none of the Regexp::Common methods show up and these two methods have the highest values of 'ExclSec'.
Class::MOP::Instance::new Class::MOP::Instance::set_slot_value
I don't want to focus too much on the DProf results, since dprofpp is giving me negative elapsed time values and some negative 'CumulS' values. I have no idea why.

I just tried this out on Mac OS X with perl 5.8.8 (macports) and the results of the comparison are (same Moose and Regexp::Common versions)

Rate A B A 2955/s -- -7% B 3174/s 7% --
So, it's not as noticeable on Mac OS X. I'm not sure why this differs so much from strawberry perl.

My question has a few parts:

I my own application, I had five or six subtypes and changing to the faster technique made my application noticeably faster. Thanks in advance for any insight you can offer. This experience resulted in me using Benchmark and Devel::DProf for the first time.


In reply to Slowdown when using Moose::Util::TypeConstraints with Regexp::Common by kevbot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.