On Nov 11, 2006, at 11:11 PM, Stevan Little wrote:
Jonathan,
Unfortunately, the current object construction slowness will make
this a non-starter. e.g. On my powerbook it takes 25ms of CPU time
for Moose to construct a dead-simple object with a single accessor
and no inheritance.
Yes, this is a well known issue, and one I am currently working on
solving it.
A question though, is this benchmark including Class::MOP/Moose
compile time? Because that is something we cannot do too much about
at the moment, which means Moose is really only usable in
persistent environments like mod_perl/FastCGI/etc.
No, this does not include compile time - I do work in persistent
environments so I'd be willing to live with some startup costs.
Here's the benchmark source:
#!/usr/bin/perl
use Benchmark;
use strict;
use warnings;
{ package MoosePage;
use Moose;
has 'title' => (is => 'rw');
}
{ package ClassAccessorPage;
use strict;
use warnings;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw(title));
}
printf "Using Moose version %s, Class::MOP version %s,
Class::Accessor version %s\n", $Moose::VERSION, $Class::MOP::VERSION,
$Class::Accessor::VERSION;
timethese(1000, {
'MoosePage' => sub { my $page = new MoosePage(title =>
'article') },
'ClassAccessorPage' => sub { my $page = new ClassAccessorPage
({title => 'article'}) },
});
Results on my 1.5 GHz PowerPC G4:
swartz> ./bench.pl
Using Moose version 0.15, Class::MOP version 0.36,
Class::Accessor version 0.27
Benchmark: timing 1000 iterations of ClassAccessorPage, MoosePage...
ClassAccessorPage: 0 wallclock secs ( 0.02 usr + 0.00 sys =
0.02 CPU) @ 50000.00/s (n=1000)
(warning: too few iterations for a reliable count)
MoosePage: 22 wallclock secs (18.41 usr + 0.33 sys = 18.74 CPU)
@ 53.36/s (n=1000)
It's pretty simple - even the accessor declarations are superfluous
since I'm just calling constructors, though oddly if I take out the
'has' declaration Moose gets a little slower.
http://code2.0beta.co.uk/moose/svn/Moose/trunk/lib/Moose/Cookbook/
FAQ.pod mentions that immutable classes may eliminate, or at least
vastly improve, object construction performance, and implies this
will hopefully be done by the end of the year. Does this schedule
still sound reasonable?
There is currently an experimental branch of the immutable Moose
http://code2.0beta.co.uk/moose/svn/Moose/branches/Moose-immutable/
it requires this branch from Class-MOP as well
http://code2.0beta.co.uk/moose/svn/Class-MOP/branches/Class-MOP-
tranformations/
It is not yet finished, but the plan is to get it completely
working by the end of this month. Production worthiness will take a
little longer of course, probably close to that "end of the year"
timeline.
Early benchmarks show an average performance boost of 400-500%,
with a upper ceiling of 1200% for really simple constructors and a
bottom of about 175% for very complex constructors. A lot of this
is dependent upon the complexity of your attributes, and I do my
best to make sure you only pay for what you actually use.
This branch also includes a speed up version of the type constraint
system as well, which shows a performance boost of around 400% from
previous versions. This also contributes to the speedup in object
construction as well.
Ok! That's good to know. I can't afford to be that much on the
bleeding edge for this project, but I'll happily try out the next
CPAN release when it's available.
In the meantime, does anyone have a recommendation for an
alternate class framework I can use that will make the eventual
jump to Moose as painless as possible?
Well, it all depends on what Moose features you use.
We've just started out so it's hard to know which Moose features we'd
be using. The accessor and type declarations and default
constructors, of course - these are available elsewhere on CPAN but I
like Moose's syntax far better than others. Before and after methods
are nice too. Hopefully any class introspection we end up doing will
be "pay for what you use" as you suggest.
Have you thought about some global flag, or at least a per-class
flag, to enable/disable type constraint checking? It would make sense
to enable type checking for development servers and disable for
production servers.
One alternative, which I use myself, is to downgrade to Moose 0.11
(and Class::MOP 0.30 or so). It is still a little slow, but much
more manageable, especially if you are still in the development
phase where speed really shouldn't be that important.
Cool - I'll try these out in the benchmark.
Jon