8000 MNT: make data structs static instead of heap-allocated · numpy/numpy@f3643a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3643a2

Browse files
committed
MNT: make data structs static instead of heap-allocated
1 parent c35d7a0 commit f3643a2

38 files changed

+276
-279
lines changed

numpy/_core/src/common/npy_cpu_dispatch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
NPY_VISIBILITY_HIDDEN int
99
npy_cpu_dispatch_tracer_init(PyObject *mod)
1010
{
11-
if (npy_ma_static_data->cpu_dispatch_registry != NULL) {
11+
if (npy_ma_static_data.cpu_dispatch_registry != NULL) {
1212
PyErr_Format(PyExc_RuntimeError, "CPU dispatcher tracer already initlized");
1313
return -1;
1414
}
@@ -25,21 +25,21 @@ npy_cpu_dispatch_tracer_init(PyObject *mod)
2525
if (err != 0) {
2626
return -1;
2727
}
28-
npy_ma_static_data->cpu_dispatch_registry = reg_dict;
28+
npy_ma_static_data.cpu_dispatch_registry = reg_dict;
2929
return 0;
3030
}
3131

3232
NPY_VISIBILITY_HIDDEN void
3333
npy_cpu_dispatch_trace(const char *fname, const char *signature,
3434
const char **dispatch_info)
3535
{
36-
PyObject *func_dict = PyDict_GetItemString(npy_ma_static_data->cpu_dispatch_registry, fname);
36+
PyObject *func_dict = PyDict_GetItemString(npy_ma_static_data.cpu_dispatch_registry, fname);
3737
if (func_dict == NULL) {
3838
func_dict = PyDict_New();
3939
if (func_dict == NULL) {
4040
return;
4141
}
42-
int err = PyDict_SetItemString(npy_ma_static_data->cpu_dispatch_registry, fname, func_dict);
42+
int err = PyDict_SetItemString(npy_ma_static_data.cpu_dispatch_registry, fname, func_dict);
4343
Py_DECREF(func_dict);
4444
if (err != 0) {
4545
return;

numpy/_core/src/common/npy_ctypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ npy_ctypes_check(PyTypeObject *obj)
2222
int ret;
2323

2424
npy_cache_import("numpy._core._internal", "npy_ctypes_check",
25-
&npy_ma_thread_unsafe_state->npy_ctypes_check);
26-
if (npy_ma_thread_unsafe_state->npy_ctypes_check == NULL) {
25+
&npy_ma_thread_unsafe_state.npy_ctypes_check);
26+
if (npy_ma_thread_unsafe_state.npy_ctypes_check == NULL) {
2727
goto fail;
2828
}
2929

30-
ret_obj = PyObject_CallFunctionObjArgs(npy_ma_thread_unsafe_state->npy_ctypes_check,
30+
ret_obj = PyObject_CallFunctionObjArgs(npy_ma_thread_unsafe_state.npy_ctypes_check,
3131
(PyObject *)obj, NULL);
3232
if (ret_obj == NULL) {
3333
goto fail;

numpy/_core/src/common/ufunc_override.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ PyUFuncOverride_GetNonDefaultArrayUfunc(PyObject *obj)
4343
return NULL;
4444
}
4545
/* Ignore if the same as ndarray.__array_ufunc__ */
46-
if (cls_array_ufunc == npy_ma_static_data->ndarray_array_ufunc) {
46+
if (cls_array_ufunc == npy_ma_static_data.ndarray_array_ufunc) {
4747
Py_DECREF(cls_array_ufunc);
4848
return NULL;
4949
}

numpy/_core/src/multiarray/alloc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ NPY_NO_EXPORT PyObject *
4848
_get_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
4949
{
5050
#ifdef NPY_OS_LINUX
51-
if (npy_ma_thread_unsafe_state->madvise_hugepage) {
51+
if (npy_ma_thread_unsafe_state.madvise_hugepage) {
5252
Py_RETURN_TRUE;
5353
}
5454
#endif
@@ -66,12 +66,12 @@ _get_madvise_hugepa 10000 ge(PyObject *NPY_UNUSED(self), PyObject *NPY_UNUSED(args))
6666
NPY_NO_EXPORT PyObject *
6767
_set_madvise_hugepage(PyObject *NPY_UNUSED(self), PyObject *enabled_obj)
6868
{
69-
int was_enabled = npy_ma_thread_unsafe_state->madvise_hugepage;
69+
int was_enabled = npy_ma_thread_unsafe_state.madvise_hugepage;
7070
int enabled = PyObject_IsTrue(enabled_obj);
7171
if (enabled < 0) {
7272
return NULL;
7373
}
74-
npy_ma_thread_unsafe_state->madvise_hugepage = enabled;
74+
npy_ma_thread_unsafe_state.madvise_hugepage = enabled;
7575
if (was_enabled) {
7676
Py_RETURN_TRUE;
7777
}
@@ -110,7 +110,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
110110
#ifdef NPY_OS_LINUX
111111
/* allow kernel allocating huge pages for large arrays */
112112
if (NPY_UNLIKELY(nelem * esz >= ((1u<<22u))) &&
113-
npy_ma_thread_unsafe_state->madvise_hugepage) {
113+
npy_ma_thread_unsafe_state.madvise_hugepage) {
114114
npy_uintp offset = 4096u - (npy_uintp)p % (4096u);
115115
npy_uintp length = nelem * esz - offset;
116116
/**

numpy/_core/src/multiarray/array_converter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ static int
186186
pyscalar_mode_conv(PyObject *obj, scalar_policy *policy)
187187
{
188188
PyObject *strings[3] = {
189-
npy_ma_str->convert, npy_ma_str->preserve,
190-
npy_ma_str->convert_if_no_array};
189+
npy_ma_str.convert, npy_ma_str.preserve,
190+
npy_ma_str.convert_if_no_array};
191191

192192
/* First quick pass using the identity (should practically always match) */
193193
for (int i = 0; i < 3; i++) {

numpy/_core/src/multiarray/arrayfunction_override.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ get_array_function(PyObject *obj)
2020
{
2121
/* Fast return for ndarray */
2222
if (PyArray_CheckExact(obj)) {
23-
Py_INCREF(npy_ma_static_data->ndarray_array_function);
24-
return npy_ma_static_data->ndarray_array_function;
23+
Py_INCREF(npy_ma_static_data.ndarray_array_function);
24+
return npy_ma_static_data.ndarray_array_function;
2525
}
2626

27-
PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str->array_function);
27+
PyObject *array_function = PyArray_LookupSpecial(obj, npy_ma_str.array_function);
2828
if (array_function == NULL && PyErr_Occurred()) {
2929
PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */
3030
}
@@ -125,7 +125,7 @@ get_implementing_args_and_methods(PyObject *relevant_args,
125125
static int
126126
is_default_array_function(PyObject *obj)
127127
{
128-
return obj == npy_ma_static_data->ndarray_array_function;
128+
return obj == npy_ma_static_data.ndarray_array_function;
129129
}
130130

131131

@@ -153,7 +153,7 @@ array_function_method_impl(PyObject *func, PyObject *types, PyObject *args,
153153
}
154154
}
155155

156-
PyObject *implementation = PyObject_GetAttr(func, npy_ma_str->implementation);
156+
PyObject *implementation = PyObject_GetAttr(func, npy_ma_str.implementation);
157157
if (implementation == NULL) {
158158
return NULL;
159159
}
@@ -233,10 +233,10 @@ set_no_matching_types_error(PyObject *public_api, PyObject *types)
233233
/* No acceptable override found, raise TypeError. */
234234
npy_cache_import("numpy._core._internal",
235235
"array_function_errmsg_formatter",
236-
&npy_ma_thread_unsafe_state->array_function_errmsg_formatter);
237-
if (npy_ma_thread_unsafe_state->array_function_errmsg_formatter != NULL) {
236+
&npy_ma_thread_unsafe_state.array_function_errmsg_formatter);
237+
if (npy_ma_thread_unsafe_state.array_function_errmsg_formatter != NULL) {
238238
PyObject *errmsg = PyObject_CallFunctionObjArgs(
239-
npy_ma_thread_unsafe_state->array_function_errmsg_formatter,
239+
npy_ma_thread_unsafe_state.array_function_errmsg_formatter,
240240
public_api, types, NULL);
241241
if (errmsg != NULL) {
242242
PyErr_SetObject(PyExc_TypeError, errmsg);
@@ -299,12 +299,12 @@ array_implement_c_array_function_creation(
299299
}
300300

301301
/* The like argument must be present in the keyword arguments, remove it */
302-
if (PyDict_DelItem(kwargs, npy_ma_str->like) < 0) {
302+
if (PyDict_DelItem(kwargs, npy_ma_str.like) < 0) {
303303
goto finish;
304304
}
305305

306306
/* Fetch the actual symbol (the long way right now) */
307-
numpy_module = PyImport_Import(npy_ma_str->numpy);
307+
numpy_module = PyImport_Import(npy_ma_str.numpy);
308308
if (numpy_module == NULL) {
309309
goto finish;
310310
}

numpy/_core/src/multiarray/arrayobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
928928
if (result == NULL
929929
&& (cmp_op == Py_EQ || cmp_op == Py_NE)
930930
&& PyErr_ExceptionMatches(
931-
npy_ma_static_data->_UFuncNoLoopError)) {
931+
npy_ma_static_data._UFuncNoLoopError)) {
932932
PyErr_Clear();
933933

934934
PyArrayObject *array_other = (PyArrayObject *)PyArray_FROM_O(other);

numpy/_core/src/multiarray/arraytypes.c.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4185,7 +4185,7 @@ NPY_NO_EXPORT _PyArray_LegacyDescr @from@_Descr = {
41854185

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

41904190
static _PyArray_LegacyDescr *_builtin_descrs[] = {
41914191
&BOOL_Descr,

numpy/_core/src/multiarray/arraywrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ npy_find_array_wrap(
5757
}
5858
}
5959
else {
60-
PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str->array_wrap);
60+
PyObject *new_wrap = PyArray_LookupSpecial_OnInstance(obj, npy_ma_str.array_wrap);
6161
if (new_wrap == NULL) {
6262
if (PyErr_Occurred()) {
6363
goto fail;
@@ -160,7 +160,7 @@ npy_apply_wrap(
160160
else {
161161
/* Replace passed wrap/wrap_type (borrowed refs) with new_wrap/type. */
162162
new_wrap = PyArray_LookupSpecial_OnInstance(
163-
original_out, npy_ma_str->array_wrap);
163+
original_out, npy_ma_str.array_wrap);
164164
if (new_wrap != NULL) {
165165
wrap = new_wrap;
166166
wrap_type = (PyObject *)Py_TYPE(original_out);

numpy/_core/src/multiarray/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix)
142142
if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) {
143143
/* Invoke the AxisError constructor */
144144
PyObject *exc = PyObject_CallFunction(
145-
npy_ma_static_data->AxisError, "iiO", *axis, ndim,
145+
npy_ma_static_data.AxisError, "iiO", *axis, ndim,
146146
msg_prefix);
147147
if (exc == NULL) {
148148
return -1;
149149
}
150-
PyErr_SetObject(npy_ma_static_data->AxisError, exc);
150+
PyErr_SetObject(npy_ma_static_data.AxisError, exc);
151151
Py_DECREF(exc);
152152

153153
return -1;

numpy/_core/src/multiarray/common_dtype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ PyArray_CommonDType(PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2)
6464
}
6565
if (common_dtype == (PyArray_DTypeMeta *)Py_NotImplemented) {
6666
Py_DECREF(Py_NotImplemented);
67-
PyErr_Format(npy_ma_static_data->DTypePromotionError,
67+
PyErr_Format(npy_ma_static_data.DTypePromotionError,
6868
"The DTypes %S and %S do not have a common DType. "
6969
"For example they cannot be stored in a single array unless "
7070
"the dtype is `object`.", dtype1, dtype2);
@@ -285,7 +285,7 @@ PyArray_PromoteDTypeSequence(
285285
Py_INCREF(dtypes_in[l]);
286286
PyTuple_SET_ITEM(dtypes_in_tuple, l, (PyObject *)dtypes_in[l]);
287287
}
288-
PyErr_Format(npy_ma_static_data->DTypePromotionError,
288+
PyErr_Format(npy_ma_static_data.DTypePromotionError,
289289
"The DType %S could not be promoted by %S. This means that "
290290
"no common DType exists for the given inputs. "
291291
"For example they cannot be stored in a single array unless "

numpy/_core/src/multiarray/compiled_base.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *const *args, Py_ssize_t
14141414

14151415
/* Don't add docstrings */
14161416
#if PY_VERSION_HEX > 0x030b0000
1417-
if (npy_ma_static_data->optimize > 1) {
1417+
if (npy_ma_static_data.optimize > 1) {
14181418
#else
14191419
if (Py_OptimizeFlag > 1) {
14201420
#endif
@@ -1858,15 +1858,15 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
18581858
/* for unity stride we can just copy out of the lookup table */
18591859
if (order == 'b') {
18601860
for (index = 0; index < in_n; index++) {
1861-
npy_uint64 v = npy_ma_static_data->unpack_lookup_big[*inptr].uint64;
1861+
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
18621862
memcpy(outptr, &v, 8);
18631863
outptr += 8;
18641864
inptr += in_stride;
18651865
}
18661866
}
18671867
else {
18681868
for (index = 0; index < in_n; index++) {
1869-
npy_uint64 v = npy_ma_static_data->unpack_lookup_big[*inptr].uint64;
1869+
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
18701870
if (order != 'b') {
18711871
v = npy_bswap8(v);
18721872
}
@@ -1877,7 +1877,7 @@ unpack_bits(PyObject *input, int axis, PyObject *count_obj, char order)
18771877
}
18781878
/* Clean up the tail portion */
18791879
if (in_tail) {
1880-
npy_uint64 v = npy_ma_static_data->unpack_lookup_big[*inptr].uint64;
1880+
npy_uint64 v = npy_ma_static_data.unpack_lookup_big[*inptr].uint64;
18811881
if (order != 'b') {
18821882
v = npy_bswap8(v);
18831883
}

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {
235235

236236
int int_copymode;
237237

238-
if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data->_CopyMode) {
238+
if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data._CopyMode) {
239239
PyObject* mode_value = PyObject_GetAttrString(obj, "value");
240240
if (mode_value == NULL) {
241241
return NPY_FAIL;
@@ -270,7 +270,7 @@ PyArray_AsTypeCopyConverter(PyObject *obj, NPY_ASTYPECOPYMODE *copymode)
270270
{
271271
int int_copymode;
272272

273-
if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data->_CopyMode) {
273+
if ((PyObject *)Py_TYPE(obj) == npy_ma_static_data._CopyMode) {
274274
PyErr_SetString(PyExc_ValueError,
275275
"_CopyMode enum is not allowed for astype function. "
276276
"Use true/false instead.");
@@ -1411,7 +1411,7 @@ PyArray_IntTupleFromIntp(int len, npy_intp const *vals)
14111411
NPY_NO_EXPORT int
14121412
_not_NoValue(PyObject *obj, PyObject **out)
14131413
{
1414-
if (obj == npy_ma_static_data->_NoValue) {
1414+
if (obj == npy_ma_static_data._NoValue) {
14151415
*out = NULL;
14161416
}
14171417
else {
@@ -1431,7 +1431,7 @@ PyArray_DeviceConverterOptional(PyObject *object, NPY_DEVICE *device)
14311431
}
14321432

14331433
if (PyUnicode_Check(object) &&
1434-
PyUnicode_Compare(object, npy_ma_str->cpu) == 0) {
1434+
PyUnicode_Compare(object, npy_ma_str.cpu) == 0) {
14351435
*device = NPY_DEVICE_CPU;
14361436
return NPY_SUCCEED;
14371437
}

0 commit comments

Comments
 (0)
0