8000 ENH: use caching memory allocator in more places · numpy/numpy@5a08e20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a08e20

Browse files
committed
ENH: use caching memory allocator in more places
In particular use it in PyArray_IntpConverter which is used in many function entrypoints.
1 parent 8294859 commit 5a08e20

14 files changed

+103
-84
lines changed

numpy/core/src/multiarray/alloc.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ npy_alloc_cache_dim(npy_uintp sz);
2121
NPY_NO_EXPORT void
2222
npy_free_cache_dim(void * p, npy_uintp sd);
2323

24+
static NPY_INLINE void
25+
npy_free_cache_dim_obj(PyArray_Dims dims)
26+
{
27+
npy_free_cache_dim(dims.ptr, dims.len);
28+
}
29+
30+
st 8000 atic NPY_INLINE void
31+
npy_free_cache_dim_array(PyArrayObject * arr)
32+
{
33+
npy_free_cache_dim(PyArray_DIMS(arr), PyArray_NDIM(arr));
34+
}
35+
2436
#endif

numpy/core/src/multiarray/arrayobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,14 +1518,14 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
15181518
}
15191519
}
15201520

1521-
PyDimMem_FREE(dims.ptr);
1522-
PyDimMem_FREE(strides.ptr);
1521+
npy_free_cache_dim_obj(dims);
1522+
npy_free_cache_dim_obj(strides);
15231523
return (PyObject *)ret;
15241524

15251525
fail:
15261526
Py_XDECREF(descr);
1527-
PyDimMem_FREE(dims.ptr);
1528-
PyDimMem_FREE(strides.ptr);
1527+
npy_free_cache_dim_obj(dims);
1528+
npy_free_cache_dim_obj(strides);
15291529
return NULL;
15301530
}
15311531

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ VOID_getitem(void *input, void *vap)
700700
PyArrayObject *ret;
701701

702702
if (!(PyArray_IntpConverter(descr->subarray->shape, &shape))) {
703-
PyDimMem_FREE(shape.ptr);
703+
npy_free_cache_dim_obj(shape);
704704
PyErr_SetString(PyExc_ValueError,
705705
"invalid shape in fixed-type tuple.");
706706
return NULL;
@@ -709,7 +709,7 @@ VOID_getitem(void *input, void *vap)
709709
ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type,
710710
descr->subarray->base, shape.len, shape.ptr,
711711
NULL, ip, PyArray_FLAGS(ap)&(~NPY_ARRAY_F_CONTIGUOUS), NULL);
712-
PyDimMem_FREE(shape.ptr);
712+
npy_free_cache_dim_obj(shape);
713713
if (!ret) {
714714
return NULL;
715715
}
@@ -838,7 +838,7 @@ VOID_setitem(PyObject *op, void *input, void *vap)
838838
PyArray_Dims shape = {NULL, -1};
839839
PyArrayObject *ret;
840840
if (!(PyArray_IntpConverter(descr->subarray->shape, &shape))) {
841-
PyDimMem_FREE(shape.ptr);
841+
npy_free_cache_dim_obj(shape);
842842
PyErr_SetString(PyExc_ValueError,
843843
"invalid shape in fixed-type tuple.");
844844
return -1;
@@ -847,7 +847,7 @@ VOID_setitem(PyObject *op, void *input, void *vap)
847847
ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type,
848848
descr->subarray->base, shape.len, shape.ptr,
849849
NULL, ip, PyArray_FLAGS(ap), NULL);
850-
PyDimMem_FREE(shape.ptr);
850+
npy_free_cache_dim_obj(shape);
851851
if (!ret) {
852852
return -1;
853853
}

numpy/core/src/multiarray/compiled_base.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "npy_config.h"
1111
#include "templ_common.h" /* for npy_mul_with_overflow_intp */
1212
#include "lowlevel_strided_loops.h" /* for npy_bswap8 */
13+
#include "alloc.h"
1314

1415

