cython icon indicating copy to clipboard operation
cython copied to clipboard

[ENH] C++ return should move (at least in some cases)

Open da-woods opened this issue 1 year ago • 5 comments

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

#distutils: language=c++

from libcpp.vector cimport vector

cdef vector[double] f():
    cdef vector[double] v
    return v

This generates

  __pyx_r = __pyx_v_v;
  goto __pyx_L0;

  /* function exit code */
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;

Ideally __pyx_r = __pyx_v_v; should have a move_if_possible

Describe the solution you'd like.

No response

Describe alternatives you've considered.

No response

Additional context

This isn't universal I think - it should only apply to non-reference function local variables or temps. So it'd need to be implemented somewhat carefully (e.g. returning a member of a cdef class shouldn't move)

da-woods avatar Feb 28 '23 19:02 da-woods