Playing around with Method::Signatures::Simple

Method::Signatures::Simple: Nice, very simple, and almost as fast as pure perl, is this module ready for use in modules you depend on?

package Test;

use Method::Signatures::Simple;
use Benchmark qw/:all/ ;

sub new {
    bless {}, shift;

# with Method::Signatures::Simple
method signatures ($first, $second) {
    die unless $self;
    die unless $first;
    die unless $second;

# default perl
sub default {
    my $self = shift;
    my ($first, $second) = @_;

    die unless $self;
    die unless $first;
    die unless $second;

# some benchmarking
my $self = __PACKAGE__->new;

my $call_signatures = sub {

my $call_default = sub {

cmpthese(10000000, {
    'Signatures' => $call_signatures,
    'Default'    => $call_default


                Rate    Default Signatures
Default    1316309/s         --        -8%
Signatures 1434103/s         9%         --


In some occassions that can happen, but itshould not be reproducable. As the benchmark is running in sequence, some other load on the system during the "default" benchmark can simply affect the benchmark.

Here is the average result of 10 test runs...
Rate Signatures Default
Signatures 1941748/s -- -3%
Default 2000000/s 3% --

At this point I'd be tempted to look at subtle differences in how Method::Signatures::Simple builds the optree; e.g.

my $first = shift;
my $second = shift;


my ( $first, $second ) = @_;

Tried with a 10s fixed benchmark time (-10 as first parameter to cmpthese), to give it a chance to generate stable results:

Rate Default Signatures
Default 1415342/s -- -2%
Signatures 1449838/s 2% --

(perl5.10.1 x64 w/threads, stock Ubuntu10.10 version)

Seems logical that the extra overhead is in the shift(), since it has to modify an array, so by changing to use this:

sub default {
    my ($self, $first, $second) = @_;
instead, I get:
                Rate Signatures    Default
Signatures 1651587/s         --       -13%
Default    1904918/s        15%         --
on the same system. I'm surprised that signatures are so much slower here, perhaps there's overhead in a coderef or similar?

About Forward Ever

user-pic I blog about Perl.