MemoryView

MemoryView provides the features to share multidimensional homogeneous arrays of fixed-size element on memory among extension libraries.

Disclaimer

Overview

We sometimes deal with certain kinds of objects that have arrays of the same typed fixed-size elements on a contiguous memory area as its internal representation. Numo::NArray in numo-narray and Magick::Image in rmagick are typical examples of such objects. MemoryView plays the role of the hub to share the internal data of such objects without copy among such libraries.

Copy-less sharing of data is very important in some field such as data analysis, machine learning, and image processing. In these field, people need to handle large amount of on-memory data with several libraries. If we are forced to copy to exchange large data among libraries, a large amount of the data processing time must be occupied by copying data. You can avoid such wasting time by using MemoryView.

MemoryView has two categories of APIs:

  1. Producer API

    Classes can register own MemoryView entry which allows objects of that classes to expose their MemoryView

  2. Consumer API

    Consumer API allows us to obtain and manage the MemoryView of an object

MemoryView structure

A MemoryView structure, rb_memory_view_t, is used for exporting objects’ MemoryView. This structure contains the reference of the object, which is the owner of the MemoryView, the pointer to the head of exported memory, and the metadata that describes the structure of the memory. The metadata can describe multidimensional arrays with strides.

The member of MemoryView structure

The MemoryView structure consists of the following members.

MemoryView APIs

For consumers

Fill the members of view as an 1-dimensional byte array.

Fill the strides array with byte-Strides of a contiguous array of the given shape with the given element size.

Fill the item_desc member of view.

Return true if the data in the MemoryView view is row-major or column-major contiguous.

Return false otherwise.

Return true if the data in the MemoryView view is row-major contiguous.

Return false otherwise.

Return true if the data in the MemoryView view is column-major contiguous.

Return false otherwise.