Skip to content

Commit 557a060

Browse files
Implement fast path
1 parent 72e5b25 commit 557a060

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement a fast path for date types in :func:`date.today()`

Modules/_datetimemodule.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,21 +3259,36 @@ date_fromtimestamp(PyObject *cls, PyObject *obj)
32593259
static PyObject *
32603260
date_today(PyObject *cls, PyObject *Py_UNUSED(dummy))
32613261
{
3262-
PyObject *time;
3263-
PyObject *result;
3264-
time = time_time();
3265-
if (time == NULL)
3266-
return NULL;
3262+
/* Use C implementation to boost performance for date type */
3263+
if ((PyTypeObject *)cls == &PyDateTime_DateType) {
3264+
struct tm tm;
3265+
time_t t;
3266+
time(&t);
32673267

3268-
/* Note well: today() is a class method, so this may not call
3269-
* date.fromtimestamp. For example, it may call
3270-
* datetime.fromtimestamp. That's why we need all the accuracy
3271-
* time.time() delivers; if someone were gonzo about optimization,
3272-
* date.today() could get away with plain C time().
3273-
*/
3274-
result = PyObject_CallMethodOneArg(cls, &_Py_ID(fromtimestamp), time);
3275-
Py_DECREF(time);
3276-
return result;
3268+
if (_PyTime_localtime(t, &tm) != 0)
3269+
return NULL;
3270+
3271+
return new_date_ex(tm.tm_year + 1900,
3272+
tm.tm_mon + 1,
3273+
tm.tm_mday,
3274+
(PyTypeObject*)cls);
3275+
} else {
3276+
PyObject *time;
3277+
PyObject *result;
3278+
time = time_time();
3279+
if (time == NULL)
3280+
return NULL;
3281+
3282+
/* Note well: today() is a class method, so this may not call
3283+
* date.fromtimestamp. For example, it may call
3284+
* datetime.fromtimestamp. That's why we need all the accuracy
3285+
* time.time() delivers; if someone were gonzo about optimization,
3286+
* date.today() could get away with plain C time().
3287+
*/
3288+
result = PyObject_CallMethodOneArg(cls, &_Py_ID(fromtimestamp), time);
3289+
Py_DECREF(time);
3290+
return result;
3291+
}
32773292
}
32783293

32793294
/*[clinic input]

0 commit comments

Comments
 (0)