C arrays deserve better language support

Note: This CEP should might possibly be merged into the Cython array CEP.

Original proposal by Brian Granger.

Things that could be improved:

Optimizations:

Cons:

The general consensus is that, especially as we support constant-sized arrays with this syntax, this will be a good thing to have.

See discussion at http://codespeak.net/pipermail/cython-dev/2008-April/000411.html and http://codespeak.net/pipermail/cython-dev/2008-April/000385.html

Array objects

It is natural to want to pass arrays around, assign them to variables, etc. as if they were atomic objects rather than pointers. Currently one has to manually use malloc and free (which is often done incorrectly, especially in the case of error recovery). The obvious way to do this is to tie into the Python memory management system. An example of this is given by Lisandro Dalcin:

It may be worthwhile to add such functionality into the language itself, or via a provide extension type (with possible syntactic sugar). The returned object would be a special array type, assignable only to arrays of the same type, recounted, with fast (1-dimensional) indexing, iteration, etc. It would be much more lightweight (but potentially faster and fewer dependancies) then using, for example, NumPy. Maybe we could support a fast append too.

Syntax

What syntax would we use. In light of the recent buffer interface, what about carray[int] for the type and carray[int](size) as a constructor? Some another alternative is [int].

Discussion

What about:

   1 cdef inline void* __malloc__(int bytes): return sage_malloc(bytes)
   2 cdef inline void __free__(void* ptr): return sage_free(ptr)

at the top-level Cython module scope in order to override the malloc-er and use sage_malloc instead?

enhancements/arraytypes (last edited 2009-06-18 06:25:09 by robertwb)