FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-88473: Implement fast path in date.today() for date types by StanFromIreland · Pull Request #130980 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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
@@ -0,0 +1,3 @@
Implement a fast path for :class:`datetime.date` objects in :func:`datetime.date.today`
which results in a 5x performance gain while proper subclasses retain their
previous performance.
32 changes: 22 additions & 10 deletions Modules/_datetimemodule.c
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
Expand Up @@ -3291,19 +3291,31 @@ static PyObject *
datetime_date_today_impl(PyTypeObject *type)
/*[clinic end generated code: output=d5474697df6b251c input=21688afa289c0a06]*/
{
PyObject *time;
PyObject *result;
time = time_time();
if (time == NULL)
/* Use C implementation to boost performance for date type */
if (type == &PyDateTime_DateType) {
struct tm tm;
Comment thread
StanFromIreland marked this conversation as resolved.
time_t t;
time(&t);

if (_PyTime_localtime(t, &tm) != 0) {
return NULL;
Comment thread
StanFromIreland marked this conversation as resolved.
}

return new_date_ex(tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
type);
}

PyObject *time = time_time();
if (time == NULL) {
return NULL;
}

/* Note well: today() is a class method, so this may not call
* date.fromtimestamp. For example, it may call
* datetime.fromtimestamp. That's why we need all the accuracy
* time.time() delivers; if someone were gonzo about optimization,
* date.today() could get away with plain C time().
/* Note well: since today() is a class method, it may not call
* date.fromtimestamp, e.g., it may call datetime.fromtimestamp.
*/
result = PyObject_CallMethodOneArg((PyObject*)type, &_Py_ID(fromtimestamp), time);
PyObject *result = PyObject_CallMethodOneArg((PyObject*)type, &_Py_ID(fromtimestamp), time);
Py_DECREF(time);
return result;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL