After deciding to use Moose and Roles for my next project I checked out the speed penalty I had to expect. After finding out that Introspection (i.e. 'does') was horribly slow, but the rest in expected ranges I tried the speedup promised by  __PACKAGE__->meta->make_immutable;. Strangely no speedup at all:
#!/usr/bin/perl use strict; use warnings; use Moose; use Benchmark qw( cmpthese ); package MyImmuClass; use Moose; sub setsize { my $self= shift; $self->{size}= shift; } has 'size',is => 'rw', isa => 'Int'; __PACKAGE__->meta->make_immutable; package MyClass; use Moose; sub setsize { my $self= shift; $self->{size}= shift; } sub getsize { my $self= shift; $self->{size}; } has 'size',is => 'rw', isa => 'Int'; package main; my $obj= MyClass->new; my $immuobj= MyImmuClass->new; my $x; my %tests = ( Moose => sub { $x= $obj->size; }, 'hash access' => sub { $x= $obj->{size}; }, MooseImmu => sub { $x= $immuobj->size; }, accessor => sub { $x= $obj->getsize } ); cmpthese(-3, \%tests); %tests = ( Moose => sub { $obj->size(21); }, MooseImmu => sub { $immuobj->size(21); }, 'hash access' => sub { $obj->{size}=20; }, accessor => sub { $obj->setsize(22); } ); cmpthese(-3, \%tests); #------------------------------------------------------ Rate MooseImmu Moose accessor hash access MooseImmu 1505277/s -- -1% -2% -82% Moose 1517026/s 1% -- -2% -82% accessor 1542411/s 2% 2% -- -81% hash access 8232304/s 447% 443% 434% -- Rate Moose MooseImmu accessor hash access Moose 413580/s -- -1% -69% -92% MooseImmu 419426/s 1% -- -69% -91% accessor 1335932/s 223% 219% -- -73% hash access 4874075/s 1079% 1062% 265% --

As you can see, no speedup at all. The only speedup I did find was with Introspection, 'does' was 4 times faster now, but still 26 times slower than 'can', the duck typing alternative.

make_immutable should make accessors faster through 'inlining'. The tests seem to show that either this happens anyway or it doesn't work. My question is: Which one and why?

PS: If anyone is astonished that Moose is slower than an accessor on write, he should take into account that there is parameter checking built in

UPDATE:

FunkyMonk: Ah, the information I looked at was for an older version of Moose. CASE CLOSED. Explanation found

zwon, thanks for the debugging, corrected. Your observation is also quite probable, see my PS which makes the same point. Interestingly the speed is slightly better if 'Str' is used, but still with a big gap to the non-moose accessor, so even with Str there is some (unnecessary?) checking going on


In reply to Moose immutable speedup? by jethro

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.