Repository navigation
gh-90370: Avoid temporary varargs tuple creation in argument passing
#30312
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
Changes from 1 commit
b68176d
43b9410
9b37a77
46e4472
e06f343
d35410b
0a9fe91
9188052
d245ec0
8d16685
f4213ea
156f8d6
c32aed4
7ef5623
3ac2821
f63a704
8f61f5e
1eff215
5baf0f5
30c0606
d9e7408
0f04a95
85a8ac5
a9d1424
d6ff01a
597152e
3119fa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
varargs tuple creation in argument passing
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3324,14 +3324,15 @@ PyDoc_STRVAR(test_vararg_and_posonly__doc__, | |
| {"test_vararg_and_posonly", (PyCFunction)(void(*)(void))test_vararg_and_posonly, METH_FASTCALL, test_vararg_and_posonly__doc__}, | ||
|
|
||
| static PyObject * | ||
| test_vararg_and_posonly_impl(PyObject *module, PyObject *a, PyObject *args); | ||
| test_vararg_and_posonly_impl(PyObject *module, PyObject *a, | ||
| Py_ssize_t varargssize, PyObject *const *args); | ||
|
|
||
| static PyObject * | ||
| test_vararg_and_posonly(PyObject *module, PyObject *const *args, Py_ssize_t nargs) | ||
| { | ||
| PyObject *return_value = NULL; | ||
| PyObject *a; | ||
| PyObject *__clinic_args = NULL; | ||
| PyObject *const *__clinic_args = NULL; | ||
|
|
||
| if (!_PyArg_CheckPositional("test_vararg_and_posonly", nargs, 1, PY_SSIZE_T_MAX)) { | ||
| goto exit; | ||
|
|
@@ -3341,16 +3342,16 @@ test_vararg_and_posonly(PyObject *module, PyObject *const *args, Py_ssize_t narg | |
| for (Py_ssize_t i = 0; i < nargs - 1; ++i) { | ||
| PyTuple_SET_ITEM(__clinic_args, i, args[1 + i]); | ||
| } | ||
| return_value = test_vararg_and_posonly_impl(module, a, __clinic_args); | ||
| return_value = test_vararg_and_posonly_impl(module, a, varargssize, __clinic_args); | ||
|
|
||
| exit: | ||
| Py_XDECREF(__clinic_args); | ||
| return return_value; | ||
| } | ||
|
|
||
| static PyObject * | ||
| test_vararg_and_posonly_impl(PyObject *module, PyObject *a, PyObject *args) | ||
| /*[clinic end generated code: output=ada613d2d87c9341 input=08dc2bf7afbf1613]*/ | ||
| test_vararg_and_posonly_impl(PyObject *module, PyObject *a, | ||
| Py_ssize_t varargssize, PyObject *const *args) | ||
| /*[clinic end generated code: output=6a6c69c6ff7deb9b input=08dc2bf7afbf1613]*/ | ||
|
|
||
| /*[clinic input] | ||
| test_vararg | ||
|
|
@@ -3370,7 +3371,8 @@ PyDoc_STRVAR(test_vararg__doc__, | |
| {"test_vararg", (PyCFunction)(void(*)(void))test_vararg, METH_FASTCALL|METH_KEYWORDS, test_vararg__doc__}, | ||
|
|
||
| static PyObject * | ||
| test_vararg_impl(PyObject *module, PyObject *a, PyObject *args); | ||
| test_vararg_impl(PyObject *module, PyObject *a, Py_ssize_t varargssize, | ||
| PyObject *const *args); | ||
|
|
||
| static PyObject * | ||
| test_vararg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) | ||
|
|
@@ -3379,26 +3381,27 @@ test_vararg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject | |
| static const char * const _keywords[] = {"a", NULL}; | ||
| static _PyArg_Parser _parser = {NULL, _keywords, "test_vararg", 0}; | ||
| PyObject *argsbuf[2]; | ||
| Py_ssize_t varargssize = Py_MAX(nargs - 1, 0); | ||
| Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; | ||
| PyObject *a; | ||
| PyObject *__clinic_args = NULL; | ||
| PyObject *const *__clinic_args = NULL; | ||
|
|
||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf); | ||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, varargssize, argsbuf); | ||
| if (!args) { | ||
| goto exit; | ||
| } | ||
| a = args[0]; | ||
| __clinic_args = args[1]; | ||
| return_value = test_vararg_impl(module, a, __clinic_args); | ||
| __clinic_args = (PyObject *const *)args[1]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to avoid such unsafe casts. Maybe passing a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This copy and cast are unnecessary. |
||
| return_value = test_vararg_impl(module, a, varargssize, __clinic_args); | ||
|
|
||
| exit: | ||
| Py_XDECREF(__clinic_args); | ||
| return return_value; | ||
| } | ||
|
|
||
| static PyObject * | ||
| test_vararg_impl(PyObject *module, PyObject *a, PyObject *args) | ||
| /*[clinic end generated code: output=f721025731c3bfe8 input=81d33815ad1bae6e]*/ | ||
| test_vararg_impl(PyObject *module, PyObject *a, Py_ssize_t varargssize, | ||
| PyObject *const *args) | ||
| /*[clinic end generated code: output=9f3d65086405e9b6 input=81d33815ad1bae6e]*/ | ||
|
|
||
| /*[clinic input] | ||
| test_vararg_with_default | ||
|
|
@@ -3419,7 +3422,8 @@ PyDoc_STRVAR(test_vararg_with_default__doc__, | |
| {"test_vararg_with_default", (PyCFunction)(void(*)(void))test_vararg_with_default, METH_FASTCALL|METH_KEYWORDS, test_vararg_with_default__doc__}, | ||
|
|
||
| static PyObject * | ||
| test_vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args, | ||
| test_vararg_with_default_impl(PyObject *module, PyObject *a, | ||
| Py_ssize_t varargssize, PyObject *const *args, | ||
| int b); | ||
|
|
||
| static PyObject * | ||
|
|
@@ -3429,17 +3433,18 @@ test_vararg_with_default(PyObject *module, PyObject *const *args, Py_ssize_t nar | |
| static const char * const _keywords[] = {"a", "b", NULL}; | ||
| static _PyArg_Parser _parser = {NULL, _keywords, "test_vararg_with_default", 0}; | ||
| PyObject *argsbuf[3]; | ||
| Py_ssize_t varargssize = Py_MAX(nargs - 1, 0); | ||
| Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; | ||
| PyObject *a; | ||
| PyObject *__clinic_args = NULL; | ||
| PyObject *const *__clinic_args = NULL; | ||
| int b = 0; | ||
|
|
||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf); | ||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, varargssize, argsbuf); | ||
| if (!args) { | ||
| goto exit; | ||
| } | ||
| a = args[0]; | ||
| __clinic_args = args[1]; | ||
| __clinic_args = (PyObject *const *)args[1]; | ||
| if (!noptargs) { | ||
| goto skip_optional_kwonly; | ||
| } | ||
|
|
@@ -3448,17 +3453,17 @@ test_vararg_with_default(PyObject *module, PyObject *const *args, Py_ssize_t nar | |
| goto exit; | ||
| } | ||
| skip_optional_kwonly: | ||
| return_value = test_vararg_with_default_impl(module, a, __clinic_args, b); | ||
| return_value = test_vararg_with_default_impl(module, a, varargssize, __clinic_args, b); | ||
|
|
||
| exit: | ||
| Py_XDECREF(__clinic_args); | ||
| return return_value; | ||
| } | ||
|
|
||
| static PyObject * | ||
| test_vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args, | ||
| test_vararg_with_default_impl(PyObject *module, PyObject *a, | ||
| Py_ssize_t varargssize, PyObject *const *args, | ||
| int b) | ||
| /*[clinic end generated code: output=63b34d3241c52fda input=6e110b54acd9b22d]*/ | ||
| /*[clinic end generated code: output=711a606d86d09fec input=6e110b54acd9b22d]*/ | ||
|
|
||
| /*[clinic input] | ||
| test_vararg_with_only_defaults | ||
|
|
@@ -3479,7 +3484,8 @@ PyDoc_STRVAR(test_vararg_with_only_defaults__doc__, | |
| {"test_vararg_with_only_defaults", (PyCFunction)(void(*)(void))test_vararg_with_only_defaults, METH_FASTCALL|METH_KEYWORDS, test_vararg_with_only_defaults__doc__}, | ||
|
|
||
| static PyObject * | ||
| test_vararg_with_only_defaults_impl(PyObject *module, PyObject *args, int b, | ||
| test_vararg_with_only_defaults_impl(PyObject *module, Py_ssize_t varargssize, | ||
| PyObject *const *args, int b, | ||
| PyObject *c); | ||
|
|
||
| static PyObject * | ||
|
|
@@ -3489,16 +3495,17 @@ test_vararg_with_only_defaults(PyObject *module, PyObject *const *args, Py_ssize | |
| static const char * const _keywords[] = {"b", "c", NULL}; | ||
| static _PyArg_Parser _parser = {NULL, _keywords, "test_vararg_with_only_defaults", 0}; | ||
| PyObject *argsbuf[3]; | ||
| Py_ssize_t varargssize = Py_MAX(nargs - 0, 0); | ||
| Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; | ||
| PyObject *__clinic_args = NULL; | ||
| PyObject *const *__clinic_args = NULL; | ||
| int b = 0; | ||
| PyObject *c = " "; | ||
|
|
||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, argsbuf); | ||
| args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, varargssize, argsbuf); | ||
| if (!args) { | ||
| goto exit; | ||
| } | ||
| __clinic_args = args[0]; | ||
| __clinic_args = (PyObject *const *)args[0]; | ||
| if (!noptargs) { | ||
| goto skip_optional_kwonly; | ||
| } | ||
|
|
@@ -3513,14 +3520,14 @@ test_vararg_with_only_defaults(PyObject *module, PyObject *const *args, Py_ssize | |
| } | ||
| c = args[2]; | ||
| skip_optional_kwonly: | ||
| return_value = test_vararg_with_only_defaults_impl(module, __clinic_args, b, c); | ||
| return_value = test_vararg_with_only_defaults_impl(module, varargssize, __clinic_args, b, c); | ||
|
|
||
| exit: | ||
| Py_XDECREF(__clinic_args); | ||
| return return_value; | ||
| } | ||
|
|
||
| static PyObject * | ||
| test_vararg_with_only_defaults_impl(PyObject *module, PyObject *args, int b, | ||
| test_vararg_with_only_defaults_impl(PyObject *module, Py_ssize_t varargssize, | ||
| PyObject *const *args, int b, | ||
| PyObject *c) | ||
| /*[clinic end generated code: output=dc29ce6ebc2ec10c input=fa56a709a035666e]*/ | ||
| /*[clinic end generated code: output=f1415a05ea48fc50 input=fa56a709a035666e]*/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2470,11 +2470,11 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs, | |
| PyObject *kwargs, PyObject *kwnames, | ||
| struct _PyArg_Parser *parser, | ||
| int minpos, int maxpos, int minkw, | ||
| int vararg, PyObject **buf) | ||
| int vararg, Py_ssize_t varargssize, | ||
| PyObject **buf) | ||
| { | ||
| PyObject *kwtuple; | ||
| PyObject *keyword; | ||
| Py_ssize_t varargssize = 0; | ||
| int i, posonly, minposonly, maxargs; | ||
| int reqlimit = minkw ? maxpos + minkw : minpos; | ||
| Py_ssize_t nkwargs; | ||
|
|
@@ -2529,26 +2529,12 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs, | |
| return NULL; | ||
| } | ||
|
|
||
| /* create varargs tuple */ | ||
| varargssize = nargs - maxpos; | ||
| if (varargssize < 0) { | ||
| varargssize = 0; | ||
| } | ||
| buf[vararg] = PyTuple_New(varargssize); | ||
| if (!buf[vararg]) { | ||
| return NULL; | ||
| } | ||
| /* pass varargs by pointer */ | ||
| buf[vararg] = (PyObject *)&args[vararg]; | ||
|
|
||
| /* copy tuple args */ | ||
| for (i = 0; i < nargs; i++) { | ||
| if (i >= vararg) { | ||
| Py_INCREF(args[i]); | ||
| PyTuple_SET_ITEM(buf[vararg], i - vararg, args[i]); | ||
| continue; | ||
| } | ||
| else { | ||
| /* copy required positional args */ | ||
| for (i = 0; i < vararg; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it possible to avoid copying at all by directly passing some sub-buffer of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. None of the positional args or variable-length args need to be copied. I will let _PyArg_UnpackKeywordsWithVarargFast parse default and keyword args only. |
||
| buf[i] = args[i]; | ||
| } | ||
| } | ||
|
|
||
| /* copy keyword args using kwtuple to drive process */ | ||
|
|
||
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 seems suspicious -- isn't __clinic_args a pointer-to-PyObject-pointer, not a pointer-to-tuple?
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.
My fault. The tuple doesn't need to be created.