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

Reply via email to