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

Nick Coghlan ncoghlan at gmail.com
Wed May 7 16:56:29 CEST 2008


Jeremy Hylton wrote:
> On Fri, May 2, 2008 at 1:38 PM, "Martin v. Löwis" <martin at v.loewis.de> wrote:
>> Thomas Lee wrote:
>>  > Martin v. Löwis wrote:
>>  >>> This leaves us with a few options:
>>  >>>
>>  >>
>>  >> 5. Reuse/Abuse Num(object) for arbitrary constants.
>>  >>    AFAICT, this should work out of the box.
>>  >>
>>  >>
>>  > Eek. It *does* seem like Num would work out of the box, but would this
>>  > be a good idea?
>>
>>  No. I suggested it just for completeness.
>>
>>
>>  > What about *replacing* Num with Const? Might make optimizations
>>  > specifically for numeric values slightly hairier, and semantically I
>>  > think they might be different enough to warrant separate AST nodes
>>  > despite the similarity in implementation at the compiler level.
>>
>>  I think they should be separate. Const would be a mere addition;
>>  for compatibility with other uses of the AST, that's actually better
>>  than simultaneous removal of Num.
> 
> Adding Const sounds good to me.

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...

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list