[Python-Dev] Accessing globals without dict lookup (original) (raw)

Tim Peters tim.one@comcast.net
Mon, 11 Feb 2002 03:13:46 -0500


[Skip Montanaro, on def mylen(s): return len(s) ]

Yeah, it's

TRACKGLOBAL 'len' LOADFAST LOADFAST CALLFUNCTION 1 UNTRACKGLOBAL 'len' RETURNVALUE or something similar. (Stuff in <...> represent array indexes.) My scheme makes update of my local copy of builtins.len

Who is the "me" in "my"? That is, is "my local copy" attached to the frame, or to the function object, or to the module globals, or ...? Since it's accessed via LOAD_FAST, I'm assuming it's attached to the frame object.

the responsibility of the guy who changes the global copy.

Also in my variant of Guido's proposal (and the value of len is cached in the module dict there, which tracks all changes to the builtins as they occur).

Most of the time this never changes,

Right.

so as the number of accesses to len increase, the average time per lookup approaches that of a simple LOADFAST.

You mean number of accesses to len per function call, I think. If I do

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

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