[Cython] local variable handling in generators

Vitja Makarov vitja.makarov at gmail.com
Mon May 23 10:50:35 CEST 2011


2011/5/23 Stefan Behnel <stefan_ml at behnel.de>:
> Vitja Makarov, 23.05.2011 10:13:
>>
>> With live variable analysis that should be easy to save/restore only
>> active variables at the yield point.
>
> "Active" in the sense of "modified", I suppose? That's what I was expecting.
>

Active means that variable value will be used. In my example after
'print a' a isn't used anymore.

>
>> Btw now only reaching definitions analysis is implemented. I'm going
>> to optimize by replacing sets with bitsets. And then try to implement
>> live varaiables.
>>
>> I'm going to delete variable reference using active variable info, but
>> that could introduce small incompatiblity with CPython:
>> a = X
>> print a #<- a will be decrefed here
>> print 'the end'
>
> That incompatibility is not small at all. It breaks this code:
>
>    x = b'abc'
>    cdef char* c = x
>
> Even if 'x' is no longer used after this point, it *must not* get freed
> before 'c' is going away as well. That's basically impossible to decide, as
> users may pass 'c' into a function that stores it away for alter use.
>

Yeah. That's hard to detect. But x could be marked as "don't decref
when not-active"

> I'm fine with deallocating variables that are no longer used after the user
> explicitly assigned None to them (i.e. replace the None assignment by a
> simple "DECREF + set to NULL" in that case). I don't think we should be
> doing more than that.
>

Hmm. Why should that be NULL if user sets it to None?


For instance:

for i in args:
    print i

this code will be translated into:

PyObject *i = NULL;

for (;;)
{
   tmp = next();
   if (!tmp) break;

  Pyx_XDECREF(i);
  i = tmp;
  print(i);
}

using active variables information this could be translated into:

PyObject *i = NULL;

for (;;)
{
   tmp = next();
   if (!tmp) break;

  i = tmp;
  print(i);
  Pyx_DECREF(i);
}


-- 
vitja.


More information about the cython-devel mailing list