Skip to content

Commit

Permalink
Merge pull request #22924 from seberg/refactor-decref-meth-only
Browse files Browse the repository at this point in the history
MAINT: Refactor clearing up of array data
  • Loading branch information
mattip committed Feb 20, 2023
2 parents 86450a0 + f2be9f7 commit af9f656
Show file tree
Hide file tree
Showing 19 changed files with 938 additions and 431 deletions.
1 change: 1 addition & 0 deletions numpy/core/meson.build
Expand Up @@ -695,6 +695,7 @@ src_multiarray = [
'src/multiarray/dtypemeta.c',
'src/multiarray/dragon4.c',
'src/multiarray/dtype_transfer.c',
'src/multiarray/dtype_traversal.c',
src_file.process('src/multiarray/einsum.c.src'),
src_file.process('src/multiarray/einsum_sumprod.c.src'),
'src/multiarray/experimental_public_dtype_api.c',
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/setup.py
Expand Up @@ -840,6 +840,7 @@ def get_mathlib_info(*args):
join('src', 'multiarray', 'descriptor.h'),
join('src', 'multiarray', 'dtypemeta.h'),
join('src', 'multiarray', 'dtype_transfer.h'),
join('src', 'multiarray', 'dtype_traversal.h'),
join('src', 'multiarray', 'dragon4.h'),
join('src', 'multiarray', 'einsum_debug.h'),
join('src', 'multiarray', 'einsum_sumprod.h'),
Expand Down Expand Up @@ -913,6 +914,7 @@ def get_mathlib_info(*args):
join('src', 'multiarray', 'dtypemeta.c'),
join('src', 'multiarray', 'dragon4.c'),
join('src', 'multiarray', 'dtype_transfer.c'),
join('src', 'multiarray', 'dtype_traversal.c'),
join('src', 'multiarray', 'einsum.c.src'),
join('src', 'multiarray', 'einsum_sumprod.c.src'),
join('src', 'multiarray', 'experimental_public_dtype_api.c'),
Expand Down
43 changes: 27 additions & 16 deletions numpy/core/src/multiarray/array_method.c
Expand Up @@ -344,28 +344,39 @@ fill_arraymethod_from_slots(
}

/* Check whether the provided loops make sense. */
if (meth->flags & NPY_METH_SUPPORTS_UNALIGNED) {
if (meth->unaligned_strided_loop == NULL) {
PyErr_Format(PyExc_TypeError,
"Must provide unaligned strided inner loop when using "
"NPY_METH_SUPPORTS_UNALIGNED flag (in method: %s)",
spec->name);
return -1;
}
}
else {
if (meth->unaligned_strided_loop != NULL) {
PyErr_Format(PyExc_TypeError,
"Must not provide unaligned strided inner loop when not "
"using NPY_METH_SUPPORTS_UNALIGNED flag (in method: %s)",
spec->name);
return -1;
}
}
/* Fill in the blanks: */
if (meth->unaligned_contiguous_loop == NULL) {
meth->unaligned_contiguous_loop = meth->unaligned_strided_loop;
}
if (meth->strided_loop == NULL) {
PyErr_Format(PyExc_TypeError,
"Must provide a strided inner loop function. (method: %s)",
spec->name);
return -1;
meth->strided_loop = meth->unaligned_strided_loop;
}
if (meth->contiguous_loop == NULL) {
meth->contiguous_loop = meth->strided_loop;
}
if (meth->unaligned_contiguous_loop != NULL &&
meth->unaligned_strided_loop == NULL) {
PyErr_Format(PyExc_TypeError,
"Must provide unaligned strided inner loop when providing "
"a contiguous version. (method: %s)", spec->name);
return -1;
}
if ((meth->unaligned_strided_loop == NULL) !=
!(meth->flags & NPY_METH_SUPPORTS_UNALIGNED)) {

if (meth->strided_loop == NULL) {
PyErr_Format(PyExc_TypeError,
"Must provide unaligned strided inner loop when casting spec "
"has the NPY_METH_SUPPORTS_UNALIGNED flag. "
"(method: %s)", spec->name);
"Must provide a strided inner loop function. (method: %s)",
spec->name);
return -1;
}

Expand Down
7 changes: 4 additions & 3 deletions numpy/core/src/multiarray/arrayobject.c
Expand Up @@ -56,6 +56,7 @@ maintainer email: oliphant.travis@ieee.org
#include "alloc.h"
#include "mem_overlap.h"
#include "numpyos.h"
#include "refcount.h"
#include "strfuncs.h"

#include "binop_override.h"
Expand Down Expand Up @@ -452,9 +453,9 @@ array_dealloc(PyArrayObject *self)
}

if ((fa->flags & NPY_ARRAY_OWNDATA) && fa->data) {
/* Free internal references if an Object array */
if (PyDataType_FLAGCHK(fa->descr, NPY_ITEM_REFCOUNT)) {
PyArray_XDECREF(self);
/* Free any internal references */
if (PyDataType_REFCHK(fa->descr)) {
PyArray_ClearArray(self);
}
if (fa->mem_handler == NULL) {
char *env = getenv("NUMPY_WARN_IF_NO_MEM_POLICY");
Expand Down
24 changes: 21 additions & 3 deletions numpy/core/src/multiarray/convert_datatype.c
Expand Up @@ -31,6 +31,7 @@
#include "array_method.h"
#include "usertypes.h"
#include "dtype_transfer.h"
#include "dtype_traversal.h"
#include "arrayobject.h"


Expand Down Expand Up @@ -152,7 +153,7 @@ PyArray_GetCastingImpl(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
{
PyObject *res;
if (from == to) {
res = NPY_DT_SLOTS(from)->within_dtype_castingimpl;
res = (PyObject *)NPY_DT_SLOTS(from)->within_dtype_castingimpl;
}
else {
res = PyDict_GetItemWithError(NPY_DT_SLOTS(from)->castingimpls, (PyObject *)to);
Expand Down Expand Up @@ -2414,8 +2415,7 @@ PyArray_AddCastingImplementation(PyBoundArrayMethodObject *meth)
return -1;
}
Py_INCREF(meth->method);
NPY_DT_SLOTS(meth->dtypes[0])->within_dtype_castingimpl = (
(PyObject *)meth->method);
NPY_DT_SLOTS(meth->dtypes[0])->within_dtype_castingimpl = meth->method;

return 0;
}
Expand Down Expand Up @@ -3912,6 +3912,21 @@ PyArray_InitializeObjectToObjectCast(void)
}


static int
PyArray_SetClearFunctions(void)
{
PyArray_DTypeMeta *Object = PyArray_DTypeFromTypeNum(NPY_OBJECT);
NPY_DT_SLOTS(Object)->get_clear_loop = &npy_get_clear_object_strided_loop;
Py_DECREF(Object); /* use borrowed */

PyArray_DTypeMeta *Void = PyArray_DTypeFromTypeNum(NPY_VOID);
NPY_DT_SLOTS(Void)->get_clear_loop = &npy_get_clear_void_and_legacy_user_dtype_loop;
Py_DECREF(Void); /* use borrowed */
return 0;
}



NPY_NO_EXPORT int
PyArray_InitializeCasts()
{
Expand All @@ -3931,5 +3946,8 @@ PyArray_InitializeCasts()
if (PyArray_InitializeDatetimeCasts() < 0) {
return -1;
}
if (PyArray_SetClearFunctions() < 0) {
return -1;
}
return 0;
}

0 comments on commit af9f656

Please sign in to comment.