8000 gh-90370: Avoid temporary `varargs` tuple creation in argument passing by colorfulappl · Pull Request #30312 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b68176d
Avoid temporary `varargs` tuple creation in argument passing
colorfulappl Dec 31, 2021
43b9410
Optimize argument passing of builtin print() with modified Argument C…
colorfulappl Dec 31, 2021
9b37a77
Revert modification of PyAPI_FUNC _PyArg_UnpackKeywordsWithVararg to …
colorfulappl Jan 4, 2022
46e4472
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 4, 2022
e06f343
Revert _PyArg_UnpackKeywordsWithVararg and add _PyArg_UnpackKeywordsW…
colorfulappl Jan 4, 2022
d35410b
Fix a bug which allows more than one varargs
colorfulappl Feb 22, 2022
0a9fe91
Do not copy posargs and vararg during argument parsing
colorfulappl Mar 22, 2022
9188052
Rename _PyArg_UnpackKeywordsWithVarargFast, it returns kwargs only now
colorfulappl Mar 23, 2022
d245ec0
Check type of varargs when generating argument parser
colorfulappl Mar 23, 2022
8d16685
Fix varargssize calculation in class init and add test cases
colorfulappl Mar 23, 2022
f4213ea
Merge branch 'main' into opt_ac
colorfulappl Mar 23, 2022
156f8d6
Rerun make clinic
colorfulappl Mar 23, 2022
c32aed4
Edit documentation
colorfulappl Mar 23, 2022
7ef5623
Merge branch 'main' into opt_ac
colorfulappl Feb 3, 2023
3ac2821
Fix errors introduced by merging and rerun make clinic
colorfulappl Feb 3, 2023
f63a704
Update news
colorfulappl Feb 3, 2023
8f61f5e
Fix varargssize in new_or_init
colorfulappl Feb 6, 2023
1eff215
Simplify the code a bit
colorfulappl Feb 6, 2023
5baf0f5
Optimize generated varargssize assignment
colorfulappl Feb 6, 2023
30c0606
Fix argument passing in new_or_init
colorfulappl Feb 7, 2023
d9e7408
Add tests for class method `__new__`
colorfulappl Feb 7, 2023
0f04a95
Merge branch 'main' into opt_ac
colorfulappl Feb 7, 2023
85a8ac5
Correct test function name
colorfulappl Feb 8, 2023
a9d1424
Fix vararg parsing when its name is not `args`
colorfulappl Feb 8, 2023
d6ff01a
Merge branch 'main' into opt_ac
colorfulappl Feb 8, 2023
597152e
Add testcases for undeclared keyword arguments
colorfulappl Feb 8, 2023
3119fa6
Merge branch 'main' into opt_ac
erlend-aasland Apr 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert modification of PyAPI_FUNC _PyArg_UnpackKeywordsWithVararg to …
…keep backward compatibility
  • Loading branch information
colorfulappl committed Jan 4, 2022
commit 9b37a772f3b265eb823e7f884c8f50d68f70941b
3 changes: 1 addition & 2 deletions Include/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
PyObject *kwargs, PyObject *kwnames,
struct _PyArg_Parser *parser,
int minpos, int maxpos, int minkw,
int vararg, Py_ssize_t varargssize,
PyObject **buf);
int vararg, PyObject **buf);

#define _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, parser, minpos, maxpos, minkw, buf) \
(((minkw) == 0 && (kwargs) == NULL && (kwnames) == NULL && \
Expand Down
12 changes: 6 additions & 6 deletions Li 8000 b/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -3386,7 +3386,7 @@ test_vararg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
PyObject *a;
PyObject *const *__clinic_args = NULL;

args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, varargssize, argsbuf);
args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf);
if (!args) {
goto exit;
}
Expand All @@ -3401,7 +3401,7 @@ exit:
static PyObject *
test_vararg_impl(PyObject *module, PyObject *a, Py_ssize_t varargssize,
PyObject *const *args)
/*[clinic end generated code: output=9f3d65086405e9b6 input=81d33815ad1bae6e]*/
/*[clinic end generated code: output=c67c92891ba95cb0 input=81d33815ad1bae6e]*/

/*[clinic input]
test_vararg_with_default
Expand Down Expand Up @@ -3439,7 +3439,7 @@ test_vararg_with_default(PyObject *module, PyObject *const *args, Py_ssize_t nar
PyObject *const *__clinic_args = NULL;
int b = 0;

args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, varargssize, argsbuf);
args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf);
if (!args) {
goto exit;
}
Expand All @@ -3463,7 +3463,7 @@ static PyObject *
test_vararg_with_default_impl(PyObject *module, PyObject *a,
Py_ssize_t varargssize, PyObject *const *args,
int b)
/*[clinic end generated code: output=711a606d86d09fec input=6e110b54acd9b22d]*/
/*[clinic end generated code: output=a36408b211ae3626 input=6e110b54acd9b22d]*/

/*[clinic input]
test_vararg_with_only_defaults
Expand Down Expand Up @@ -3501,7 +3501,7 @@ test_vararg_with_only_defaults(PyObject *module, PyObject *const *args, Py_ssize
int b = 0;
PyObject *c = " ";

args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, varargssize, argsbuf);
args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, argsbuf);
if (!args) {
goto exit;
}
Expand Down Expand Up @@ -3530,4 +3530,4 @@ static PyObject *
test_vararg_with_only_defaults_impl(PyObject *module, Py_ssize_t varargssize,
PyObject *const *args, int b,
PyObject *c)
/*[clinic end generated code: output=f1415a05ea48fc50 input=fa56a709a035666e]*/
/*[clinic end generated code: output=e626ca46f05eebcc input=fa56a709a035666e]*/
4 changes: 2 additions & 2 deletions Python/clinic/bltinmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, Py_ssize_t varargssize,
PyObject **buf)
int vararg, 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;
Expand Down Expand Up @@ -2529,12 +2529,17 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
return NULL;
}

varargssize = nargs - maxpos;
if (varargssize < 0) {
varargssize = 0;
}

/* pass varargs by pointer */
buf[vararg] = (PyObject *)&args[vararg];

/* copy required positional args */
for (i = 0; i < vararg; i++) {
Copy link
Member

Choose a reason for hiding this comment

The 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 *args buffer into the ..._impl function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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];
buf[i] = args[i];
}

/* copy keyword args using kwtuple to drive process */
Expand Down
2 changes: 1 addition & 1 deletion Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def parser_body(prototype, *fields, declarations=''):
min_kw_only
)
else:
args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s, varargssize" % (
args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s" % (
min_pos,
max_pos,
min_kw_only,
Expand Down
0