Skip to content
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

gh-100146: Steal references from stack when building a list #100147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lpereira
Copy link
Contributor

@lpereira lpereira commented Dec 9, 2022

When executing the BUILD_LIST opcode, steal the references from the stack, in a manner similar to the BUILD_TUPLE opcode. Implement this by offloading the logic to a new private API, _PyList_FromArraySteal(), that works similarly to _PyTuple_FromArraySteal().

This way, instead of performing multiple stack pointer adjustments while the list is being initialized, the stack is adjusted only once and a fast memory copy operation is performed in one fell swoop.

When executing the BUILD_LIST opcode, steal the references from the stack,
in a manner similar to the BUILD_TUPLE opcode.  Implement this by offloading
the logic to a new private API, _PyList_FromArraySteal(), that works similarly
to _PyTuple_FromArraySteal().

This way, instead of performing multiple stack pointer adjustments while the
list is being initialized, the stack is adjusted only once and a fast memory
copy operation is performed in one fell swoop.
@lpereira lpereira force-pushed the build-list-stack-steal branch from 808385c to 9379cd4 Compare Dec 9, 2022
Copy link
Member

@gvanrossum gvanrossum left a comment

Yeah, looks good to me. We probably won't be able to prove this improves performance, but I don't doubt that plenty of code will benefit from such a micro-optimization.

Ping me Monday about merging it.

@gvanrossum gvanrossum added the 🔨 test-with-buildbots Test the PR with the buildbot fleet and report in the status section label Dec 10, 2022
@bedevere-bot
Copy link

bedevere-bot commented Dec 10, 2022

🤖 New build scheduled with the buildbot fleet by @gvanrossum for commit 9379cd4 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test the PR with the buildbot fleet and report in the status section label Dec 10, 2022
@python python deleted a comment from netlify bot Dec 10, 2022
@gvanrossum
Copy link
Member

gvanrossum commented Dec 10, 2022

I've requested a buildbot build to make sure there isn't some dumb issue with refleaks.

Improve ``BUILD_LIST`` opcode so that it works similarly to the
``BUILD_TUPLE`` opcode, by stealing references from the stack rather than
repeatedly using stack operations to set list elements. Implementation
details are in a new private API :c:func:`_PyList_FromArraySteal`.
Copy link
Contributor

@kumaraditya303 kumaraditya303 Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
details are in a new private API :c:func:`_PyList_FromArraySteal`.
details are in a new private API :c:func:`!_PyList_FromArraySteal`.

No need to link to private functions.

Improve ``BUILD_LIST`` opcode so that it works similarly to the
``BUILD_TUPLE`` opcode, by stealing references from the stack rather than
Copy link
Member

@iritkatriel iritkatriel Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Improve ``BUILD_LIST`` opcode so that it works similarly to the
``BUILD_TUPLE`` opcode, by stealing references from the stack rather than
Improve :opcode:`BUILD_LIST` so that it works similarly to
:opcode:`BUILD_TUPLE`, by stealing references from the stack rather than

@gvanrossum
Copy link
Member

gvanrossum commented Dec 10, 2022

I don't know what's up with the ARM64 Windows buildbot, but it's been failing the same way for at least three days, so no need to worry that this PR could somehow have made it go red.

PyObject *
_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
{
if (n == 0) {
Copy link
Member

@markshannon markshannon Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why special case 0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants