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

PERF: datetimelike addition #56373

Merged
merged 5 commits into from
Dec 9, 2023
Merged

Conversation

jbrockmendel
Copy link
Member

In [1]: import pandas as pd

In [2]: dti = pd.date_range("2016-01-01", periods=10_000)

In [3]: td = pd.Timedelta(days=1)

In [4]: %timeit dti + td
108 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)  # <- main
94.3 µs ± 3.27 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)  # <- PR

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Interesting idea!



@cython.overflowcheck(True)
cpdef cnp.ndarray add_overflowsafe(cnp.ndarray left, cnp.ndarray right):
Copy link
Member

Choose a reason for hiding this comment

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

Are these possible to specialize to int64?

Copy link
Member Author

Choose a reason for hiding this comment

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

not without specifying an ndim.

ndarray iresult = cnp.PyArray_EMPTY(
left.ndim, left.shape, cnp.NPY_INT64, 0
)
cnp.broadcast mi = cnp.PyArray_MultiIterNew3(iresult, left, right)
Copy link
Member

Choose a reason for hiding this comment

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

Pretty cool

@@ -413,17 +413,6 @@ def setup(self):
def time_add_overflow_arr_rev(self):
checked_add_with_arr(self.arr, self.arr_rev)

def time_add_overflow_arr_mask_nan(self):
Copy link
Member

Choose a reason for hiding this comment

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

Are these considered superfluous?

Copy link
Member Author

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


# Note: doing this try/except outside the loop improves performance over
# doing it inside the loop.
try:
Copy link
Member

@WillAyd WillAyd Dec 7, 2023

Choose a reason for hiding this comment

The 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:

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Here's a quick demo

In [42]: 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])
    ...: 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
    ...: overflows
Out[42]: array([ True,  True, False,  True,  True, False])

Copy link
Member Author

Choose a reason for hiding this comment

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

Neat. Does this play nicely with the NPY_NAT check?

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member

@WillAyd WillAyd Dec 7, 2023

Choose a reason for hiding this comment

The 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

@mroeschke mroeschke added Performance Memory or execution speed performance Numeric Operations Arithmetic, Comparison, and Logical operations labels Dec 7, 2023
@mroeschke mroeschke added this to the 2.2 milestone Dec 9, 2023
@mroeschke mroeschke merged commit a6c0ae4 into pandas-dev:main Dec 9, 2023
44 checks passed
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the ref-checked_add branch December 9, 2023 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants