[Python-Dev] Accessing globals without dict lookup

Skip Montanaro skip@pobox.com
Mon, 11 Feb 2002 08:16:32 -0600

    Tim> [Skip Montanaro, on
    Tim>     def mylen(s):
    Tim>         return len(s)
    Tim> ]
    >> Yeah, it's
    >> TRACK_GLOBAL        'len'
    >> LOAD_FAST           <len>
    >> LOAD_FAST           <s>
    >> CALL_FUNCTION       1
    >> UNTRACK_GLOBAL      'len'
    >> or something similar.  (Stuff in <...> represent array indexes.)
    >> My scheme makes update of my local copy of __builtins__.len

    Tim> Who is the "me" in "my"?

Sorry, should have been "the" instead of "my".  TRACK_GLOBAL is responsible
for making the original copy.  I should have added another argument to it:

    TRACK_GLOBAL        'len', <len>
    LOAD_FAST           <len>
    LOAD_FAST           <s>
    CALL_FUNCTION       1
    UNTRACK_GLOBAL      'len', <len>

    Tim> You mean number of accesses to len per function call, I think.  


    Tim> If I do

    Tim>     for i in xrange(1000000):
    Tim>         print mylen("abc")

    Tim> I'm going to do a TRACK_GLOBAL and UNTRACK_GLOBAL thingie too for
    Tim> each LOAD_FAST of len, and then the average time per len lookup
    Tim> really has to count the average time for those guys too.

Actually, no.  I originally meant to say "Ignoring the fact that my
optimizer would leave this example untouched...", but deleted it while
editing the message as more detail than you were asking for.  Your example:

    def mylen(s):
        return len(s)

doesn't access len in a loop, so it would be ignored.  On the other hand:

    for i in xrange(1000000):
        print mylen("abc")

would track mylen (but not xrange).