[Cython] Assignment nodes, buffers and transforms

Stefan Behnel stefan_ml at behnel.de
Wed Jul 23 19:07:08 CEST 2008


Hi,

Dag Sverre Seljebotn wrote:
> Robert Bradshaw wrote:
>> cdef object x
>> cdef int a, b
>> a = b = x # currently x gets converted twice, but one could imagine 
>> wanting to optimize so that it happens only once...
> 
> Two options: you either do it before/as part of the splitting up, as in:
> cdef int i, j
> x = i = j = somecall()
> 
> turns into
> cdef object tmp1
> cdef int tmp2
> tmp1 = somecall()
> x = tmp1
> tmp2 = tmp1
> i = tmp2
> j = tmp2
> 
> Option 2: you realize that your time is better spent at optimizing the
> general case that could arise if one wrote the thing using seperate
> assignments (it is still optimizeable in theory) and stick a (much more
> advanced) algorithm after the splitting up, where it can benefit more
> assignment types.

I disagree with option 2. A statement like

    a = b = xyz

is very clearly asking for "xyz" being evaluated once, while

    a = xyz
    b = xyz

is explicit about evaluating "xyz" twice (whatever xyz actually is). The code
we currently generate for the first statement (at least in some cases) is
surprising, while in the second case it matches my expectations.

Also, I expect the logic required to determine that two statements assign the
same thing to different names to be non-trivial. Why should the above be
optimised, but not

    a = xyz
    c = zyx
    b = xyz

As usual: hard to draw the line once you get started on the "general" case...

Stefan



More information about the Cython-dev mailing list