summaryrefslogtreecommitdiffstats
path: root/Modules/clinic/_datetimemodule.c.h
diff options
context:
space:
mode:
authorEugene Toder <eltoder@users.noreply.github.com>2024-01-30 15:19:46 (GMT)
committerGitHub <noreply@github.com>2024-01-30 15:19:46 (GMT)
commit1f515e8a109204f7399d85b7fd806135166422d9 (patch)
treea8af50bda79583edcfc0b24306a8a71a49b5609a /Modules/clinic/_datetimemodule.c.h
parent4287e8608bcabcc5bde851d838d4709db5d69089 (diff)
downloadcpython-1f515e8a109204f7399d85b7fd806135166422d9.zip
cpython-1f515e8a109204f7399d85b7fd806135166422d9.tar.gz
cpython-1f515e8a109204f7399d85b7fd806135166422d9.tar.bz2
gh-112919: Speed-up datetime, date and time.replace() (GH-112921)
Use argument clinic and call new_* functions directly. This speeds up these functions 6x to 7.5x when calling with keyword arguments.
Diffstat (limited to 'Modules/clinic/_datetimemodule.c.h')
-rw-r--r--Modules/clinic/_datetimemodule.c.h352
1 files changed, 351 insertions, 1 deletions
diff --git a/Modules/clinic/_datetimemodule.c.h b/Modules/clinic/_datetimemodule.c.h
index 1ee50fc..48499e0 100644
--- a/Modules/clinic/_datetimemodule.c.h
+++ b/Modules/clinic/_datetimemodule.c.h
@@ -82,6 +82,207 @@ exit:
return return_value;
}
+PyDoc_STRVAR(datetime_date_replace__doc__,
+"replace($self, /, year=unchanged, month=unchanged, day=unchanged)\n"
+"--\n"
+"\n"
+"Return date with new specified fields.");
+
+#define DATETIME_DATE_REPLACE_METHODDEF \
+ {"replace", _PyCFunction_CAST(datetime_date_replace), METH_FASTCALL|METH_KEYWORDS, datetime_date_replace__doc__},
+
+static PyObject *
+datetime_date_replace_impl(PyDateTime_Date *self, int year, int month,
+ int day);
+
+static PyObject *
+datetime_date_replace(PyDateTime_Date *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 3
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(year), &_Py_ID(month), &_Py_ID(day), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"year", "month", "day", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "replace",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[3];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ int year = GET_YEAR(self);
+ int month = GET_MONTH(self);
+ int day = GET_DAY(self);
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 3, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (args[0]) {
+ year = PyLong_AsInt(args[0]);
+ if (year == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[1]) {
+ month = PyLong_AsInt(args[1]);
+ if (month == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ day = PyLong_AsInt(args[2]);
+ if (day == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_pos:
+ return_value = datetime_date_replace_impl(self, year, month, day);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(datetime_time_replace__doc__,
+"replace($self, /, hour=unchanged, minute=unchanged, second=unchanged,\n"
+" microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged)\n"
+"--\n"
+"\n"
+"Return time with new specified fields.");
+
+#define DATETIME_TIME_REPLACE_METHODDEF \
+ {"replace", _PyCFunction_CAST(datetime_time_replace), METH_FASTCALL|METH_KEYWORDS, datetime_time_replace__doc__},
+
+static PyObject *
+datetime_time_replace_impl(PyDateTime_Time *self, int hour, int minute,
+ int second, int microsecond, PyObject *tzinfo,
+ int fold);
+
+static PyObject *
+datetime_time_replace(PyDateTime_Time *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 6
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(hour), &_Py_ID(minute), &_Py_ID(second), &_Py_ID(microsecond), &_Py_ID(tzinfo), &_Py_ID(fold), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"hour", "minute", "second", "microsecond", "tzinfo", "fold", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "replace",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[6];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ int hour = TIME_GET_HOUR(self);
+ int minute = TIME_GET_MINUTE(self);
+ int second = TIME_GET_SECOND(self);
+ int microsecond = TIME_GET_MICROSECOND(self);
+ PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
+ int fold = TIME_GET_FOLD(self);
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 5, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (args[0]) {
+ hour = PyLong_AsInt(args[0]);
+ if (hour == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[1]) {
+ minute = PyLong_AsInt(args[1]);
+ if (minute == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[2]) {
+ second = PyLong_AsInt(args[2]);
+ if (second == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[3]) {
+ microsecond = PyLong_AsInt(args[3]);
+ if (microsecond == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[4]) {
+ tzinfo = args[4];
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+skip_optional_pos:
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ fold = PyLong_AsInt(args[5]);
+ if (fold == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_kwonly:
+ return_value = datetime_time_replace_impl(self, hour, minute, second, microsecond, tzinfo, fold);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(datetime_datetime_now__doc__,
"now($type, /, tz=None)\n"
"--\n"
@@ -146,4 +347,153 @@ skip_optional_pos:
exit:
return return_value;
}
-/*[clinic end generated code: output=562813dd3e164794 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(datetime_datetime_replace__doc__,
+"replace($self, /, year=unchanged, month=unchanged, day=unchanged,\n"
+" hour=unchanged, minute=unchanged, second=unchanged,\n"
+" microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged)\n"
+"--\n"
+"\n"
+"Return datetime with new specified fields.");
+
+#define DATETIME_DATETIME_REPLACE_METHODDEF \
+ {"replace", _PyCFunction_CAST(datetime_datetime_replace), METH_FASTCALL|METH_KEYWORDS, datetime_datetime_replace__doc__},
+
+static PyObject *
+datetime_datetime_replace_impl(PyDateTime_DateTime *self, int year,
+ int month, int day, int hour, int minute,
+ int second, int microsecond, PyObject *tzinfo,
+ int fold);
+
+static PyObject *
+datetime_datetime_replace(PyDateTime_DateTime *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 9
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(year), &_Py_ID(month), &_Py_ID(day), &_Py_ID(hour), &_Py_ID(minute), &_Py_ID(second), &_Py_ID(microsecond), &_Py_ID(tzinfo), &_Py_ID(fold), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"year", "month", "day", "hour", "minute", "second", "microsecond", "tzinfo", "fold", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "replace",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[9];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ int year = GET_YEAR(self);
+ int month = GET_MONTH(self);
+ int day = GET_DAY(self);
+ int hour = DATE_GET_HOUR(self);
+ int minute = DATE_GET_MINUTE(self);
+ int second = DATE_GET_SECOND(self);
+ int microsecond = DATE_GET_MICROSECOND(self);
+ PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
+ int fold = DATE_GET_FOLD(self);
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 8, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (args[0]) {
+ year = PyLong_AsInt(args[0]);
+ if (year == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[1]) {
+ month = PyLong_AsInt(args[1]);
+ if (month == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[2]) {
+ day = PyLong_AsInt(args[2]);
+ if (day == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[3]) {
+ hour = PyLong_AsInt(args[3]);
+ if (hour == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[4]) {
+ minute = PyLong_AsInt(args[4]);
+ if (minute == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[5]) {
+ second = PyLong_AsInt(args[5]);
+ if (second == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[6]) {
+ microsecond = PyLong_AsInt(args[6]);
+ if (microsecond == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+ if (args[7]) {
+ tzinfo = args[7];
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
+skip_optional_pos:
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ fold = PyLong_AsInt(args[8]);
+ if (fold == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_kwonly:
+ return_value = datetime_datetime_replace_impl(self, year, month, day, hour, minute, second, microsecond, tzinfo, fold);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=c7a04b865b1e0890 input=a9049054013a1b77]*/