Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An array type and memory view types for Cython

After much discussion, it became clear (to me) that there is a need for different types of array containers and views in Cython. I therefore propose to add a memory managed array type and typed memory views.

What do we need?

  • Memory management through ref-counting.
  • Automatic memory allocation.
  • Different behaviour of an array type, e.g. as a simple array that behaves like a tuple, and as a SIMD type that supports various kinds of parallel arithmetic.

What's there already?

  • Python has a __tuple__ type as a fixed size array of Python objects. No C types are supported.
  • Python has an __array__ type as fixed size array of C items. No structs or extension types are supported. Cython cannot generally see what the type is, and the behaviour of the type is fixed.
  • Cython has support for PEP 3118 __buffer__ views. These do not support memory allocation, neither are they ref-counted. It's therefore tricky to pass them around safely.

The proposal

The idea is to augment Cython with three new types, namely

  • a dynamically memory managed array
  • a typed memory view
  • a SIMD memory view

Why not different containers?

While there is the need for a memory managed sequence type in general, different behaviour can be implemented on top of that by re-implementing certain aspects as a view on any container type. This includes types that support only the buffer protocol, and it also includes other views. So it is a lot better to use a layered approach here than to make each of the above types separate container types that cannot interact without copying data.

The new types

The dynamic array type

  • allocates memory on creation
  • reallocates on (explicit) resizing, e.g. a .resize() method
  • supports PEP 3118 (and disables shrinking with live buffers)
  • returns a typed value on indexing
  • returns a typed array copy on slicing
  • behaves like a tuple otherwise

The typed memory view

  • created on top of a buffer or array
  • never allocates memory (for data, that is)
  • creates a new view object on slicing
  • behaves like the dynamic array otherwise

The SIMD view type

  • created on top of a buffer, array or memory view
  • supports parallel per-item arithmetic
  • behaves like the typed memory view otherwise

Syntax

There has been much discussion about a suitable syntax. The major proposal here was to add only a new syntax for the SIMD type (example: "int," for a 2D type) and to leave the other types out. The alternative proposed here is not to add a new kind of bracket syntax, and instead use the type parametrisation feature described in CEP 506. This will support all three types in exactly the same way, both in Cython and in pure Python syntax.

Interaction of the three types

The array type is a container, but the memory view type and the SIMD type are views, so the question is how these types behave on slicing and on operations that require copies.

Something went wrong with that request. Please try again.