-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PERF: datetimelike addition #56373
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
PERF: datetimelike addition #56373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
cimport cython | ||
from cpython.datetime cimport ( | ||
PyDateTime_CheckExact, | ||
PyDateTime_DATE_GET_HOUR, | ||
|
@@ -678,3 +679,43 @@ cdef int64_t _convert_reso_with_dtstruct( | |
raise OutOfBoundsDatetime from err | ||
|
||
return result | ||
|
||
|
||
@cython.overflowcheck(True) | ||
cpdef cnp.ndarray add_overflowsafe(cnp.ndarray left, cnp.ndarray right): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these possible to specialize to int64? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not without specifying an ndim. |
||
""" | ||
Overflow-safe addition for datetime64/timedelta64 dtypes. | ||
|
||
`right` may either be zero-dim or of the same shape as `left`. | ||
""" | ||
cdef: | ||
Py_ssize_t N = left.size | ||
int64_t lval, rval, res_value | ||
ndarray iresult = cnp.PyArray_EMPTY( | ||
left.ndim, left.shape, cnp.NPY_INT64, 0 | ||
) | ||
cnp.broadcast mi = cnp.PyArray_MultiIterNew3(iresult, left, right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty cool |
||
|
||
# Note: doing this try/except outside the loop improves performance over | ||
# doing it inside the loop. | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way I think there's another way of doing this without a loop that could also be more performant. The inspiration from this comes from a book called Hacker's Delight by Henry S Warren in section 4-2 The code would look something like:
The essence of the algorithm is that you use unsigned arithmetic (which has defined behavior) to do the summation, and then inspect the sign bit to see if "overflow" would have happened With this would need neither the cython.overflow check nor the try...except block There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's a quick demo
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat. Does this play nicely with the NPY_NAT check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good callout. I suppose you would just mask that as well. So something like: In [5]: left = np.array([2 ** 63 - 1, 2 ** 63 - 1, 2 ** 63 - 1, -(2 ** 63), -(2 ** 63), -(2 ** 63)])
...: right = np.array([42, 1, 0, -42, -1, 0])
...: is_nat = (left == 2 ** 63 - 1) | (right == 2 ** 63 - 1)
...: uleft = left.view("uint64")
...: uright = right.view("uint64")
...: summed = uleft + uright
...: neg_overflow = uleft & uright & ~summed
...: pos_overflow = ~uleft & ~uright & summed
...: overflows = ((neg_overflow | pos_overflow) >= 0x8000000000000000) & ~is_nat
...: overflows
Out[5]: array([False, False, False, True, True, False]) I just hardcoded the above to 2 ** 63 - 1 but can replace with macro or python variable, whichever is available There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does that actually out-perform? (maybe SIMD or something?) seems like a lot of additional memory allocations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard to say. In theory yes this would leverage SIMD better if numpy uses it under the hood (not well versed in if NumPy uses that or not). Also the overflow checks on gcc/clang are pretty fast because they use the compiler builtin, but I think MSVC falls back to a slow implementation (there is a builtin for MSVC too but I don't recall Cython using it - would have to double check) It requires more research so not something that needs to be done in this PR. Just food for future thought |
||
for i in range(N): | ||
# Analogous to: lval = lvalues[i] | ||
lval = (<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 1))[0] | ||
|
||
# Analogous to: rval = rvalues[i] | ||
rval = (<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 2))[0] | ||
|
||
if lval == NPY_DATETIME_NAT or rval == NPY_DATETIME_NAT: | ||
res_value = NPY_DATETIME_NAT | ||
else: | ||
res_value = lval + rval | ||
|
||
# Analogous to: result[i] = res_value | ||
(<int64_t*>cnp.PyArray_MultiIter_DATA(mi, 0))[0] = res_value | ||
|
||
cnp.PyArray_MultiIter_NEXT(mi) | ||
except OverflowError as err: | ||
raise OverflowError("Overflow in int64 addition") from err | ||
|
||
return iresult |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these considered superfluous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they use a no-longer-used mask argument. actually these are all pretty superfluous. will update to remove