summaryrefslogtreecommitdiff
path: root/numpy/core/src/multiarray
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2023-02-20 21:51:23 +0100
committerSebastian Berg <sebastianb@nvidia.com>2023-02-20 21:51:23 +0100
commit1779c5aa5fd9c2992e16f7d3031c3fd0f0e22bfc (patch)
treef56d0329c7e887e8f176c704f08b072feb92cc4e /numpy/core/src/multiarray
parent138bba5ffa1945e13cc9633cbc7577753e7ccd50 (diff)
downloadnumpy-1779c5aa5fd9c2992e16f7d3031c3fd0f0e22bfc.tar.gz
MAINT: Avoid PyArray_Item_INCREF in putmask
Diffstat (limited to 'numpy/core/src/multiarray')
-rw-r--r--numpy/core/src/multiarray/item_selection.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c
index f963e82a2..51417d253 100644
--- a/numpy/core/src/multiarray/item_selection.c
+++ b/numpy/core/src/multiarray/item_selection.c
@@ -632,11 +632,12 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0)
{
PyArrayObject *mask, *values;
PyArray_Descr *dtype;
- npy_intp chunk, ni, nv;
+ npy_intp itemsize, ni, nv;
char *src, *dest;
npy_bool *mask_data;
int copied = 0;
int overlap = 0;
+ NPY_BEGIN_THREADS_DEF;
mask = NULL;
values = NULL;
@@ -697,31 +698,47 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0)
self = obj;
}
- chunk = PyArray_DESCR(self)->elsize;
+ itemsize = PyArray_DESCR(self)->elsize;
dest = PyArray_DATA(self);
if (PyDataType_REFCHK(PyArray_DESCR(self))) {
+ NPY_cast_info cast_info;
+ NPY_ARRAYMETHOD_FLAGS flags;
+ const npy_intp one = 1;
+ const npy_intp strides[2] = {itemsize, itemsize};
+
+ NPY_cast_info_init(&cast_info);
+ if (PyArray_GetDTypeTransferFunction(
+ PyArray_ISALIGNED(self), itemsize, itemsize, dtype, dtype, 0,
+ &cast_info, &flags) < 0) {
+ return NULL;
+ }
+ if (!(flags & NPY_METH_REQUIRES_PYAPI)) {
+ NPY_BEGIN_THREADS;
+ }
+
for (npy_intp i = 0, j = 0; i < ni; i++, j++) {
if (j >= nv) {
j = 0;
}
if (mask_data[i]) {
- char *src_ptr = src + j*chunk;
- char *dest_ptr = dest + i*chunk;
-
- PyArray_Item_INCREF(src_ptr, PyArray_DESCR(self));
- PyArray_Item_XDECREF(dest_ptr, PyArray_DESCR(self));
- memmove(dest_ptr, src_ptr, chunk);
+ char *data[2] = {src + j*itemsize, dest + i*itemsize};
+ if (cast_info.func(
+ &cast_info.context, data, &one, strides,
+ cast_info.auxdata) < 0) {
+ NPY_END_THREADS;
+ goto fail;
+ }
}
}
}
else {
- NPY_BEGIN_THREADS_DEF;
- NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(self));
- npy_fastputmask(dest, src, mask_data, ni, nv, chunk);
- NPY_END_THREADS;
+ NPY_BEGIN_THREADS;
+ npy_fastputmask(dest, src, mask_data, ni, nv, itemsize);
}
+ NPY_END_THREADS;
+
Py_XDECREF(values);
Py_XDECREF(mask);
if (copied) {