Skip Menu |

This queue is for tickets about the Moo CPAN distribution.

Report information
The Basics
Id: 67109
Status: resolved
Priority: 0/
Queue: Moo

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Unimportant
Broken in: 0.009007
Fixed in: 1.002000



Subject: isa quote_sub slower than a regular sub
I was benchmarking various OO modules and discovered that using quote_sub for isa checks is *slower* than a regular sub. About 30% slower. This is contrary to the advice of the docs. The attached benchmark comes out like so: Benchmark: timing 6000000 iterations of with_qsub, with_sub... with_qsub: 24 wallclock secs (24.14 usr + 0.04 sys = 24.18 CPU) @ 248138.96/s (n=6000000) with_sub: 19 wallclock secs (17.87 usr + 0.01 sys = 17.88 CPU) @ 335570.47/s (n=6000000)
Subject: Foo.pm
package Foo; use Moo; use Sub::Quote; has with_sub => (is => 'rw', isa => sub { $_[0] =~ /^[+-]?\d+$/ }); has with_qsub => (is => 'rw', isa => quote_sub q{ $_[0] =~ /^[+-]?\d+$/ }); 1;
Subject: test.plx
Download test.plx
application/octet-stream 258b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #67109] isa quote_sub slower than a regular sub
Date: Thu, 31 Mar 2011 17:18:06 +0000
To: bug-Moo [...] rt.cpan.org
From: Matt S Trout <dbix-class [...] trout.me.uk>
On 31/03/11 09:48, Michael G Schwern via RT wrote: Show quoted text
> Thu Mar 31 05:48:49 2011: Request 67109 was acted upon. > Transaction: Ticket created by MSCHWERN > Queue: Moo > Subject: isa quote_sub slower than a regular sub > Broken in: 0.009007 > Severity: Unimportant > Owner: Nobody > Requestors: mschwern@cpan.org > Status: new > Ticket<URL: https://rt.cpan.org/Ticket/Display.html?id=67109> > > > I was benchmarking various OO modules and discovered that using > quote_sub for isa checks is *slower* than a regular sub. About 30% > slower. This is contrary to the advice of the docs. > > The attached benchmark comes out like so: > Benchmark: timing 6000000 iterations of with_qsub, with_sub... > with_qsub: 24 wallclock secs (24.14 usr + 0.04 sys = 24.18 CPU) @ > 248138.96/s (n=6000000) > with_sub: 19 wallclock secs (17.87 usr + 0.01 sys = 17.88 CPU) @ > 335570.47/s (n=6000000)
Try benching quote_sub { my ($thing) = @_; $thing =~ ... as well?
Still slower, but not quite as slow. The really curious thing is it effects get! https://gist.github.com/897540
Subject: Re: [rt.cpan.org #67109] isa quote_sub slower than a regular sub
Date: Sat, 2 Apr 2011 21:08:27 +1300
To: Matt S Trout <dbix-class [...] trout.me.uk>, bug-Moo [...] rt.cpan.org
From: Mark Lawrence <nomad [...] null.net>
On Thu Mar 31, 2011 at 05:18:06PM +0000, Matt S Trout wrote: Show quoted text
> Try benching > > quote_sub { my ($thing) = @_; $thing =~ ... > > as well?
Is that legal syntax? Documentation says quote_sub expects a string, but more importantly behaviour is a little weird: syntax error at Foo.pm line 8, near "; $thing " Global symbol "$thing" requires explicit package name at Foo.pm line 8. Or perhaps you actually meant to keep the "q{" but only change the string... in which case I get the same result as using "$_[0]": Benchmark: running with_qsub, with_qsub2, with_sub for at least 3 CPU seconds... with_qsub: 2 wallclock secs ( 3.15 usr + 0.00 sys = 3.15 CPU) @ 166480.63/s (n=524414) with_qsub2: 4 wallclock secs ( 3.10 usr + -0.01 sys = 3.09 CPU) @ 167566.02/s (n=517779) with_sub: 4 wallclock secs ( 3.15 usr + 0.01 sys = 3.16 CPU) @ 183408.54/s (n=579571) -- Mark Lawrence
The latest Moo release removes all of the non-essential output from quote_sub'd isas, and the benchmark now shows quote_sub as faster than regular subs. Marking resolved.
On Sat May 04 19:39:57 2013, haarg wrote:
Show quoted text
> The latest Moo release removes all of the non-essential output from
> quote_sub'd isas, and the benchmark now shows quote_sub as faster than
> regular subs. Marking resolved.

FWIW quote_sub now just barely edges out a regular sub using Moo 1.2.0 and the original Benchmark on this ticket, but there's a lot of benchmarking flutter.

$ perl -I ~/tmp/ ~/tmp/test.plx
Benchmark: running with_qsub, with_sub for at least 3 CPU seconds...
 with_qsub:  3 wallclock secs ( 3.08 usr +  0.02 sys =  3.10 CPU) @ 148980.32/s (n=461839)
  with_sub:  3 wallclock secs ( 3.07 usr +  0.01 sys =  3.08 CPU) @ 145403.90/s (n=447844)

$ perl -I ~/tmp/ ~/tmp/test.plx
Benchmark: running with_qsub, with_sub for at least 3 CPU seconds...
 with_qsub:  4 wallclock secs ( 3.36 usr +  0.01 sys =  3.37 CPU) @ 141464.99/s (n=476737)
  with_sub:  3 wallclock secs ( 3.12 usr +  0.01 sys =  3.13 CPU) @ 147552.40/s (n=461839)

$ perl -I ~/tmp/ ~/tmp/test.plx
Benchmark: running with_qsub, with_sub for at least 3 CPU seconds...
 with_qsub:  3 wallclock secs ( 3.11 usr +  0.01 sys =  3.12 CPU) @ 148872.12/s (n=464481)
  with_sub:  3 wallclock secs ( 2.99 usr +  0.02 sys =  3.01 CPU) @ 140479.07/s (n=422842)

$ perl -I ~/tmp/ ~/tmp/test.plx
Benchmark: running with_qsub, with_sub for at least 3 CPU seconds...
 with_qsub:  3 wallclock secs ( 3.10 usr + -0.00 sys =  3.10 CPU) @ 149832.58/s (n=464481)
  with_sub:  4 wallclock secs ( 3.37 usr +  0.02 sys =  3.39 CPU) @ 132107.37/s (n=447844)

$ perl -v
This is perl 5, version 16, subversion 2 (v5.16.2) built for darwin-thread-multi-2level