Repository: numpy/numpy
File: numpy/_core/src/multiarray/methods.c (C)

Code before:
    if (PyArray_IS_C_CONTIGUOUS((PyArrayObject *)self)) {
        order = 'C';
        picklebuf_args = Py_BuildValue("(O)", self);
        rev_perm = Py_BuildValue("O", Py_None);
    }
    else if (PyArray_IS_F_CONTIGUOUS((PyArrayObject *)self)) {
        /* if the array if Fortran-contiguous and not C-contiguous,
         * the PickleBuffer instance will hold a view on the transpose
         * of the initial array, that is C-contiguous. */
        order = 'F';
        transposed_array = PyArray_Transpose((PyArrayObject *)self, NULL);
        picklebuf_args = Py_BuildValue("(N)", transposed_array);
        rev_perm = Py_BuildValue("O", Py_None);
    }
    else {
        order = 'K'; // won't be used
        const int n = PyArray_NDIM(self);
        npy_stride_sort_item items[NPY_MAXDIMS];
        // sort (strde, perm) as descending = transpose to C
        PyArray_CreateSortedStridePerm(n, PyArray_STRIDES(self), items);

        // check if nparray is transposed_contiguous
        for (int i = n - 1, check_s = 1; i >= 0; i--) {
            if (check_s * PyArray_DESCR(self)->elsize !=
                items[i].stride) {
                return array_reduce_ex_regular(self, protocol);
            }
            check_s *= PyArray_DIMS(self)[items[i].perm];
        }

        rev_perm = PyTuple_New(n);
        PyArray_Dims perm;
        npy_intp d[NPY_MAXDIMS];
        for (int i = 0; i < n; i++) {
            d[i] = items[i].perm;
            PyTuple_SET_ITEM(rev_perm, items[i].perm, PyLong_FromLong(i));
        }
        perm.ptr = d;
        perm.len = n;
        transposed_array = PyArray_Transpose((PyArrayObject *)self, &perm);
        picklebuf_args = Py_BuildValue("(N)", transposed_array);
    }
    if (picklebuf_args == NULL) {
        Py_DECREF(picklebuf_class);
        return NULL;
    }

    buffer = PyObject_CallObject(picklebuf_class, picklebuf_args);
    Py_DECREF(picklebuf_class);
    Py_DECREF(picklebuf_args);
    if (buffer == NULL) {

Review comment (performance):
If we assume it works usually and/or the other path is slow anyway, could also just not check this and see if the transpose works out?
Not sure that is better, though.

Code after:
        PyArray_Dims perm;
        npy_intp dims[NPY_MAXDIMS];
        for (int i = 0; i < n; i++) {
            dims[i] = items[i].perm;
            PyObject *idx = PyLong_FromLong(i);
            if (idx == NULL) {
                Py_DECREF(rev_perm);
                return NULL;
            }
            PyTuple_SET_ITEM(rev_perm, items[i].perm, idx);
        }
        perm.ptr = dims;
        perm.len = n;
        transposed_array = PyArray_Transpose((PyArrayObject *)self, &perm);
        if (transposed_array == NULL) {
            Py_DECREF(rev_perm);
            return NULL;
        }
        if (!PyArray_IS_C_CONTIGUOUS((PyArrayObject *)transposed_array)) {
            // self is non-contiguous
            Py_DECREF(rev_perm);
            Py_DECREF(transposed_array);
            return array_reduce_ex_regular(self, protocol);
        }
    }
    buffer = PyObject_CallOneArg(picklebuf_class, transposed_array == NULL ? (PyObject*) self: transposed_array);
    if (buffer == NULL) {
        /* Some arrays may refuse to export a buffer, in which case
         * just fall back on regular __reduce_ex__ implementation
         * (gh-12745).
         */
        Py_XDECREF(rev_perm);
        Py_XDECREF(transposed_array);
        PyErr_Clear();
        return array_reduce_ex_regular(self, protocol);
    }

    /* Get the _frombuffer() function for reconstruction */
    if (npy_cache_import_runtime("numpy._core.numeric", "_frombuffer",
                                 &from_buffer_func) == -1) {
        Py_XDECREF(rev_perm);
        Py_XDECREF(transposed_array);
        Py_DECREF(buffer);
        return NULL;
    }

    PyObject *shape = NULL;
    if (order == 'K') {
        shape = PyArray_IntTupleFromIntp(
                PyArray_NDIM((PyArrayObject *)transposed_array),
                PyArray_SHAPE((PyArrayObject *)transposed_array));