Skip to content
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

MAINT: Allocate fewer bytes for empty arrays. #7820

Merged
merged 1 commit into from
Jul 11, 2016
Merged
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
int allow_emptystring)
{
PyArrayObject_fields *fa;
int i;
int i, is_empty;
npy_intp nbytes;

if (descr->subarray) {
Expand Down Expand Up @@ -953,6 +953,7 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
}

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

Expand All @@ -961,13 +962,13 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
* Compare to PyArray_OverflowMultiplyList that
* returns 0 in this case.
*/
is_empty = 1;
continue;
}

if (dim < 0) {
PyErr_SetString(PyExc_ValueError,
"negative dimensions " \
"are not allowed");
"negative dimensions are not allowed");
Py_DECREF(descr);
return NULL;
}
Expand Down Expand Up @@ -1041,8 +1042,9 @@ PyArray_NewFromDescr_int(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
* Allocate something even for zero-space arrays
* e.g. shape=(0,) -- otherwise buffer exposure
* (a.data) doesn't work as it should.
* Could probably just allocate a few bytes here. -- Chuck
*/
if (nbytes == 0) {
if (is_empty) {
nbytes = descr->elsize;
}
/*
Expand Down