Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-148085: datetime cache time module lookups#148088
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
36c494b48e9ea1ae32bfd7e7b739cd247ca2f3dbe0d3944fe33c64f2ff27dc4056cf08e00aa62170a3f07751fc6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Cache ``time.struct_time``, and ``time.strftime`` imports, speeding up | ||
| :meth:`~datetime.date.timetuple` and :meth:`~datetime.date.strftime` by up to | ||
| 2x. As such, patching :mod:`time` functions after importing :mod:`datetime` | ||
| will no longer affect :mod:`datetime` behaviour. Patch by Maurycy | ||
| Pawłowski-Wieroński. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -52,6 +52,9 @@ typedef struct { | ||
| /* The interned Unix epoch datetime instance */ | ||
| PyObject *epoch; | ||
| PyObject *time_struct_time; | ||
| PyObject *time_strftime; | ||
| } datetime_state; | ||
| /* The module has a fixed number of static objects, due to being exposed | ||
| @@ -1879,10 +1882,12 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, | ||
| assert(object && format && timetuple); | ||
| assert(PyUnicode_Check(format)); | ||
| PyObject *strftime = PyImport_ImportModuleAttrString("time", "strftime"); | ||
| if (strftime == NULL) { | ||
| PyObject *current_mod = NULL; | ||
| datetime_state *st = GET_CURRENT_STATE(current_mod); | ||
| if (st == NULL) { | ||
| return NULL; | ||
| } | ||
| PyObject *strftime = st->time_strftime; | ||
| /* Scan the input format, looking for %z/%Z/%f escapes, building | ||
| * a new format. Since computing the replacements for those codes | ||
| @@ -2042,7 +2047,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, | ||
| Py_XDECREF(zreplacement); | ||
| Py_XDECREF(colonzreplacement); | ||
| Py_XDECREF(Zreplacement); | ||
| Py_XDECREF(strftime); | ||
| RELEASE_CURRENT_STATE(st, current_mod); | ||
| return result; | ||
| Error: | ||
| @@ -2055,41 +2060,26 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, | ||
| * from C. Perhaps they should be. | ||
| */ | ||
| /* Call time.time() and return its result (a Python float). */ | ||
| static PyObject * | ||
| time_time(void) | ||
| { | ||
| PyObject *result = NULL; | ||
| PyObject *time = PyImport_ImportModuleAttrString("time", "time"); | ||
| if (time != NULL) { | ||
| result = PyObject_CallNoArgs(time); | ||
| Py_DECREF(time); | ||
| } | ||
| return result; | ||
| } | ||
| /* Build a time.struct_time. The weekday and day number are automatically | ||
| * computed from the y,m,d args. | ||
| */ | ||
| static PyObject * | ||
| build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag) | ||
| { | ||
| PyObject *struct_time; | ||
| PyObject *result; | ||
| struct_time = PyImport_ImportModuleAttrString("time", "struct_time"); | ||
| if (struct_time == NULL) { | ||
| PyObject *current_mod = NULL; | ||
| datetime_state *st = GET_CURRENT_STATE(current_mod); | ||
| if (st == NULL) { | ||
| return NULL; | ||
| } | ||
| result = PyObject_CallFunction(struct_time, "((iiiiiiiii))", | ||
| PyObject *result = PyObject_CallFunction(st->time_struct_time, | ||
| "((iiiiiiiii))", | ||
| y, m, d, | ||
| hh, mm, ss, | ||
| weekday(y, m, d), | ||
| days_before_month(y, m) + d, | ||
| dstflag); | ||
| Py_DECREF(struct_time); | ||
| RELEASE_CURRENT_STATE(st, current_mod); | ||
| return result; | ||
| } | ||
| @@ -3311,7 +3301,11 @@ datetime_date_today_impl(PyTypeObject *type) | ||
| type); | ||
| } | ||
| PyObject *time = time_time(); | ||
| PyTime_t ts; | ||
| if (PyTime_Time(&ts) < 0) { | ||
| return NULL; | ||
| } | ||
| PyObject *time = PyFloat_FromDouble(PyTime_AsSecondsDouble(ts)); | ||
| if (time == NULL) { | ||
| return NULL; | ||
| } | ||
maurycy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. maurycy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -7414,6 +7408,8 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module) | ||
| .us_per_week = Py_NewRef(st_old->us_per_week), | ||
| .seconds_per_day = Py_NewRef(st_old->seconds_per_day), | ||
| .epoch = Py_NewRef(st_old->epoch), | ||
| .time_struct_time = Py_XNewRef(st_old->time_struct_time), | ||
| .time_strftime = Py_XNewRef(st_old->time_strftime), | ||
| }; | ||
| return 0; | ||
| } | ||
| @@ -7458,6 +7454,15 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module) | ||
| return -1; | ||
| } | ||
| st->time_struct_time = PyImport_ImportModuleAttrString("time", "struct_time"); | ||
| if (st->time_struct_time == NULL) { | ||
| return -1; | ||
| } | ||
| st->time_strftime = PyImport_ImportModuleAttrString("time", "strftime"); | ||
| if (st->time_strftime == NULL) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
| @@ -7467,6 +7472,9 @@ traverse_state(datetime_state *st, visitproc visit, void *arg) | ||
| /* heap types */ | ||
| Py_VISIT(st->isocalendar_date_type); | ||
| Py_VISIT(st->time_struct_time); | ||
| Py_VISIT(st->time_strftime); | ||
| return 0; | ||
| } | ||
| @@ -7482,6 +7490,8 @@ clear_state(datetime_state *st) | ||
| Py_CLEAR(st->us_per_week); | ||
| Py_CLEAR(st->seconds_per_day); | ||
| Py_CLEAR(st->epoch); | ||
| Py_CLEAR(st->time_struct_time); | ||
| Py_CLEAR(st->time_strftime); | ||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.