8000 MNT: Reorganize non-constant global statics into structs by ngoldbaum · Pull Request #26607 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MNT: Reorganize non-constant global statics into structs #26607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
baee891
MNT: move interned strings into a single global struct
ngoldbaum May 30, 2024
69075c1
MNT: move cached imports into a global struct
ngoldbaum May 21, 2024
e5c1bd6
MNT: move cpu dispatch registry into global data struct
ngoldbaum May 30, 2024
7719cf2
MNT: move ndarray.__array_*__ references to global data struct
ngoldbaum May 30, 2024
3cbb68d
MNT: move sys.flags.optimize cache to global data struct
ngoldbaum May 30, 2024
2ffcc71
MNT: set up tuple for truediv in global data struct
ngoldbaum May 30, 2024
d2ca21b
MNT: move unpack_bits LUT into global static struct
ngoldbaum May 30, 2024
a1f7200
MNT: move references to int(1) and int(0) to global static struct
ngoldbaum May 30, 2024
26c243d
MNT: move initialization of global ArrayMethods to module initialization
ngoldbaum May 30, 2024
536e5fb
MNT: move initialization of global tuples to global data struct
ngoldbaum May 30, 2024
0c22126
MNT: move default extobj contextvar to global data dict
ngoldbaum May 30, 2024
90b1f38
MNT: move PyArray_SetStringFunction internals into global data struct
ngoldbaum May 30, 2024
6a296c4
BUG: remove questionable static initialization of an array object
ngoldbaum May 30, 2024
398f095
MNT: split global data struct into two structs
ngoldbaum Jun 3, 2024
8f84875
MNT: add PyArrayMethodObject caches to static data struct
ngoldbaum Jun 5, 2024
402a83c
MNT: move some thread-unsafe state in thread-unsafe state struct
ngoldbaum Jun 5, 2024
e43275a
MNT: make data structs static instead of heap-allocated
ngoldbaum Jun 5, 2024
b706536
MNT: apply sebastian's refactoring suggestions
ngoldbaum Jun 6, 2024
c237038
MNT: move static data structs into their own file
ngoldbaum Jun 7, 2024
98ae65d
MNT: Add more global state I missed to the thread_unsafe_state struct
ngoldbaum Jun 7, 2024
a334ddc
MNT: verify all entries in npy_interned_str and npy_static_pydata are…
ngoldbaum Jun 11, 2024
9ed317f
Apply suggestions from code review
ngoldbaum Jun 13, 2024
3ae66b1
MAINT: apply more of Sebastian's suggestions
ngoldbaum Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MNT: apply sebastian's refactoring suggestions
  • Loading branch information
