[Cython] read-only memory views

mark florisson markflorisson88 at gmail.com
Wed Sep 19 00:42:39 CEST 2012


On 18 September 2012 20:55, Christoph Groth <cwg at falma.de> wrote:
> Hello,
>
> I have written a python extension module, tinyarray, (to be made public
> soon) which implements an important subset of numpy optimized for
> _small_ arrays.  Tinyarrays are immutable, which makes them usable as
> dictionary keys.
>
> Some important operations, i.e. creation of a small array from a tuple
> or the scalar product of two small arrays are 10 to 45 times faster than
> with numpy.  This can really boost the performance of programs which use
> small numpy arrays.  (I know that numpy is meant to be used for large
> arrays, but there are legitimate uses of small arrays as well.)
>
> It would be great if Cython could support tinyarrays.  Right now, it
> doesn't.  See
> http://article.gmane.org/gmane.comp.python.cython.user/7329
>
> (As a temporary workaround, I have to made the buffers writable.  But
> this breaks strict immutability.)
>
> Would fixing this be a lot of work?

That depends on how we implement it. A 'const' keyword has been
discussed in general. This would be a little more work, since it needs
grammar modifications. Also, there would be a difference between
declaring the data and the variable const, i.e. 'const double[:]' vs
'double[:] const', which I personally find bothersome.

Using flags are obvious, like the ones in cython.view, i.e.
double[:view.readonly], but the problem with that is which axis to
choose (or any axis?), and we don't have a way of combining flags. So
we would not only need view.contiguous, but also
view.contiguous_readonly etc.

Without a greater plan for 'const' I think it might be awkward to use
for this purpose. I think the flags are apt, and should be used in the
first dimension. If we wait for other people to comment we could give
you pointers if you want to give implementing it a swing, it wouldn't
be too hard.

> Cheers,
> Christoph
>
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel


More information about the cython-devel mailing list