[Python-Dev] About dictionary lookup caching

"Martin v. Löwis" martin at v.loewis.de
Tue Dec 19 22:15:42 CET 2006


Andrea Griffini schrieb:
> My opinion is that it would be by far better to do this ordering
> of co_names at compile time but I've no idea where to look
> for trying to make such a change.
> 
> Can someone please point me in the right direction ?

It's all in Python/compiler.c. The names list is created in
dict_keys_inorder, where the dictionary keys are supposed to
be tuples (name, str), and the values are the variable number.

The dictionaries are filled through compiler_add_o, which looks
up whether there is already a name in the table (and if so returns
its number), or else adds a new entry, using len(dict) as the new
number.

As you can see, this inherently adds the names in the order in
which they are encountered.

I can see a number of choices:
1. make a separate list for names loaded through LOAD_GLOBAL.
   This would be an API change (and affect reflection and whatnot),
   but it would likely be the algorithmically easiest way
2. make a pass through the AST before compilation starts, to
   already add all globals to the names dictionary (while you are
   at it, you might as well fill all symbol tables in that pass).
   You can then renumber them as you please, and only invoke byte
   code generation afterwards.
3. try fitting this into the peephole optimizer. You'd make a
   peephole pass which reindexes co_names, and then run through
   the byte code to update all opargs. Be prepared to have this
   pass fail, in case you'd need an extended arg, but don't have
   space for it.

Regards,
Martin


More information about the Python-Dev mailing list