ngoldbaum committed Jun 19, 2024
commit b7065369cfacb8fee309e74a3e9737661fae5bd9
8 changes: 4 additions & 4 deletions numpy/_core/src/common/npy_cpu_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
NPY_VISIBILITY_HIDDEN int
npy_cpu_dispatch_tracer_init(PyObject *mod)
{
if (npy_ma_static_data.cpu_dispatch_registry != NULL) {
if (npy_static_pydata.cpu_dispatch_registry != NULL) {
PyErr_Format(PyExc_RuntimeError, "CPU dispatcher tracer already initlized");
return -1;
}
Expand All @@ -25,21 +25,21 @@ npy_cpu_dispatch_tracer_init(PyObject *mod)
if (err != 0) {
return -1;
}
npy_ma_static_data.cpu_dispatch_registry = reg_dict;
npy_static_pydata.cpu_dispatch_registry = reg_dict;
return 0;
}

NPY_VISIBILITY_HIDDEN void
npy_cpu_dispatch_trace(const char *fname, const char *signature,
const char **dispatch_info)
{
PyObject *func_dict = PyDict_GetItemString(npy_ma_static_data.cpu_dispatch_registry, fname);
PyObject *func_dict = PyDict_GetItemString(npy_static_pydata.cpu_dispatch_registry, fname);
if (func_dict == NULL) {
func_dict = PyDict_New();
if (func_dict == NULL) {
return;
}
int err = PyDict_SetItemString(npy_ma_static_data.cpu_dispatch_registry, fname, func_dict);
int err = PyDict_SetItemString(npy_static_pydata.cpu_dispatch_registry, fname, func_dict);
Py_DECREF(func_dict);
if (err != 0) {
return;
Expand Down
6 changes: 3 additions & 3 deletions numpy/_core/src/common/npy_ctypes.h
ED4F
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ npy_ctypes_check(PyTypeObject *obj)
int ret;

npy_cache_import("numpy._core._internal", "npy_ctypes_check",
&npy_ma_thread_unsafe_state.npy_ctypes_check);
if (npy_ma_thread_unsafe_state.npy_ctypes_check == NULL) {
&npy_thread_unsafe_state.npy_ctypes_check);
if (npy_thread_unsafe_state.npy_ctypes_check == NULL) {
goto fail;
}

ret_obj = PyObject_CallFunctionObjArgs(npy_ma_thread_unsafe_state.npy_ctypes_check,
ret_obj = PyObject_CallFunctionObjArgs(npy_thread_unsafe_state.npy_ctypes_check,
(PyObject *)obj, NULL);
if (ret_obj == NULL) {
goto fail;
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/common/ufunc_override.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj)
return NULL;
}
/* Ignore if the same as ndarray.__array_ufunc__ */
if (cls_array_ufunc == npy_ma_static_data.ndarray_array_ufunc) {
if (cls_array_ufunc == npy_static_pydata.ndarray_array_ufunc) {
Py_DECREF(cls_array_ufunc);
return NULL;
}
Expand Down
8 changes: 4 additions & 4 deletions numpy/_core/src/multiarray/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ NPY_NO_EXPORT PyObject *
_get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
{
#ifdef NPY_OS_LINUX
if (npy_ma_thread_unsafe_state.madvise_hugepage) {
if (npy_thread_unsafe_state.madvise_hugepage) {
Py_RETURN_TRUE;
}
#endif
Expand All @@ -66,12 +66,12 @@ _get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
NPY_NO_EXPORT PyObject *
_set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj)
{
int was_enabled = npy_ma_thread_unsafe_state.madvise_hugepage;
int was_enabled = npy_t F438 hread_unsafe_state.madvise_hugepage;
int enabled = PyObject_IsTrue(enabled_obj);
if (enabled < 0) {
return NULL;
}
npy_ma_thread_unsafe_state.madvise_hugepage = enabled;
npy_thread_unsafe_state.madvise_hugepage = enabled;
if (was_enabled) {
Py_RETURN_TRUE;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
#ifdef NPY_OS_LINUX
/* allow kernel allocating huge pages for large arrays */
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) &&
npy_ma_thread_unsafe_state.madvise_hugepage) {
npy_thread_unsafe_state.madvise_hugepage) {
npy_uintp offset = 4096u - (npy_uintp)p % (4096u);
npy_uintp length = nelem * esz - offset;
/**
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/src/multiarray/array_converter.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ static int
pyscalar_mode_conv(PyObject *obj, scalar_policy *policy)
{
PyObject *strings[3] = {
npy_ma_str.convert, npy_ma_str.preserve,
npy_ma_str.convert_if_no_array};
npy_interned_str.convert, npy_interned_str.preserve,
npy_interned_str.convert_if_no_array};

/* First quick pass using the identity (should practically always match) */
for (int i = 0; i < 3; i++) {
Expand Down
20 changes: 10 additions & 10 deletions numpy/_core/src/multiarray/arrayfunction_override.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ get_array_function(PyObject *obj)
{
/* Fast return for ndarray */
if (PyArray_CheckExact(obj)) {
Py_INCREF(npy_ma_static_data.ndarray_array_function);
return npy_ma_static_data.ndarray_array_function;
Py_INCREF(npy_static_pydata.ndarray_array_function);
return npy_static_pydata.ndarray_array_function;
}

PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str.array_function);
PyObject *array_function = PyArray_LookupSpecial(obj, npy_interned_str.array_function);
if (array_function == NULL && PyErr_Occurred()) {
PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
}
Expand Down Expand Up @@ -125,7 +125,7 @@ get_implementing_args_and_methods(PyObject *relevant_args,
static int
is_default_array_function(PyObject *obj)
{
return obj == npy_ma_static_data.ndarray_array_function;
return obj == npy_static_pydata.ndarray_array_function;
}


Expand Down Expand Up @@ -153,7 +153,7 @@ array_function_method_impl(PyObject *func, PyObject *types, PyObject *args,
}
}

PyObject *implementation = PyObject_GetAttr(func, npy_ma_str.implementation);
PyObject *implementation = PyObject_GetAttr(func, npy_interned_str.implementation);
if (implementation == NULL) {
return NULL;
}
Expand Down Expand Up @@ -233,10 +233,10 @@ set_no_matching_types_error(PyObject *public_api, PyObject *types)
/* No acceptable override found, raise TypeError. */
npy_cache_import("numpy._core._internal",
"array_function_errmsg_formatter",
&npy_ma_thread_unsafe_state.array_function_errmsg_formatter);
if (npy_ma_thread_unsafe_state.array_function_errmsg_formatter != NULL) {
&npy_thread_unsafe_state.array_function_errmsg_formatter);
if (npy_thread_unsafe_state.array_function_errmsg_formatter != NULL) {
PyObject *errmsg = PyObject_CallFunctionObjArgs(
npy_ma_thread_unsafe_state.array_function_errmsg_formatter,
npy_thread_unsafe_state.array_function_errmsg_formatter,
public_api, types, NULL);
if (errmsg != NULL) {
PyErr_SetObject(PyExc_TypeError, errmsg);
Expand Down Expand Up @@ -299,12 +299,12 @@ array_implement_c_array_function_creation(
}

/* The like argument must be present in the keyword arguments, remove it */
if (PyDict_DelItem(kwargs, npy_ma_str.like) < 0) {
if (PyDict_DelItem(kwargs, npy_interned_str.like) < 0) {
goto finish;
}

/* Fetch the actual symbol (the long way right now) */
numpy_module = PyImport_Import(npy_ma_str.numpy);
numpy_module = PyImport_Import(npy_interned_str.numpy);
if (numpy_module == NULL) {
goto finish;
}
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
if (result == NULL
&& (cmp_op == Py_EQ || cmp_op == Py_NE)
&& PyErr_ExceptionMatches(
83CB npy_ma_static_data._UFuncNoLoopError)) {
npy_static_pydata._UFuncNoLoopError)) {
PyErr_Clear();

PyArrayObject *array_other = (PyArrayObject *)PyArray_FROM_O(other);
Expand Down
2 changes: 1 addition & 1 deletion numpy/_core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -4185,7 +4185,7 @@ NPY_NO_EXPORT _PyArray_LegacyDescr @from@_Descr = {

/* The smallest type number is ?, the largest bounded by 'z'. */
#define _MAX_LETTER ('z' + 1)
#define LETTER_TO_NUM(letter) npy_ma_static_data._letter_to_num[letter - '?']
#define LETTER_TO_NUM(letter) npy_static_cdata._letter_to_num[letter - '?']

static _PyArray_LegacyDescr *_builtin_descrs[] = {
&BOOL_Descr,
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/src/multiarray/arraywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ npy_find_array_wrap(
}
}
else {
PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str.array_wrap);
PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_interned_str.array_wrap);
if (new_wrap == NULL) {
if (PyErr_Occurred()) {
goto fail;
Expand Down Expand Up @@ -160,7 +160,7 @@ npy_apply_wrap(
else {
/* Replace passed wrap/wrap_type (borrowed refs) with new_wrap/type. */
new_wrap = PyArray_LookupSpecial_OnInstance(
original_out, npy_ma_str.array_wrap);
original_out, npy_interned_str.array_wrap);
if (new_wrap != NULL) {
wrap = new_wrap;
wrap_type = (PyObject *)Py_TYPE(original_out);
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/src/multiarray/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix)
if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) {
/* Invoke the AxisError constructor */
PyObject *exc = PyObject_CallFunction(
npy_ma_static_data.AxisError, "iiO", *axis, ndim,
npy_static_pydata.AxisError, "iiO", *axis, ndim,
msg_prefix);
if (exc == NULL) {
return -1;
}
PyErr_SetObject(npy_ma_static_data.AxisError, exc);
PyErr_SetObject(npy_static_pydata.AxisError, exc);
Py_DECREF(exc);

return -1;
Expand Down
4 changes: 2 additions & 2 deletions numpy/_core/src/multiarray/common_dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PyArray_CommonDType(PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2)
}
if (common_dtype == (PyArray_DTypeMeta *)Py_NotImplemented) {
Py_DECREF(Py_NotImplemented);
PyErr_Format(npy_ma_static_data.DTypePromotionError,
PyErr_Format(npy_static_pydata.DTypePromotionError,
"The DTypes %S and %S do not have a common DType. "
"For example they cannot be stored in a single array unless "
"the dtype is `object`.", dtype1, dtype2);
Expand Down Expand Up @@ -285,7 +285,7 @@ PyArray_PromoteDTypeSequence(
Py_INCREF(dtypes_in[l]);
PyTuple_SET_ITEM(dtypes_in_tuple, l, (PyObject *)dtypes_in[l]);
}
PyErr_Format(npy_ma_static_data.DTypePromotionError,
PyErr_Format(npy_static_pydata.DTypePromotionError,
"The DType %S could not be promoted by %S. This means that "
"no common DType exists for the given inputs. "
"For example they cannot be stored in a single array unless "
Expand Down
8 changes: 4 additions & 4 deletions numpy/_core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *const *args, Py_ssize_t

/* Don't add docstrings */
#if PY_VERSION_HEX > 0x030b0000
if (npy_ma_static_data.optimize > 1) {
if (npy_static_cdata.optimize > 1) {
#else
if (Py_OptimizeFlag > 1) {
#endif
Expand Down Expand Up @@ -1858,15 +1858,15 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
/* for unity stride we can just copy out of the lookup table */
if (order == 'b') {
for (index = 0; index < in_n; index++) {
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64;
memcpy(outptr, &v, 8);
outptr += 8;
inptr += in_stride;
}
}
else {
for (index = 0; index < in_n; index++) {
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64;
if (order != 'b') {
v = npy_bswap8(v);
}
Expand All @@ -1877,7 +1877,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
}
/* Clean up the tail portion */
if (in_tail) {
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
npy_uint64 v = npy_static_cdata.unpack_lookup_big[*inptr].uint64;
if (order != 'b') {
v = npy_bswap8(v);
}
Expand Down
8 changes: 4 additions & 4 deletions numpy/_core/src/multiarray/conversion_utils.c
779E
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {

int int_copymode;

if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data._CopyMode) {
if ((PyObject *)Py_TYPE(obj) == npy_static_pydata._CopyMode) {
PyObject* mode_value = PyObject_GetAttrString(obj, "value");
if (mode_value == NULL) {
return NPY_FAIL;
Expand Down Expand Up @@ -270,7 +270,7 @@ PyArray_AsTypeCopyConverter(PyObject *obj, NPY_ASTYPECOPYMODE *copymode)
{
int int_copymode;

if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data._CopyMode) {
if ((PyObject *)Py_TYPE(obj) == npy_static_pydata._CopyMode) {
PyErr_SetString(PyExc_ValueError,
"_CopyMode enum is not allowed for astype function. "
"Use true/false instead.");
Expand Down Expand Up @@ -1411,7 +1411,7 @@ PyArray_IntTupleFromIntp(int len, npy_intp const *vals)
NPY_NO_EXPORT int
_not_NoValue(PyObject *obj, PyObject **out)
{
if (obj == npy_ma_static_data._NoValue) {
if (obj == npy_static_pydata._NoValue) {
*out = NULL;
}
else {
Expand All @@ -1431,7 +1431,7 @@ PyArray_DeviceConverterOptional(PyObject *object, NPY_DEVICE *device)
}

if (PyUnicode_Check(object) &&
PyUnicode_Compare(object, npy_ma_str.cpu) == 0) {
PyUnicode_Compare(object, npy_interned_str.cpu) == 0) {
*device = NPY_DEVICE_CPU;
return NPY_SUCCEED;
}
Expand Down
Loading
0