Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Tommy (Chaplain)
on Aug 16, 2018 at 00:08 UTC ( [id://1220395]=CUFP: print w/replies, xml ) Need Help??

If this could be done in a 'better' way, I'd enjoy hearing it. Criticisms welcome.

Recently needed to basically cache the output of almost every class method in one of my modules that is part of a web app. Every method runs a different database query and encodes the results to JSON. The results change daily, so upstream of the module implementation there is logic that enforces a 12 hour TTL for anything any method returns. In the interim time though, there's no reason for the app to run these database queries at all when it already did the work. Reading about possible approaches to the problem on stack overflow yesterday I saw that use of autoload was discouraged, so this is what I came up with and as far as I can tell, after running two days in DEV, it appears to have no issues. I'm actually quite pleased, because this approach allowed me to be 'clever' without an implementation that is unmaintainable and unintelligible by others... Gist here

use strict; use warnings; package My::Class::Proxy; # Drop-in replacement for 'Some Class' # Proxies all public method calls to Some::Class in order to provide s +mart # caching and memoization, e.g.- avoiding expensive DB queries when no +t required use 5.020; use Moose; extends 'Some::Class'; use Moose::Util qw(); my $meta = Moose::Util::find_meta( 'Some::Class' ); my @nocache = qw( new meta DESTROY AUTOLOAD ); state $outputs = {}; for my $method ( $meta->get_method_list ) { # don't memo-ize blaclisted or private methods next if ( grep { $_ eq $method } @nocache or $method =~ /^_/ ); around $method => sub { my ( $orig, $self, $refresh, @args ) = @_; $outputs = {} if !!$refresh; @args = map { $_ // '' } @args; my $call_key = join '', $orig, @args; return $outputs->{ $call_key } if defined $outputs->{ $call_key +}; $outputs->{ $call_key } = $self->$orig( @args ); return $outputs->{ $call_key }; }; } # Moose-specific optimization __PACKAGE__->meta->make_immutable(); 1;

Tommy
A mistake can be valuable or costly, depending on how faithfully you pursue correction

Replies are listed 'Best First'.
Re: Proxying (almost) all methods in a class for mass memoization
by tobyink (Canon) on Aug 16, 2018 at 03:25 UTC

    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).

      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,
      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: CUFP [id://1220395]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found