1516
/*
@@ -1091,7 +1092,7 @@ arr_ravel_multi_index(PyObject *self, PyObject *args, PyObject *kwds)
10911092
for (i = 0; i < dimensions.len; ++i) {
10921093
Py_XDECREF(op[i]);
10931094
}
1094-
PyDimMem_FREE(dimensions.ptr);
1095+
npy_free_cache_dim_obj(dimensions);
10951096
NpyIter_Deallocate(iter);
10961097
return PyArray_Return(ret);
10971098

@@ -1100,7 +1101,7 @@ arr_ravel_multi_index(PyObject *self, PyObject *args, PyObject *kwds)
11001101
for (i = 0; i < dimensions.len; ++i) {
11011102
Py_XDECREF(op[i]);
11021103
}
1103-
PyDimMem_FREE(dimensions.ptr);
1104+
npy_free_cache_dim_obj(dimensions);
11041105
NpyIter_Deallocate(iter);
11051106
return NULL;
11061107
}
@@ -1352,7 +1353,7 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
13521353

13531354
Py_DECREF(ret_arr);
13541355
Py_XDECREF(indices);
1355-
PyDimMem_FREE(dimensions.ptr);
1356+
npy_free_cache_dim_obj(dimensions);
13561357
NpyIter_Deallocate(iter);
13571358

13581359
return ret_tuple;
@@ -1362,7 +1363,7 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds)
13621363
Py_XDECREF(ret_arr);
13631364
Py_XDECREF(dtype);
13641365
Py_XDECREF(indices);
1365-
PyDimMem_FREE(dimensions.ptr);
1366+
npy_free_cache_dim_obj(dimensions);
13661367
NpyIter_Deallocate(iter);
13671368
return NULL;
13681369
}

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "arraytypes.h"
1616

1717
#include "conversion_utils.h"
18+
#include "alloc.h"
1819

1920
static int
2021
PyArray_PyIntAsInt_ErrMsg(PyObject *o, const char * msg) NPY_GCC_NONNULL(2);
@@ -119,7 +120,7 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq)
119120
return NPY_FAIL;
120121
}
121122
if (len > 0) {
122-
seq->ptr = PyDimMem_NEW(len);
123+
seq->ptr = npy_alloc_cache_dim(len);
123124
if (seq->ptr == NULL) {
124125
PyErr_NoMemory();
125126
return NPY_FAIL;
@@ -128,7 +129,7 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq)
128129
seq->len = len;
129130
nd = PyArray_IntpFromIndexSequence(obj, (npy_intp *)seq->ptr, len);
130131
if (nd == -1 || nd != len) {
131-
PyDimMem_FREE(seq->ptr);
132+
npy_free_cache_dim_obj(*seq);
132133
seq->ptr = NULL;
133134
return NPY_FAIL;
134135
}

numpy/core/src/multiarray/descriptor.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "_datetime.h"
1717
#include "common.h"
1818
#include "descriptor.h"
19+
#include "alloc.h"
1920

2021
/*
2122
* offset: A starting offset.
@@ -306,7 +307,7 @@ _convert_from_tuple(PyObject *obj)
306307
int i;
307308

308309
if (!(PyArray_IntpConverter(val, &shape)) || (shape.len > NPY_MAXDIMS)) {
309-
PyDimMem_FREE(shape.ptr);
310+
npy_free_cache_dim_obj(shape);
310311
PyErr_SetString(PyExc_ValueError,
311312
"invalid shape in fixed-type tuple.");
312313
goto fail;
@@ -320,12 +321,12 @@ _convert_from_tuple(PyObject *obj)
320321
&& PyNumber_Check(val))
321322
|| (shape.len == 0
322323
&& PyTuple_Check(val))) {
323-
PyDimMem_FREE(shape.ptr);
324+
npy_free_cache_dim_obj(shape);
324325
return type;
325326
}
326327
newdescr = PyArray_DescrNewFromType(NPY_VOID);
327328
if (newdescr == NULL) {
328-
PyDimMem_FREE(shape.ptr);
329+
npy_free_cache_dim_obj(shape);
329330
goto fail;
330331
}
331332

@@ -335,14 +336,14 @@ _convert_from_tuple(PyObject *obj)
335336
PyErr_SetString(PyExc_ValueError,
336337
"invalid shape in fixed-type tuple: "
337338
"dimension smaller then zero.");
338-
PyDimMem_FREE(shape.ptr);
339+
npy_free_cache_dim_obj(shape);
339340
goto fail;
340341
}
341342
if (shape.ptr[i] > NPY_MAX_INT) {
342343
PyErr_SetString(PyExc_ValueError,
343344
"invalid shape in fixed-type tuple: "
344345
"dimension does not fit into a C int.");
345-
PyDimMem_FREE(shape.ptr);
346+
npy_free_cache_dim_obj(shape);
346347
goto fail;
347348
}
348349
}
@@ -351,12 +352,12 @@ _convert_from_tuple(PyObject *obj)
351352
PyErr_SetString(PyExc_ValueError,
352353
"invalid shape in fixed-type tuple: dtype size in "
353354
"bytes must fit into a C int.");
354-
PyDimMem_FREE(shape.ptr);
355+
npy_free_cache_dim_obj(shape);
355356
goto fail;
356357
}
357358
newdescr->elsize = type->elsize * items;
358359
if (newdescr->elsize == -1) {
359-
PyDimMem_FREE(shape.ptr);
360+
npy_free_cache_dim_obj(shape);
360361
goto fail;
361362
}
362363

@@ -381,7 +382,7 @@ _convert_from_tuple(PyObject *obj)
381382
*/
382383
newdescr->subarray->shape = PyTuple_New(shape.len);
383384
if (newdescr->subarray->shape == NULL) {
384-
PyDimMem_FREE(shape.ptr);
385+
npy_free_cache_dim_obj(shape);
385386
goto fail;
386387
}
387388
for (i=0; i < shape.len; i++) {
@@ -391,12 +392,12 @@ _convert_from_tuple(PyObject *obj)
391392
if (PyTuple_GET_ITEM(newdescr->subarray->shape, i) == NULL) {
392393
Py_DECREF(newdescr->subarray->shape);
393394
newdescr->subarray->shape = NULL;
394-
PyDimMem_FREE(shape.ptr);
395+
npy_free_cache_dim_obj(shape);
395396
goto fail;
396397
}
397398
}
398399

