Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Proxying (almost) all methods in a class for mass memoization

by tobyink (Canon)
on Aug 16, 2018 at 03:25 UTC ( [id://1220399]=note: print w/replies, xml ) Need Help??


in reply to Proxying (almost) all methods in a class for mass memoization

This:

return $outputs->{ $call_key } if defined $outputs->{ $call_key +}; $outputs->{ $call_key } = $self->$orig( @args ); return $outputs->{ $call_key };

Would be more concise like this:

return( $outputs->{$call_key} //= $self->$orig(@args) );

As it's the last line in the sub, you don't even need the return keyword. (And sub calls are slightly optimized if you leave it out.)

One thing to beware: get_method_list doesn't return a list of all methods — it doesn't include inherited methods. You could use get_all_methods (which retuns Moose::Meta::Method objects instead of a list of method names as strings, so you will need to do $_->name on each).

Replies are listed 'Best First'.
Re^2: Proxying (almost) all methods in a class for mass memoization
by hippo (Bishop) on Aug 16, 2018 at 08:06 UTC
    And sub calls are slightly optimized if you leave [return] out.

    That's a surprising twist. I would have imagined that such a thing would have been easily optimised away at compilation. I'm going to have to start benchmarking frequently-called, tiny subs to see what sort of difference this makes. Thanks (I think)!

      > I would have imagined that such a thing would have been easily optimised away at compilation

      And, unsurprisingly, it's been the case since 5.20!

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        I wonder what it takes to trigger that optimization.

        davido:~/scripts$ perlbrew exec --with perl-5.10.1 perl mytest.pl Perl version: 5.010001 Rate retn bare retn 6849315/s -- -8% bare 7462687/s 9% -- (25000000, 25000000) davido:~/scripts$ perlbrew exec --with perl-5.28.0 perl mytest.pl Perl version: 5.028000 Rate bare retn bare 10729614/s -- -3% retn 11013216/s 3% -- (25000000, 25000000)

        I mean I do see a difference, but other optimizations between Perl 5.10 and Perl 5.28 seem to be far more significant (probably the integer optimization).

        Here is the sample code:

        use strict; use warnings; use Benchmark qw(cmpthese); my $x; my $y; sub bare { ++$x; $x } sub retn { ++$y; return $y } print "Perl version: $]\n\n"; cmpthese(25_000_000, { bare => sub {my $t = bare()}, retn => sub {my $t = retn()}, }); print "\n($x, $y)\n";

        The reason for the double sub call is to assure that the return value is being obtained, and not discarded due to context. So we do have the double-call overhead, but it's the same overhead for both sides of the test.

        Anyway, where there had been a 9% difference in favor of bare returns in 5.10, there's now a statistically insignificant 3% difference that seems to go either direction.


        Dave

Re^2: Proxying (almost) all methods in a class for mass memoization
by Anonymous Monk on Aug 16, 2018 at 10:59 UTC
    Appreciate the feedback and the useful insights, tobyink! The bit about return()ing from subs... that's revelatory for sure!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found