This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Avoid temporary `varargs` tuple creation in argument passing
Type: performance Stage: patch review
Components: Argument Clinic Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, colorfulappl, erlendaasland, larry, pablogsal
Priority: normal Keywords: patch

Created on 2021-12-31 10:05 by colorfulappl, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
bench-print.py colorfulappl, 2021-12-31 10:18
Pull Requests
URL Status Linked Edit
PR 30312 open colorfulappl, 2021-12-31 10:07
Messages (6)
msg409412 - (view) Author: (colorfulappl) * Date: 2021-12-31 10:05
When "Augument Clinic generated code" are parsing arguments, the args are packed to a tuple before passing to callee. This may be unnecessary.

Pass a raw pointer which points to on-stack varargs, and a varargssize integer to indicate how many varargs are passed, can save the time of tuple creation/destruction and value copy.
msg409413 - (view) Author: (colorfulappl) * Date: 2021-12-31 10:18
I wrote some microbenchs.

Patch: https://github.com/python/cpython/pull/30312/commits/b68176d081e19a3cedbaf2cdb31ecd7690421ec8

Environment:
macOS 12.1
clang 13.0.0
configure with --enable-optimizations

Result on microbench:

```
+--------------------------------------------+-------------------------+------------------------+
| Benchmark                                  | ./opt_baseline/res.json | ./opt_patched/res.json |
+============================================+=========================+========================+
| print(a, b, c)                             | 917 ns                  | 820 ns: 1.12x faster   |
+--------------------------------------------+-------------------------+------------------------+
| print(a, b, c, *v)                         | 1.56 us                 | 1.62 us: 1.04x slower  |
+--------------------------------------------+-------------------------+------------------------+
| print(a, sep='', file=stdout)              | 376 ns                  | 295 ns: 1.27x faster   |
+--------------------------------------------+-------------------------+------------------------+
| print(*v, sep='', flush=True, file=stdout) | 2.02 us                 | 1.94 us: 1.04x faster  |
+--------------------------------------------+-------------------------+------------------------+
| Geometric mean                             | (ref)                   | 1.05x faster           |
+--------------------------------------------+-------------------------+------------------------+

Benchmark hidden because not significant (3): print(a), print(a, sep='', flush=True, file=stdout), print(a, b, c, *v, sep='', flush=True, file=stdout)
```
msg409414 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-12-31 11:04
Note that _PyArg_UnpackKeywordsWithVararg is defined with PyAPI_FUNC. Changing its argument spec is strictly a backwards incompatible change, IIUC.
msg409659 - (view) Author: (colorfulappl) * Date: 2022-01-04 09:36
I am a rookie in Python, did not notice changing PyAPI_FUNC means breaking backward compatibility.

I have reverted _PyArg_UnpackKeywordsWithVararg and committed again.
msg411129 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2022-01-21 14:04
> Note that _PyArg_UnpackKeywordsWithVararg is defined with PyAPI_FUNC. Changing its argument spec is strictly a backwards incompatible change, IIUC.

AFAIK we have committed _PyArg_UnpackKeywordsWithVararg on 3.11 alpha, so I think it should be fine. Also CC: @pablogsal
msg411130 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-21 14:06
> AFAIK we have committed _PyArg_UnpackKeywordsWithVararg on 3.11 alpha, so I think it should be fine.

I see, so no ABI worries then.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90370
2022-01-21 14:06:02erlendaaslandsetmessages: + msg411130
2022-01-21 14:04:00BTaskayasetnosy: + pablogsal
messages: + msg411129
2022-01-04 09:36:50colorfulapplsetmessages: + msg409659
2021-12-31 11:04:46erlendaaslandsetnosy: + BTaskaya
messages: + msg409414
2021-12-31 10:18:07colorfulapplsetfiles: + bench-print.py

messages: + msg409413
2021-12-31 10:07:14colorfulapplsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28527
2021-12-31 10:05:47colorfulapplcreate