[Cython] Assignments in cdefs
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Sun Jul 20 20:22:47 CEST 2008
Stefan Behnel wrote:
> 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?
I didn't do the general optimization, I simply set first = True on the
assignment I generated when transforming the cdef statement "manually",
so this is only half-way to the general thing (why? Because I'd need to
_track assignments_, that's another usecase for my assignment
refactoring proposal :-) With that in place I would have started done a
simple one, would have taken 15 minutes [1]).
As for the example, it is a "best-effort" thing. If no assignments are
flagged as first, then the optimization just doesn't happen, which is
the likely outcome of your example. However, if you come up with an
algorithm to check this, then in this case you can flag them both as first.
I should perhaps be more precise in my claim: I don't actually have an
algorithm for doing this -- what I'm saying is that *if* one uses a
fancy flow control algorithm it can be put into a self-contained visitor
that goes over the node graph and flags assignments as it sees fit. It
just seems easier to do this kind of stuff by having the "call thread"
belong to the flow control algorithm rather than rely on setting side
effects everywhere.
At any rate, [1] would provide enough information to reconstruct the
information I now discarded from the existing flow control, which should
do for having the patch accepted :-)
[1] The typical simple algorithm (which I'll do if assignment
refactorings happen) would be like this:
- Any assignments not on top indent level would never be first assignments
- Any assignments on the top indent level which is the first
("vertically" and regardless of indentation level) is flagged as first.
--
Dag Sverre
More information about the Cython-dev
mailing list