Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: private recursive subroutines

by ikegami (Patriarch)
on May 12, 2007 at 16:09 UTC ( [id://615089]=note: print w/replies, xml ) Need Help??


in reply to Re^2: private recursive subroutines
in thread private recursive subroutines

From my limited understanding, the method cache allows Perl to remember to which function a method call resolves so it doesn't have to traverse @ISA every time.

Changing the symbol table clears the cache. the value of *foo changes twice in the snippet in your post, the explicit assignment and the implicit assignment at the end of local's scope.

$o->method(); # Normal (first call) $o->method(); # Faster due to cache *foo = sub {}; $o->method(); # Normal (cache cleared) $o->method(); # Faster due to cache

Replies are listed 'Best First'.
Re^4: private recursive subroutines
by demerphq (Chancellor) on May 13, 2007 at 17:19 UTC

    Thanks, this is pretty much the response I would have made. The only thing I'd add is that as far as I understand things Perl 5.10 wont pay the same global penalty, rather the penalty will be restricted to the package or method itself, although I'm not sure which. This a side effect of Brandon Black's MRO patch for adding C3 style method resolution support to Perl.

    ---
    $world=~s/war/peace/g

      Yes, in perl-current with my mro.c stuff (and eventually 5.10), when you change the @ISA of a class or change a method in a class, it only invalidates the method cache of that specific class, as well as any classes which inherit from that class (as opposed to the behavior in 5.9.4 and earlier, where changing a method or @ISA anywhere invalidated all method caches globally).

      Also, while in 5.9.4 and earlier the recursive lookup via @ISA is performed every time an uncached method call was made, with the new mro stuff the recursive @ISA parent hierarchy of a class is linearized into a simple flat list of classes to search with no duplicates, and cached. That "linearized isa" cache is only blown on @ISA changes in the given class or its parents, not method changes. So even when you (locally) invalidate the method caches by redefining a subroutine, these @ISA linearizations stay in place, making that first lookup to refill the cache faster than it was before.

        Im just curious as to whether there will be any way from perl code to see the linearized array. I could see that being useful indeed. Maybe @^ISA could provide read access to this data.

        ---
        $world=~s/war/peace/g

Re^4: private recursive subroutines
by clinton (Priest) on May 12, 2007 at 16:14 UTC
    ok - but that hit is taken when the sub is inserted into the symbol table. Assuming you don't every redefine the sub, you can use it without messing with the method cache, no?
      Calling the function has no effect on the cache, only changing *foo does.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found