You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mypyc] Make tuple packing and unpacking more efficient (#16022)
Previously returning a tuple from a function resulted in redundant
increfs and decrefs
for each item, and similarly unpacking the returned tuple in an
assignment had extra
incref/decref pair per item. This PR introduces these changes to make
this better:
* Creating a tuple steals the items always.
* Accessing a tuple item optionally borrows the item.
* A borrowed reference can be turned into a regular one using the new
`Unborrow` op.
* The no-op `KeepAlive` op can steal the operands to avoid decrefing the
operands.
Assignment from tuple now uses the three final features to avoid increfs
and decrefs
when unpacking a tuple in assignment. The docstrings in this PR contain
additional
explanation of how this works.
In a micro-benchmark this improved performance by about 2-5%. In
realistic examples
the impact is likely small, but every little helps.
Here is an example where this helps:
```
def f() -> tuple[C, C]:
return C(), C() # Avoid 2 increfs and 2 decrefs
def g() -> None:
x, y = f() # Avoid 2 increfs and 2 decrefs
...
```
---------
Co-authored-by: Alex Waygood <[email protected]>
0 commit comments