8000 Merge pull request #7820 from charris/fix-empty-array-allocation-size · numpy/numpy@08fc49e · GitHub
[go: up one dir, main page]

Skip to content

Commit 08fc49e

Browse files
authored
Merge pull request #7820 from charris/fix-empty-array-allocation-size
MAINT: Allocate fewer bytes for empty arrays.
2 parents 6685ee8 + 3e39614 commit 08fc49e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
902902
int allow_emptystring)
903903
{
904904
PyArrayObject_fields *fa;
905-
int i;
905+
int i, is_empty;
906906
npy_intp nbytes;
907907

908908
if (descr->subarray) {
@@ -953,6 +953,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
953953
}
954954

955955
/* Check dimensions and multiply them to nbytes */
956+
is_empty = 0;
956957
for (i = 0; i < nd; i++) {
957958
npy_intp dim = dims[i];
958959

@@ -961,13 +962,13 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
961962
* Compare to PyArray_OverflowMultiplyList that
962963
* returns 0 in this case.
963964
*/
965+
is_empty = 1;
964966
continue;
965967
}
966968

967969
if (dim < 0) {
968970
PyErr_SetString(PyExc_ValueError,
969-
"negative dimensions " \
970-
"are not allowed");
971+
"negative dimensions are not allowed");
971972
Py_DECREF(descr);
972973
return NULL;
973974
}
@@ -1041,8 +1042,9 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
10411042
* Allocate something even for zero-space arrays
10421043
* e.g. shape=(0,) -- otherwise buffer exposure
10431044
* (a.data) doesn't work as it should.
1045+
* Could probably just allocate a few bytes here. -- Chuck
10441046
*/
1045-
if (nbytes == 0) {
1047+
if (is_empty) {
10461048
nbytes = descr->elsize;
10471049
}
10481050
/*

0 commit comments

Comments
 (0)
0