[Cython] Temp allocation flow

Dag Sverre Seljebotn dagss at student.matnat.uio.no
Mon Dec 1 02:09:03 CET 2008


Greg Ewing wrote:
> Dag Sverre Seljebotn wrote:
>> It 
>> now takes two parameters, "decref" and "free_temp", which both defaults 
>> to True, and which can be toggled.
> 
> Rather than a free_temp parameter, I'd be more inclined
> to have an owns_temp flag on the node, set according to
> whether the node allocated the temp itself or it was
> supplied by the parent (currently done via an optional
> parameter to allocate_temps(), which would become a
> parameter to generate_evaluation_code()).

I didn't supply enough context. It was meant for situations like this:

self.arg.generate_evaluate_code(code)
...
code.putln("if (...) {")
...
self.arg.generate_disposal_code(code)
...
code.putln("} else {")
...
self.arg.generate_disposal_code(code)
...
code.putln("}")

Here, one can't free the temp in the first generate_disposal_code 
(because then the else-block could reuse and overwrite the temp etc.). 
As mentioned this is now solved by considering temp freeing as a 
seperate but parallell step to disposal. To make the example up-to-date, 
simply add at the end, but NOT inside the C if-blocks,

self.arg.free_temps(code)

I even found one example of the opposite construction, where 
evaluation_code was called multiple times but disposal once! (In 
TupleNode.generate_assignment_code). Luckily I was able to shuffle it a 
bit to avoid the problem.

There might be more of this stuff in Cython than in Pyrex?

This all points to a fundamental problem with the current scheme: Temp 
allocation doesn't branch when the code branches -- so with code like 
above the temp allocation is non-intuitive and suboptimal.

That's one reason I long-term believe in more transforms even for the 
stuff that nodes do today -- if code branches like the above were always 
done using nodes rather than code.putln, temp allocation would both be 
much simpler conceptually and more optimal theoretically.

-- 
Dag Sverre


More information about the Cython-dev mailing list