[Python-Dev] Optimization of Python ASTs: How should we deal with constant values?

Thomas Lee tom at vector-seven.com
Wed May 7 17:43:45 CEST 2008


Nick Coghlan wrote:
>
> As Thomas mentions in a later message, making it possible to annotate 
> nodes would permit Functions to be annotated as being a generator at 
> the AST stage (currently it is left to the bytecode compiler's 
> symtable generation pass to make that determination).
>
> Although I guess an alternative solution to that would be to have 
> separate AST nodes for Functions and Generators as well...
>
I've actually backtracked a little and gone back down the Const path 
again. I know this is the third time I've changed my mind, but it's 
primarily because annotations tend to get a little clunky (or my 
implementation was, at least). Using Const nodes feels a lot more 
natural inside the optimizer. I think it's going to stick, at least in 
the short term.

Rather than separate FunctionDef and GeneratorDef nodes, I think a new 
bool attribute (is_generator?) on FunctionDef should do the job nicely. 
Further, I'm thinking we can move the "generator detection code" from 
the symtable into Python/ast.c so the generator/function information is 
available to the optimizer.

This is made a little tricky by the absence of the contextual 
information available that is normally available when flagging 
generators in the symtable. When generating AST nodes for a suite, we 
know nothing about the parent node in which the suite resides. Still, it 
might be doable. If this winds up being ugly, we might need to fall back 
to the original plan of a separate pass over function bodies to detect 
yield expressions.

I'll look into all this tomorrow night, along with any other crazy 
suggestions. For now I need to sleep a few hours. :)

Thanks for the feedback, it's much appreciated.

Cheers,
T



More information about the Python-Dev mailing list