PEP 266: Optimizing Global Variable/Attribute Access
Skip Montanaro
skip at pobox.com
Wed Aug 15 10:49:30 EDT 2001
Skip> Actually, if I do it right, PEPTest2 would work more like
Skip> def PEPTest2():
Skip> m = math.sin
Skip> c = 100000
Skip> while c:
Skip> l = []
Skip> f = l.append
Skip> for i in range(10):
Skip> f(m(i))
Skip> c -= 1
Skip> That is, math.sin would get cached at the earliest point where it
Skip> is in scope, which would be before the while loop.
After reading Alex's note on this subject I realized I may have
misinterpreted what you asked for. While the PEP266 implementation will
work about like the manual caching case, note that it will not change
Python's semantics. If some other routine modifies the binding for math.sin
during execution of the while loop, the PEPTest2 function will see the
change, just as it would today if you included math.sin lookups in the loop
and unlike the code above which statically binds m to the object referred to
by math.sin at the start of the function. That's the whole idea of the
proposed TRACK_OBJECT/UNTRACK_OBJECT opcodes.
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list