On Sun, 15 Nov 2009 21:02:25 -0800 abhijit mahabal <amaha...@gmail.com> wrote:
> Hi Moose folks, > I recently began converting my PhD code from Class::Std to > MooseX::Declare. (I started to write that code over 5 years ago, hence > Class::Std). > > I have run into a slowness roadblock, however, and I feel I must be > doing something wrong. I was under the impression that I would only be > paying compile time costs, but the runtime costs I have run into are > prohibitive. I have included a benchmark script below. > > I am using perl5.10.1 on Ubuntu, MX::Declare 0.32, > MooseX::Method::Signatures 0.29. > > ============ > Rate method_without_type method_with_type > regular_sub > method_without_type 4374/s -- > -7% -99% method_with_type 4721/s > 8% -- -99% regular_sub > 389610/s 8808% 8152% -- > ============= use MooseX::Declare; > > class Foo { > has x => ( > is => 'rw', > isa => 'Str', > ); > > # I use the three vars below to ensure that the subs > # are not constant-folded away. > our $meth_counter = 0; > our $meth_typed_counter = 0; > our $regular_sub_counter = 0; > > method method_with_type( Int $x ) { > $meth_typed_counter++; > $self->x . $x > }; > > method method_without_type($x) { > $meth_counter++; > $self->x . $x > }; > > sub regular_sub { > my ( $self, $x ) = @_; > $regular_sub_counter++; > $self->x . $x; > } > > }; > > package main; > use Benchmark qw(:all); > > my $foo = Foo->new( { x => "3" } ); > cmpthese( > 300000, > { > regular_sub => sub { $foo->regular_sub(5) }, > method_without_type => sub { $foo->method_without_type(5) }, > method_with_type => sub { $foo->method_with_type(5) } > } > ); > ============= > > > Is this expected? If not, what am I doing wrong? And why does adding a > type *speed* things up (by 8% in the example above)? That's > counterintuitive, to me. > > Thanks much, > --abhijit > You have to understand what kind of magic is happening behind the scenes to realize that methods can be heavy. First, the type constraint system alone will slow things down. Also, the provided arguments to a method are munged to allow for things like named parameters. And then the sub is basically rewritten (MXD is a glorified source filter but using perl to parse Perl) to account for all of that. If you are interested in the gory details, take a look inside MooseX::Method::Signatures::Meta::Method. So that answers to the general slowness, as for why adding a type speeds things up, that is a question for Florian. You are welcome to join the #devel-declare channel on irc.perl.org, most of the MXD people hang out there. -- Nicholas Perez XMPP/Email: n...@nickandperla.net http://search.cpan.org/~nperez/ http://github.com/nperez