-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: (partial) fix for np_datetime.c performance regression #57988
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
dontgoto
wants to merge
24
commits into
pandas-dev:main
from
dontgoto:240324_57951_potential_regression_induced_by_re
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
aecba4b
faster ifrac
dontgoto 6f43de0
remove unnecessary statement
dontgoto 47a0d9b
shorten <s assignment
dontgoto 61c5cae
remove assignments
dontgoto e4c64b2
shorten frac assignments
dontgoto fa3ff63
shorten days logic
dontgoto 9d768d6
remove unnecessary assignments
dontgoto c9f75b1
reuse td
dontgoto aba6c3a
fix regression for td close to int64 bounds
dontgoto e7c02e8
re-add unnecessary conversions
dontgoto 6b464a5
Merge branch 'main' into 240324_57951_potential_regression_induced_by_re
dontgoto b3c0199
remove sign if
dontgoto 5a04d8d
remove mod seconds if layer
dontgoto 39c77ab
remove second negative check if layer
dontgoto 2227b1b
duplicate larger than day check
dontgoto 0171a7c
remove forgetten functions
dontgoto 1492bc9
make days assignment conditions explicit
dontgoto 2e670c0
directly flip out.days
dontgoto 0316442
extract normalization to secs
dontgoto b61b71c
reintroduce sfrac, minimize casting
dontgoto c3b19e0
add comment
dontgoto 80ce283
fix error
dontgoto c9a1efd
Merge remote-tracking branch 'origin/240324_57951_potential_regressio…
dontgoto 322252e
remove uneven_in_seconds
dontgoto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -752,66 +752,57 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta td, | |
} | ||
|
||
const npy_int64 per_day = sec_per_day * per_sec; | ||
npy_int64 frac; | ||
const int sign = td < 0 ? -1 : 1; | ||
// put frac in seconds | ||
if (td < 0 && td % per_sec != 0) | ||
frac = td / per_sec - 1; | ||
else | ||
frac = td / per_sec; | ||
npy_int64 sfrac = td / per_sec; | ||
if (sign < 0) { | ||
if (td % per_sec != 0) | ||
sfrac -= 1; | ||
|
||
const int sign = frac < 0 ? -1 : 1; | ||
if (frac < 0) { | ||
// even fraction | ||
if ((-frac % sec_per_day) != 0) { | ||
out->days = -frac / sec_per_day + 1; | ||
frac += sec_per_day * out->days; | ||
if ((-sfrac % sec_per_day) != 0) { | ||
out->days = sfrac / sec_per_day - 1; | ||
sfrac -= sec_per_day * out->days; | ||
} else { | ||
frac = -frac; | ||
if (sfrac <= sec_per_day) { | ||
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. This seems to add more branching than what we had before, so I'm a little hesitant to say this is faster overall even though it may show up in some of our benchmarks. What kind of difference are you seeing in the current state? |
||
out->days = sfrac / sec_per_day; | ||
sfrac -= out->days * sec_per_day; | ||
} | ||
sfrac = -sfrac; | ||
} | ||
} else if (sfrac >= sec_per_day) { | ||
out->days = sfrac / sec_per_day; | ||
sfrac -= out->days * sec_per_day; | ||
} | ||
|
||
if (frac >= sec_per_day) { | ||
out->days += frac / sec_per_day; | ||
frac -= out->days * sec_per_day; | ||
if (sfrac >= sec_per_hour) { | ||
out->hrs = (npy_int32)(sfrac / sec_per_hour); | ||
sfrac %= sec_per_hour; | ||
} | ||
|
||
if (frac >= sec_per_hour) { | ||
out->hrs = (npy_int32)(frac / sec_per_hour); | ||
frac -= out->hrs * sec_per_hour; | ||
if (sfrac >= sec_per_min) { | ||
out->min = (npy_int32)(sfrac / sec_per_min); | ||
sfrac %= sec_per_min; | ||
} | ||
|
||
if (frac >= sec_per_min) { | ||
out->min = (npy_int32)(frac / sec_per_min); | ||
frac -= out->min * sec_per_min; | ||
if (sfrac >= 0) { | ||
out->sec = (npy_int32)sfrac; | ||
} | ||
|
||
if (frac >= 0) { | ||
out->sec = (npy_int32)frac; | ||
frac -= out->sec; | ||
} | ||
|
||
if (sign < 0) | ||
out->days = -out->days; | ||
|
||
if (base > NPY_FR_s) { | ||
const npy_int64 sfrac = | ||
(out->hrs * sec_per_hour + out->min * sec_per_min + out->sec) * | ||
per_sec; | ||
|
||
npy_int64 ifrac = td - (out->days * per_day + sfrac); | ||
// there will be at most 1 billion nanoseconds left here | ||
npy_int32 ifrac = (npy_int32)((td - out->days * per_day) % per_sec); | ||
|
||
if (base == NPY_FR_ms) { | ||
out->ms = (npy_int32)ifrac; | ||
out->ms = ifrac; | ||
} else if (base == NPY_FR_us) { | ||
out->ms = (npy_int32)(ifrac / 1000LL); | ||
ifrac = ifrac % 1000LL; | ||
out->us = (npy_int32)ifrac; | ||
out->ms = ifrac / 1000LL; | ||
out->us = ifrac % 1000LL; | ||
} else if (base == NPY_FR_ns) { | ||
out->ms = (npy_int32)(ifrac / (1000LL * 1000LL)); | ||
out->ms = ifrac / (1000LL * 1000LL); | ||
ifrac = ifrac % (1000LL * 1000LL); | ||
out->us = (npy_int32)(ifrac / 1000LL); | ||
ifrac = ifrac % 1000LL; | ||
out->ns = (npy_int32)ifrac; | ||
out->us = ifrac / 1000LL; | ||
out->ns = ifrac % 1000LL; | ||
} | ||
} | ||
|
||
|
@@ -822,7 +813,6 @@ void pandas_timedelta_to_timedeltastruct(npy_timedelta td, | |
"invalid base unit"); | ||
break; | ||
} | ||
|
||
out->seconds = | ||
(npy_int32)(out->hrs * sec_per_hour + out->min * sec_per_min + out->sec); | ||
out->microseconds = out->ms * 1000 + out->us; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this exactly the same as before? Previously it looks like out->days would be positive but this is now negative?
Sorry if misreading - again just want to be careful as I'm not sure how well our test cases are hitting all of these branches