[Python-Dev] Re: opcode performance measurements

Jeremy Hylton jeremy@alum.mit.edu
Thu, 31 Jan 2002 08:09:59 -0500


>>>>> "JE" == jepler  <jepler@unpythonic.dhs.org> writes:

  JE> But isn't what happens in this module something like
  JE>     LOAD_CONST <code1> MAKE_FUNCTION STORE_GLOBAL 0 (f)

  JE>     LOAD_CONST <code2> MAKE_FUNCTION STORE_GLOBAL 1 (g)

  JE> so if you convert LOAD_GLOBAL into LOAD_FAST_GLOBAL when you
  JE> MAKE_FUNCTION on code1, there is not yet a "g" in the dlict.

  JE> Are you populating the "names" part of the dlict as an earlier
  JE> "pass" of module compilation, then?

Yes.  The compiler can do a pretty good job of establishing all the
globals in a module at compile time.  When a module is loaded, the
interpreter would allocate space for all the expected globals.

  JE> "pass" of module compilation, then?  So the optimization doesn't
  JE> apply if I create the globals from within a function?

It still applies.  The function is compiled at the same time as the
module, so the module symbol table can account for globals assigned to
only in functions.

The harder cases are much more dynamic -- exec using a module's
globals, assignment to create new attributes on an imported module,
etc.  Example:

    import foo
    assert not hasattr(foo, 'bar') # just to illustrate the example
    foo.bar = 12

There's no way for the compiler to know that foo will have a bar
attribute when it compiles foo.

Jeremy