Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

The speed of blessed hashrefs, the power of Moose - how to get both?

by jdrago999 (Pilgrim)
on Nov 19, 2011 at 22:41 UTC ( #939011=perlmeditation: print w/ replies, xml ) Need Help??

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.

Comment on The speed of blessed hashrefs, the power of Moose - how to get both?
Download Code
Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by Anonymous Monk on Nov 20, 2011 at 00:19 UTC

    One of these days you lot are going to learn that sometimes the simplest answer is infact the best, that all these high-faluting technical wizardry incantations do nothing but slow Perl down by trying to make it be something that it was never intended to be, nor will it ever be.

    Object orientation is programmer masturbation, a passing fad, and truly a bad idea that really does make programmers blind.

    I pity you lot. I really do, the lengths you will go to try and force Perl be something other than Perl.... Why? Because after years of jacking off about how wonderful object orientation is, you've completely and utterly missed the point of Perl, to such a degree that your planning to kill off older versions which work perfectly well on millions of servers in your pursuit of a technological pipe-dream of elegance.

    And what do you get? Yet another computer linguistic straight jacket like JAVA which does nothing but tie you and the lady Perl up in knots and decimate performance.

    I guess you just love your bondage huh... well not me.

    OOP? NO THANKS... I'll stick with procedural design directed by simple dynamic declarative statements, and run rings around your designs in terms of performance per $$$.

      "One of these days you lot are going to learn that sometimes the simplest answer is infact the best, that all these high-faluting technical wizardry incantations do nothing but slow Perl down by trying to make it be something that it was never intended to be, nor will it ever be."

      Please re-read my post. I'm just looking to discuss performance - maybe there's something I've missed, or some kind of modifier or something like that. Can Moose be fast, *and* powerful at the same time?

        The short answer is "no".

        You've stumbled upon one of the reasons why Larry abandoned Perl 5 development over a decade ago.

        Moose, Mo, et al are very nice, but they are fundamentally kludgey bolt-on's intended to make the kludgey bolt-on that is P.OOP, look a bit nicer.

        Perl was never intended to be used like that, and it's only the sheer power of modern hardware that allows us to get away with it while we wait for Larry and co to finally ship Perl 6 which has been designed from the ground up to support such things.

        As far as I know after 11 years of work by most of the top minds in Perl, we are yet to see a version of it that is capable of compiling all of it's own syntax or running at a speed greater than a 1 legged, cross eyed saint Bernard.

        And of course whilst they are all busy doing that, and the next tier down of top minds are busy trying to make Perl 5 pretend it's Perl 6, the rest of the Perl world are left to struggle on by themselves and make the best they can of the situation.

        Such a shame as well, you see all the employers asking for all the latest buzzwords, and I bet you not one in 10 of them really understand why they want those buzzwords, just that they are the latest buzzwords and therefore they are probably good. They are shooting themselves in the foot in terms of performance and scalability without even realising it.

        moose : 67476/s hashref : 724638/s
        724638 / 67476 = 10.739 = deci-mated.
        And what does it bring us, in real terms?

      Object orientation is programmer masturbation, a passing fad, and truly a bad idea that really does make programmers blind.

      Yikes! I'm not sure I would call 40+ years of OO (17+ for perl5) a 'passing fad.' Excessive self-gratification, sure at times, but that's true with any language or style (Java, C++, procedural, functional, etc). In my experience, the language and/or technology is rarely a problem (give me the right assembly programmers and I can rule the world). Just like Eddie Wilson said it's 'words and music,' for us it's 'developer and tool.' The right pairing is a thing of beauty and the wrong pairing is destined for the unemployment line and the waste heap of history.

      -derby
        The problem is how it's been promoted as the be all and end all, all singing all dancing way of programming. Managers who don't know any better actually believe that crap!

        Sure sometimes using OO can be useful, I'm struggling to think of an occasion, but I'm sure there are examples.

        I still think it's a passing fad, and Q.I.S will have nothing to do with it.

        give me the right assembly programmers and I can rule the world

        You're a bit late. They already do rule the world (of computing). It's all that low-level stuff that turns your computer on when you press that power button... ;-)

        Just like Eddie Wilson said it's 'words and music,' for us it's 'developer and tool.' The right pairing is a thing of beauty and the wrong pairing is destined for the unemployment line and the waste heap of history.

        I fully agree to that statement. While a good developer can use any programming language, only the right language - matched to the developers mindset and the given problem - will lead to an elegant solution.

        Don't use '#ff0000':
        use Acme::AutoColor; my $redcolor = RED();
        All colors subject to change without notice.
Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by Your Mother (Canon) on Nov 20, 2011 at 02:55 UTC
    Testing Perl 5.012002, Moose 1.24, Mouse 0.91, Moo 0.009007, Object::T +iny 1.08, Object::Tiny::XS 1.01 Benchmark: timing 6000000 iterations of Moo, Moo w/quote_sub, Moose, M +ouse, Object::Tiny, Object::Tiny::XS, hash, manual, manual with no ch +ecks... Object::Tiny::XS: 1 secs ( 1.20 usr + -0.01 sys = 1.19 CPU) @ 504201 +6.81/s hash, no check : 3 secs ( 1.86 usr + 0.01 sys = 1.87 CPU) @ 320855 +6.15/s Mouse : 3 secs ( 3.66 usr + 0.00 sys = 3.66 CPU) @ 163934 +4.26/s Object::Tiny : 3 secs ( 3.80 usr + 0.00 sys = 3.80 CPU) @ 157894 +7.37/s hash : 5 secs ( 5.53 usr + 0.01 sys = 5.54 CPU) @ 108303 +2.49/s manual, no check: 9 secs ( 9.11 usr + 0.02 sys = 9.13 CPU) @ 65717 +4.15/s Moo : 17 secs (17.37 usr + 0.03 sys = 17.40 CPU) @ 34482 +7.59/s manual : 17 secs (17.89 usr + 0.02 sys = 17.91 CPU) @ 33500 +8.38/s Mouse no XS : 20 secs (20.50 usr + 0.03 sys = 20.53 CPU) @ 29225 +5.24/s Moose : 21 secs (21.33 usr + 0.03 sys = 21.36 CPU) @ 28089 +8.88/s Moo w/quote_sub : 23 secs (23.07 usr + 0.04 sys = 23.11 CPU) @ 25962 +7.87/s

    Mouse(::XS) is probably what you want: Source. Depending on how deep you want to go with Roles and Traits it’s nearly identical code-wise. This is your benchmark with a Mouse addition–

    Rate moose mo mouse blessed_hashref + hashref moose 55157/s -- -22% -67% -86% + -88% mo 71023/s 29% -- -58% -82% + -85% mouse 168919/s 206% 138% -- -56% + -65% blessed_hashref 384615/s 597% 442% 128% -- + -20% hashref 478469/s 767% 574% 183% 24% + --
Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by zwon (Monsignor) on Nov 20, 2011 at 03:28 UTC
    sub blessed_hashref { my $state = bless { %args }, 'Foo'; }

    You are not checking arguments here. In definition of MooseState you specified things like isa => 'Int', so constructor checks arguments. You're testing constructions which are not functionally equal. Remove these isa or add checks to blessed_hashref to get something that makes sense.

      This is a very important point, it's a stacked comparison. You can either put in checks in the hashref (though it might provoke argument over what exactly would be equivalent), or you can take out the checks from Moose.
        If you're going to take the checks out, why use Moose? Seriously, just use a href directly. (When do you really need method calls to attributes? Encapsulation? Hello, mantra. You're adding these unnecessary layers when you don't really need it. If you need it later, add it later! Perl is not compiled. Why are you creating a static API?)
Reaped: Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by NodeReaper (Curate) on Nov 21, 2011 at 13:04 UTC
Reaped: Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by NodeReaper (Curate) on Nov 25, 2011 at 12:58 UTC
Re: The speed of blessed hashrefs, the power of Moose - how to get both?
by thargas (Chaplain) on Oct 24, 2012 at 14:06 UTC
    Don't forget Moo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://939011]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-08-27 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (252 votes), past polls