399-
PyDimMem_FREE(shape.ptr);
400+
npy_free_cache_dim_obj(shape);
400401
type = newdescr;
401402
}
402403
return type;

numpy/core/src/multiarray/dtype_transfer.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "shape.h"
3030
#include "lowlevel_strided_loops.h"
31+
#include "alloc.h"
3132

3233
#define NPY_LOWLEVEL_BUFFER_BLOCKSIZE 128
3334

@@ -2342,7 +2343,7 @@ get_subarray_transfer_function(int aligned,
23422343
if (PyDataType_HASSUBARRAY(dst_dtype)) {
23432344
if (!(PyArray_IntpConverter(dst_dtype->subarray->shape,
23442345
&dst_shape))) {
2345-
PyDimMem_FREE(src_shape.ptr);
2346+
npy_free_cache_dim_obj(src_shape);
23462347
PyErr_SetString(PyExc_ValueError,
23472348
"invalid subarray shape");
23482349
return NPY_FAIL;
@@ -2355,8 +2356,8 @@ get_subarray_transfer_function(int aligned,
23552356
* Just a straight one-element copy.
23562357
*/
23572358
if (dst_size == 1 && src_size == 1) {
2358-
PyDimMem_FREE(src_shape.ptr);
2359-
PyDimMem_FREE(dst_shape.ptr);
2359+
npy_free_cache_dim_obj(src_shape);
2360+
npy_free_cache_dim_obj(dst_shape);
23602361

23612362
return PyArray_GetDTypeTransferFunction(aligned,
23622363
src_stride, dst_stride,
@@ -2367,8 +2368,8 @@ get_subarray_transfer_function(int aligned,
23672368
}
23682369
/* Copy the src value to all the dst values */
23692370
else if (src_size == 1) {
2370-
PyDimMem_FREE(src_shape.ptr);
2371-
PyDimMem_FREE(dst_shape.ptr);
2371+
npy_free_cache_dim_obj(src_shape);
2372+
npy_free_cache_dim_obj(dst_shape);
23722373

23732374
return get_one_to_n_transfer_function(aligned,
23742375
src_stride, dst_stride,
@@ -2382,8 +2383,8 @@ get_subarray_transfer_function(int aligned,
23822383
else if (src_shape.len == dst_shape.len &&
23832384
PyArray_CompareLists(src_shape.ptr, dst_shape.ptr,
23842385
src_shape.len)) {
2385-
PyDimMem_FREE(src_shape.ptr);
2386-
PyDimMem_FREE(dst_shape.ptr);
2386+
npy_free_cache_dim_obj(src_shape);
2387+
npy_free_cache_dim_obj(dst_shape);
23872388

23882389
return get_n_to_n_transfer_function(aligned,
23892390
src_stride, dst_stride,
@@ -2407,8 +2408,8 @@ get_subarray_transfer_function(int aligned,
24072408
out_stransfer, out_transferdata,
24082409
out_needs_api);
24092410

2410-
PyDimMem_FREE(src_shape.ptr);
2411-
PyDimMem_FREE(dst_shape.ptr);
2411+
npy_free_cache_dim_obj(src_shape);
2412+
npy_free_cache_dim_obj(dst_shape);
24122413
return ret;
24132414
}
24142415
}
@@ -3371,7 +3372,7 @@ get_setdstzero_transfer_function(int aligned,
33713372
return NPY_FAIL;
33723373
}
33733374
dst_size = PyArray_MultiplyList(dst_shape.ptr, dst_shape.len);
3374-
PyDimMem_FREE(dst_shape.ptr);
3375+
npy_free_cache_dim_obj(dst_shape);
33753376

33763377
/* Get a function for contiguous dst of the subarray type */
33773378
if (get_setdstzero_transfer_function(aligned,
@@ -3484,7 +3485,7 @@ get_decsrcref_transfer_function(int aligned,
34843485
return NPY_FAIL;
34853486
}
34863487
src_size = PyArray_MultiplyList(src_shape.ptr, src_shape.len);
3487-
PyDimMem_FREE(src_shape.ptr);
3488+
npy_free_cache_dim_obj(src_shape);
34883489

34893490
/* Get a function for contiguous src of the subarray type */
34903491
if (get_decsrcref_transfer_function(aligned,

numpy/core/src/multiarray/getset.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "getset.h"
1919
#include "arrayobject.h"
2020
#include "mem_overlap.h"
21+
#include "alloc.h"
2122

2223
/******************* array attribute get and set routines ******************/
2324

@@ -65,12 +66,12 @@ array_shape_set(PyArrayObject *self, PyObject *val)
6566
}
6667

6768
/* Free old dimensions and strides */
68-
PyDimMem_FREE(PyArray_DIMS(self));
69+
npy_free_cache_dim_array(self);
6970
nd = PyArray_NDIM(ret);
7071
((PyArrayObject_fields *)self)->nd = nd;
7172
if (nd > 0) {
7273
/* create new dimensions and strides */
73-
((PyArrayObject_fields *)self)->dimensions = PyDimMem_NEW(3*nd);
74+
((PyArrayObject_fields *)self)->dimensions = npy_alloc_cache_dim(3*nd);
7475
if (PyArray_DIMS(self) == NULL) {
7576
Py_DECREF(ret);
7677
PyErr_SetString(PyExc_MemoryError,"");
@@ -158,11 +159,11 @@ array_strides_set(PyArrayObject *self, PyObject *obj)
158159
memcpy(PyArray_STRIDES(self), newstrides.ptr, sizeof(npy_intp)*newstrides.len);
159160
PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS |
160161
NPY_ARRAY_ALIGNED);
161-
PyDimMem_FREE(newstrides.ptr);
162+
npy_free_cache_dim_obj(newstrides);
162163
return 0;
163164

164165
fail:
165-
PyDimMem_FREE(newstrides.ptr);
166+
npy_free_cache_dim_obj(newstrides);
166167
return -1;
167168
}
168169

@@ -560,7 +561,7 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
560561
if (temp == NULL) {
561562
return -1;
562563
}
563-
PyDimMem_FREE(PyArray_DIMS(self));
564+
npy_free_cache_dim_array(self);
564565
((PyArrayObject_fields *)self)->dimensions = PyArray_DIMS(temp);
565566
((PyArrayObject_fields *)self)->nd = PyArray_NDIM(temp);
566567
((PyArrayObject_fields *)self)->strides = PyArray_STRIDES(temp);

0 commit comments

Comments
 (0)
0