Stefan Behnel wrote:
> Hi all,
>
> the wiki has an enhancement page on better C array support.
>
> http://wiki.cython.org/enhancements/arraytypes
>
> Note that this is not about SIMD operations and whatnot, just about plain
>
>       int[] x
>
> The page mentions things like dynamic memory allocation happening
> automatically behind the scenes, so that
>
>       cdef int a[runtime_size]
>
> would locally allocate memory, as would subsequent slicing, I guess. I
> wonder if that's not too much magic, and if a much simpler thing wouldn't
> get us far enough already.
>
> Since we already agreed to get a full-featured SIMD type, what do you
> think
> about dropping the dynamic memory handling part for plain C arrays, and
> instead just supporting slicing operations on any C pointer type, and
> letting them return a Python list? (or a byte string in the special case
> of
> a char*)
>
> This would be a rather straight forward thing to support:
>
>       cdef char* s = ...
>       py_s = s[:30]
>
> It would translate directly to a call to PyString_FromStringAndSize(),
> which is a rather often requested feature IMO. All other base types would
> return a runtime-constructed list instead:
>
>       cdef int* x = ...
>       py_int_list = x[4:20:-1]
>
> This would generate a loop that creates Python int values from the slice
> and put them into a list, so it's basically a short form for this:
>
>       py_int_list = [ int(x[i]) for i from 20 >= i > 4 by -1 ]

Hmm. Interesting idea.

My initial feeling as well is that I'd expect x[2:40] to:
 a) Return an object which directly indexes C integers instead of doing
object conversions
 b) Perhaps: Refer to the original memory?

The two are orthogonal, and b) is certainly due to me mixing NumPy-ness
and Pythonicness -- copying on slicing is more Pythonic, after all.

At any rate, it should be possible to do

for my_typed_var in x[:10]:
    ...

without object conversions, and the cleanest way of going about that seems
to be a). For instance one could make a copy of the data and put it in an
array.array.

As far as the Cython memoryview type goes (which over the summer became
the name for the "Cython numerical array type"... since it is so close to
Python's memoryview: Slices returns new views, not copies): I think it
would feel natural to have

myptr[0:54,0:10]

simply turn the pointer into a memoryview. However I'm not at all going to
fight over this corner of the syntax for numerical use -- for numerical
use an explicit function like cython.memoryview(myptr, (54, 10),
owned=False) works just as well I think.

Dag Sverre

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to