Skip to content

Conversation

@cocolato
Copy link
Contributor

@cocolato cocolato commented Dec 18, 2025

@Fidget-Spinner
Copy link
Member

I need to discuss with Mark for this one. As it might cause a stack overflow if the stack bounds are exceeded.

@cocolato cocolato changed the title gh-134584: Eliminate redundant refcounting from UNPACK_SEQUENCE family gh-134584: Eliminate redundant refcounting from UNPACK_SEQUENCE family Dec 22, 2025
@aisk aisk added the skip news label Dec 22, 2025
@markshannon
Copy link
Member

@cocolato Thanks for the contribution.

Unfortunately, I don't think that this transformation is going to be useful.
Most unpacking is of new references, typically a, b = foo(), so we wouldn't be able to eliminate the reference counting operation.

There is potentially an optimization we can do with _UNPACK_SEQUENCE but it is considerably more complex.
If we can track which references are unique references to a new object, we can eliminate even more refcount operations.
For example: a, b = foo(), where foo ends return x, y, once specialized and traced gives us:

...
_BUILD_TUPLE 2
_RETURN_VALUE
_GUARD_TOS_TUPLE 
_UNPACK_SEQUENCE_TWO_TUPLE

If we know that the reference to the tuple in _UNPACK_SEQUENCE_TWO_TUPLE is unique we could extract the contents and free the tuple without any reference counting operations.

@cocolato
Copy link
Contributor Author

Thanks for the explanation! I now see that it doesn’t reduce refcount overhead for the common case of consuming new references (return values), so I’ll convert this PR to a draft. I might dig into how the Tier 2 optimizer could track such uniqueness.

@cocolato cocolato marked this pull request as draft December 23, 2025 11:41
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.

4 participants