[Cython] local variable handling in generators

Robert Bradshaw robertwb at math.washington.edu
Mon May 23 20:09:50 CEST 2011


On Mon, May 23, 2011 at 2:57 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Stefan Behnel, 23.05.2011 11:29:
>>
>> Stefan Behnel, 23.05.2011 11:15:
>>>
>>> Vitja Makarov, 23.05.2011 10:50:
>>>>
>>>> 2011/5/23 Stefan Behnel:
>>>>>
>>>>> 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?
>>>
>>> Because there is no user visible difference. None will always be
>>> available,
>>> even if the Cython code no longer holds a reference to it. So changing "x
>>> =
>>> None" into "Py_DECREF(x); x=NULL" is just fine, as long as we can make
>>> sure
>>> 'x' is never accessed after this point.
>>
>> The difference is clearer when I spell out the code for the first example,
>> too:
>>
>> # x = None
>> Py_INCREF(None)
>> Py_DECREF(x)
>> x = None
>>
>> would be optimised into
>>
>> # x = None
>> Py_DECREF(x)
>> x = NULL
>>
>> That's likely not a big difference, assuming that setting 'x' to None was
>> worth it for the user, i.e. it will clean up the referenced object at that
>> point. It may still be worth it inside of generators and on function
>> return, which may now have one variable less to clean up or store away. A
>> None value would still have to be DECREF-ed at least.
>
> Then again, "del x" would be a more obvious way to spell this ...

And then setting it to NULL would actually be correct.

In any case, I'm -1 to deleting references once their no longer used,
we must wait 'till the end of the function, and I'm not sure the
savings would be that great either.

In terms of packing/unpacking the variables onto the local C stack,
until we have live variable analysis (and even then) it may still be
more expensive in some cases than just leaving it there, right?

- Robert


More information about the cython-devel mailing list