-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
bpo-32403: Faster date and datetime constructors #4993
Conversation
Modules/_datetimemodule.c
Outdated
// We have "fast path" constructors for two subclasses: date and datetime | ||
if ( (PyTypeObject *)cls == & PyDateTime_DateType ) { | ||
result = new_date_ex(year, month, day, (PyTypeObject *)cls); | ||
} else if ((PyTypeObject *)cls == & PyDateTime_DateTimeType ) { |
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.
This is the part I'm not sure about. It feels weird to put special-case logic for a specific subclass in the base class logic, but it makes the methods that datetime
inherits and doesn't re-implement itself (datetime.today
and datetime.toordinal
) considerably faster when I do this.
Another option might be to do a token implementation of all the date
methods specifically calling the new_datetime_subclass_ex
, or explicitly calling date.themethod
and then using datetime.combine
on the result.
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.
I don't see a problem here.
Modules/_datetimemodule.c
Outdated
new_date_subclass_ex(int year, int month, int day, PyObject *cls) { | ||
PyObject *result; | ||
// We have "fast path" constructors for two subclasses: date and datetime | ||
if ( (PyTypeObject *)cls == & PyDateTime_DateType ) { |
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.
Please try to follow the code style used in the rest of the file. We don't leave space between &
and the variable or inside the parentheses.
Modules/_datetimemodule.c
Outdated
// We have "fast path" constructors for two subclasses: date and datetime | ||
if ( (PyTypeObject *)cls == & PyDateTime_DateType ) { | ||
result = new_date_ex(year, month, day, (PyTypeObject *)cls); | ||
} else if ((PyTypeObject *)cls == & PyDateTime_DateTimeType ) { |
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.
I don't see a problem here.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
6c6115c
to
92ac0c4
Compare
92ac0c4
to
b438cbe
Compare
@abalkin I believe I fixed the style issues. |
This fixes bpo-32403 and bpo-32404. The first one is a performance enhancement, the second is a bugfix.
Using this script to benchmark the alternate constructors, I see significant improvements in speed across the board (note that since the subclasses don't take the fast path, they are a reasonable proxy for the speed from before this patch). You can see the results on my laptop (using the debug build, not the profiling optimized build) below:
For comparision, here is the same script run against master:
https://bugs.python.org/issue32403