[Cython] memory view creation during cascaded assignments

Mark Florisson markflorisson88 at gmail.com
Sat Jul 5 14:49:14 CEST 2014


Hi Stefan,

On 5 July 2014 11:34, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi,
>
> I started optimising cascaded assignments a little to reduce the
> unnecessary duplication of coercions. While doing that, I found this test
> in memslice.pyx:
>
> '''
> def cascaded_buffer_assignment(obj):
>     """
>     >>> A = IntMockBuffer("A", range(6))
>     >>> cascaded_buffer_assignment(A)
>     acquired A
>     acquired A
>     released A
>     released A
>     """
>     cdef int[:] a, b
>     a = b = obj
> '''
>
> It's explicitly tested for that we create two independent memory views in
> this case. Is there an actual reason for this? As long as the types of a
> and b are identical, I don't see why we would want to request the buffer twice.

I think it's mostly for simplicity, since any reaching definition is
cleared at the end of the function. Are you thinking to generally
implement this? I suppose you could assign the coercion to a new
variable and replace all use sites (e.g. all the coercions in the
cascaded assignment) with that variable.

     obj = ...
     a = coerce obj int[:]
     b = coerce obj int[:]

->

    obj = ...
    tmp = coerce obj int[:]
    a = tmp
    b = tmp

These transformations may be easier on a three-address code-like
representation, where this sort of replacement can be more effective
in SSA form or by first applying copy propagation. Perhaps if the
coercion was explicit a CSE pass could even eliminate any duplicates.

Cheers,
Mark

> Stefan
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> https://mail.python.org/mailman/listinfo/cython-devel


More information about the cython-devel mailing list