[Cython] Assignments in cdefs

Stefan Behnel stefan_ml at behnel.de
Sun Jul 20 19:51:17 CEST 2008


Hi Dag,

Dag Sverre Seljebotn wrote:
> 1) Bug fix: We generated erronous C code for cdef assignments in class 
> bodies, I now raise a compiler error on this.

Good catch. Could you push that to cython-devel? (although an actual
implementation to make this work would obviously be much better :)

In general, I would appreciate it if trivial/obvious/necessary things went
there directly.


> 2) Basically I turn "cdef int x = 4" into "cdef int x; x = 4".
>
> 3) Also I added the "first" attribute to SingleAssignmentNode to 
> generalize the concept of first assignment (which takes care of optimal 
> refcounting).

As long as these two go together, I absolutely see them as a gain.


> Optimizing first assignments in the more general case is 
> now as simple as running through the tree and setting that attribute 
> (after analysis, prior to generation), though I didn't do this.

What about code like this:

    def test(a):
       cdef int c
       if a:
           c = 1
       if not a:
           c = 2

How would you know what is a "first assignment" here? And what would you gain
from your ref-counting optimisation in this case?


> This 
> kind of clashes with Robert's flow control endaveours, but I feel that 
> the beginnings of flow control is overkill for this specific case, a 
> single pass with a transform should do it...

The current flow-control implementation is somewhat limited and even has a
couple of issues. One thing I ran into recently was that it can crash with a
stack overflow for functions with tons of conditionals.

If this can be done any simpler and/or more reliable, I'm open to suggestions.


> Results:
> - C code looks exactly like before (I diffed it), except for comments.

Nice.


> - The buffer just started working without any extra efforts (and it made 
> it very easy to optimize some refcounting for buffers, though that 
> needed five lines). Of course, this is why I bothered.

Happy to hear that.


> - No CVarDefNode in the tree after analyse_declarations

Fine with me.

Stefan



More information about the Cython-dev mailing list