Esteemed Monks,

I like the declarative syntax of Moose. I don't like the 728% performance hit I get compared to using a simple blessed hashref. Even Mo, minimal as it is, offers only a 27% increase in speed compared to Moose.

Benchmark Results
Benchmark: timing 1000000 iterations of blessed_hashref, hashref, mo, moose...
blessed_hashref:  1.80017 wallclock secs ( 1.79 usr +  0.00 sys =  1.79 CPU) @ 558659.22/s (n=1000000)
        hashref:  1.37839 wallclock secs ( 1.38 usr +  0.00 sys =  1.38 CPU) @ 724637.68/s (n=1000000)
             mo: 11.6823  wallclock secs (11.67 usr +  0.00 sys = 11.67 CPU) @  85689.80/s (n=1000000)
          moose: 14.8408  wallclock secs (14.82 usr +  0.00 sys = 14.82 CPU) @  67476.38/s (n=1000000)

                    Rate        moose            mo blessed_hashref      hashref
moose            67476/s           --          -21%            -88%         -91%
mo               85690/s          27%            --            -85%         -88%
blessed_hashref 558659/s         728%          552%              --         -23%
hashref         724638/s         974%          746%             30%           --
Benchmark Script:
#!/usr/bin/perl -w package MooseState; use Moose; has 'name' => ( is => 'ro', isa => 'Str', required => 1, ); has 'capital' => ( is => 'ro', isa => 'Str', required => 1, ); has 'population' => ( is => 'rw', isa => 'Int', required => 1, ); __PACKAGE__->meta->make_immutable(); package MoState; use Mo qw(required); has 'name' => ( is => 'ro', isa => 'Str', required => 1, ); has 'capital' => ( is => 'ro', isa => 'Str', required => 1, ); has 'population' => ( is => 'rw', isa => 'Int', required => 1, ); package main; use strict; use warnings 'all'; use Benchmark qw( :all :hireswallclock ); my %args = ( name => 'Colorado', capital => 'Denver', population => 5_000_000, ); my $results = timethese(1_000_000, { blessed_hashref => \&blessed_hashref, hashref => \&hashref, moose => \&moose, mo => \&mo, }); cmpthese($results); sub blessed_hashref { my $state = bless { %args }, 'Foo'; }# end blessed_hashref() sub hashref { my $state = { %args }; }# end hashref() sub moose { my $state = MooseState->new( %args ); }# end moose() sub mo { my $state = MoState->new( %args ); }# end mo()

Is there something that can be done to make constructing Moose/Mo objects much, much faster?

Update

I see now that this is a stacked comparison in that my benchmark script is having Moose do more work than it should (checking isa and required fields). As Your Mother pointed out, Mouse::XS may just be the ticket, as it's Moosey and still very fast.


In reply to The speed of blessed hashrefs, the power of Moose - how to get both? by jdrago999

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.