[Python-Dev] Proposed PEP: Optimizing Global Variable and Attribute Access

Skip Montanaro skip at pobox.com
Wed Aug 15 01:25:51 CEST 2001


    Roman> Well, I just think that probably interpreter could optimize by
    Roman> just IMPLICITLY making caching of global references, like in this
    Roman> (current) optimization trick:

    Roman> import os    # for constants

    Roman> def f(x, y, z,   # normal atts
    Roman>       cached_os_O_NONBLOCK=os.O_NONBLOCK,   # optimization trick
    Roman>       cached ....
    Roman>      )

    Roman>     # here we use os.O_NONBLOCK, but interpreter uses
    Roman>     # local _names_ and thus fast lookup.

No, that's not possible in the general case.  You are assuming that
os.O_NONBLOCK is truly a constant.  There's nothing to prevent me from
having this code:

    i = 1

    def foo():
      for j in range(1000):
        bar()
        if i % 100 == 0:
           print i

    def bar():
      global i
      i += 1

If you simply cache the value of the global variable i, you'll never print
anything. 

For those people on python-list who have no idea what this thread is about,
please be patient.  Once Barry Warsaw assigns a PEP number to it, I will
announce it to python-list.

Thanks,

Skip




More information about the Python-list mailing list