◐ Shell
clean mode source ↗

gh-134584: Eliminate redundant refcounting from `_UNPACK_SEQUENCE_TWO_TUPLE` by cocolato · Pull Request #142952 · python/cpython

@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.