Skip to content

Replace trivial Py_BuildValue() with direct C API call #110093

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
serhiy-storchaka opened this issue Sep 29, 2023 · 0 comments
Closed

Replace trivial Py_BuildValue() with direct C API call #110093

serhiy-storchaka opened this issue Sep 29, 2023 · 0 comments

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Sep 29, 2023

Py_BuildValue() is a powerful tool which can be used to create complex Python structures. But in some cases it is used to create a Python objects from a C value for which a special C API exists. Since Py_BuildValue() adds some overhead (parsing the format string, copying va_list, several levels of recursive calls), it is less efficient than direct call of the corresponding C API. For example Py_BuildValue("i", x) can be replaced with PyLong_FromLong((int)x) or simply PyLong_FromLong(x), Py_BuildValue("(OO)", x, y) can be replaced with PyTuple_Pack(2, x, y).

Linked PRs

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant