cython icon indicating copy to clipboard operation
cython copied to clipboard

[ENH] Reduce the code size of the memory view implementation

Open scoder opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe.

Due to its complex feature set, the memoryview implementation is actually quite large, and can add a substantial amount of code (and extension module size) especially to small modules.

One idea is to extract shareable code into a common library, but that mostly helps in larger projects. For smaller libraries, tightening the feature set might help.

Describe the solution you'd like.

If memoryviews are returned from Python functions, they need to provide the full memoryview feature set to Python code, but it's probably not uncommon that memoryviews are only used as a module internal tool. In this case, we can determine the parts of the feature set that are used by the module, and exclude the others.

Candidates are:

  • slice assignments and broadcasting
  • the array implementation
  • … others?

We should also make sure that it is really only included (at all) if the code really uses it. A cimport of a pxd file that uses memoryviews somewhere in its declarations is not enough. See https://github.com/cython/cython/issues/6051

Describe alternatives you've considered.

No response

Additional context

No response

scoder avatar Feb 29 '24 11:02 scoder

Other candiates are:

  • _memoryviewslice - only used if you do the "to Python" conversion.
  • refcount_objects_in_slice and related functions - only applies if you have object memoryviews which are fairly rare.

In some cases (cdef function taking memoryview arguments) you might only need the memoryview reference counting (which needs to know the offset of one element of the memoryview class) and absolutely nothing else. But those cases are probably fairly rare.

da-woods avatar Feb 29 '24 18:02 da-woods

We're using Tempita for the memoryview code already, so if we add feature tracking, it should be easy to spread template conditions over the code.

scoder avatar Mar 01 '24 06:03 scoder