8000 PERF: add a fast path to ufunc type resolution by ngoldbaum · Pull Request #27859 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

PERF: add a fast path to ufunc type resolution #27859

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

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 30 additions & 4 deletions numpy/_core/src/umath/dispatching.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,35 @@ promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
return info;
}

/*
* Fast path for promote_and_get_info_and_ufuncimpl.
* Enters a critical section if there is a cache miss.
*/
static inline PyObject *
try_promote_and_get_info_and_ufuncimpl(PyUFuncObject *ufunc,
PyArrayObject *const ops[],
PyArray_DTypeMeta *signature[],
PyArray_DTypeMeta *op_dtypes[],
npy_bool legacy_promotion_is_possible)
{
PyObject *info = PyArrayIdentityHash_GetItem(ufunc->_dispatch_cache,

Choose a reason for hiding this comment

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

I don't think this is thread-safe because we may read from the hash table while another thread is concurrently modifying it, including possibly resizing the table.

Copy link
Member Author

Choose a reason for hiding this comment

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

😓 That's a good point.

Is there a thread-safe way to do it without an RW lock?

Choose a reason for hiding this comment

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

We could make PyArrayIdentityHash a concurrent hash table. That's generally hard, but there are a few things that make this specific case easier:

  • We only care about concurrent read performance
  • Entries are never deleted or overwritten

But we'd still have to deal with hash table resizing, which makes it more complex.

I think a RW lock will be simpler as long as it provides good enough read performance.

(PyObject **)op_dtypes);

if (info != NULL && PyObject_TypeCheck(
PyTuple_GET_ITEM(info, 1), &PyArrayMethod_Type)) {
/* Found the ArrayMethod and NOT a promoter: return it */
return info;
}

Py_BEGIN_CRITICAL_SECTION((PyObject *)ufunc);
info = promote_and_get_info_and_ufuncimpl(ufunc,
ops, signature, op_dtypes, legacy_promotion_is_possible);
Py_END_CRITICAL_SECTION();


return info;
}


/**
* The central entry-point for the promotion and dispatching machinery.
Expand Down Expand Up @@ -976,11 +1005,8 @@ promote_and_get_ufuncimpl(PyUFuncObject *ufunc,
}
}

PyObject *info;
Py_BEGIN_CRITICAL_SECTION((PyObject *)ufunc);
info = promote_and_get_info_and_ufuncimpl(ufunc,
PyObject *info = try_promote_and_get_info_and_ufuncimpl(ufunc,
ops, signature, op_dtypes, legacy_promotion_is_possible);
Py_END_CRITICAL_SECTION();

if (info == NULL) {
goto handle_error;
Expand Down
Loading
0