8000 BUG: move reduction initialization to ufunc initialization by ngoldbaum · Pull Request #28123 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: move reduction initialization to ufunc initialization #28123

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 6 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
  • Loading branch information
ngoldbaum and seberg authored Jan 8, 2025
commit d955688e1fb551e41caeada385554d28f7b672bf
15 changes: 2 additions & 13 deletions numpy/_core/src/umath/legacy_array_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,6 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc,
// set cached initial value for numeric reductions to avoid creating
// a python int in every reduction
if (PyTypeNum_ISNUMBER(bound_res->dtypes[0]->type_num)) {
char *initial =
PyMem_Calloc(1, bound_res->dtypes[0]->singleton->elsize);

if (initial == NULL) {
PyErr_NoMemory();
return NULL;
}

PyArray_Descr **descrs = PyMem_Calloc(ufunc->nin + ufunc->nout,
sizeof(PyArray_Descr *));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the places where the NPY_ALLOC_WORSPACE could also be used, but I don't much, this isn't performance critical.
(And it might be that at least with mimalloc all of this barely matters anyway.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But more importantly. We only ever use the initial value for reductions. So I think you might as well hard-code all of this (if you like up to the knowledge that get_initial_form_ufunc() only usees descrs[0] anyway).

So make descrs[3] or just &singleton... (of course only enter the branch if nin == 2 and nout == 1)


Expand All @@ -459,22 +451,19 @@ PyArray_NewLegacyWrappingArrayMethod(PyUFuncObject *ufunc,
descrs,
};

int ret = get_initial_from_ufunc(&context, 0, initial);
int ret = get_initial_from_ufunc(&context, 0, context.method->legacy_initial);

if (ret < 0) {
PyMem_Free(initial);
PyMem_Free(descrs);
return NULL;
}

// only use the initial value if it's valid
// only use the cached initial value if it's valid
if (ret > 0) {
memcpy(context.method->legacy_initial, initial,
context.descriptors[0]->elsize);
context.method->get_reduction_initial = &copy_cached_initial;
}

PyMem_Free(initial);
PyMem_Free(descrs);
}

Expand Down
Loading
0