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

Code before:
    PyArray_Descr *dtype;
    int ap1_flags = NPY_ARRAY_NOTSWAPPED | NPY_ARRAY_ALIGNED;
    PyArray_BinSearchFunc *binsearch = NULL;
    PyArray_ArgBinSearchFunc *argbinsearch = NULL;
    NPY_BEGIN_THREADS_DEF;

    /* Find common type */
    dtype = PyArray_DescrFromObject((PyObject *)op2, PyArray_DESCR(op1));
    if (dtype == NULL) {
        return NULL;
    }

    /* Look for binary search function */
    if (perm) {
        argbinsearch = get_argbinsearch_func(dtype, side);
    }
    else {
        binsearch = get_binsearch_func(dtype, side);
    }
    if (binsearch == NULL && argbinsearch == NULL) {
        PyErr_SetString(PyExc_TypeError, "compare not supported for type");
        Py_DECREF(dtype);
        return NULL;
    }

    /* need ap2 as contiguous array and of right dtype (steals and may be replace it) */
    ap2 = (PyArrayObject *)PyArray_CheckFromAny(op2, dtype,
                                0, 0,
                                NPY_ARRAY_CARRAY_RO | NPY_ARRAY_NOTSWAPPED,
                                NULL);
    if (ap2 == NULL) {
        return NULL;
    }
    /* dtype was stolen, replace it in case the array creation replaced it. */
    dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));

    /*
     * If the needle (ap2) is larger than the haystack (op1) we copy the
     * haystack to a contiguous array for improved cache utilization.
     */
    if (PyArray_SIZE(ap2) > PyArray_SIZE(op1)) {
        ap1_flags |= NPY_ARRAY_CARRAY_RO;
    }
    /* dtype is stolen, after this we have no reference */
    ap1 = (PyArrayObject *)PyArray_CheckFromAny((PyObject *)op1, dtype,
                                1, 1, ap1_flags, NULL);
    if (ap1 == NULL) {
        goto fail;
    }

    if (perm) {

Review comment (suggestion):
`replace it` -> `replace dtype` (since "it" could also refer to `ap2`, the array).

Actually, with my comment below, I think this can just be
```
/* need ap2 as contiguous array and of right dtype (note: steals dtype reference) */

Code after:
    PyArray_Descr *dtype;
    int ap1_flags = NPY_ARRAY_NOTSWAPPED | NPY_ARRAY_ALIGNED;
    PyArray_BinSearchFunc *binsearch = NULL;
    PyArray_ArgBinSearchFunc *argbinsearch = NULL;
    NPY_BEGIN_THREADS_DEF;

    /* Find common type */
    dtype = PyArray_DescrFromObject((PyObject *)op2, PyArray_DESCR(op1));
    if (dtype == NULL) {
        return NULL;
    }

    /* Look for binary search function */
    if (perm) {
        argbinsearch = get_argbinsearch_func(dtype, side);
    }
    else {
        binsearch = get_binsearch_func(dtype, side);
    }
    if (binsearch == NULL && argbinsearch == NULL) {
        PyErr_SetString(PyExc_TypeError, "compare not supported for type");
        Py_DECREF(dtype);
        return NULL;
    }

    /* need ap2 as contiguous array and of right dtype (note: steals dtype reference) */
    ap2 = (PyArrayObject *)PyArray_CheckFromAny(op2, dtype,
                                0, 0,
                                NPY_ARRAY_CARRAY_RO | NPY_ARRAY_NOTSWAPPED,
                                NULL);
    if (ap2 == NULL) {
        return NULL;
    }
    /*
     * The dtype reference we had was used for creating ap2, which may have
     * replaced it with another. So here we copy the dtype of ap2 and use it for `ap1`.
     */
     dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));

    /*
     * If the needle (ap2) is larger than the haystack (op1) we copy the
     * haystack to a contiguous array for improved cache utilization.
     */
    if (PyArray_SIZE(ap2) > PyArray_SIZE(op1)) {
        ap1_flags |= NPY_ARRAY_CARRAY_RO;
    }
    /* dtype is stolen, after this we have no reference */
    ap1 = (PyArrayObject *)PyArray_CheckFromAny((PyObject *)op1, dtype,
                                1, 1, ap1_flags, NULL);
    if (ap1 == NULL) {
        goto fail;