forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mypyc] Make tuple packing and unpacking more efficient (python#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]>
- Loading branch information
1 parent
9e520c3
commit 66fbf5b
Showing
10 changed files
with
200 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.