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

[mypyc] Make tuple packing and unpacking more efficient #16022

Merged
merged 10 commits into from Sep 12, 2023

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Sep 2, 2023

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

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

One or two typos I spotted:

mypyc/ir/ops.py Outdated Show resolved Hide resolved
mypyc/ir/ops.py Outdated Show resolved Hide resolved
mypyc/irbuild/statement.py Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JukkaL
Copy link
Collaborator Author

JukkaL commented Sep 2, 2023

I'm not sure what's going on with the build failures. The tests seem to pass locally, but in CI the mypyc-compiled builds get a sigterm, apparently.

@JukkaL JukkaL merged commit 66fbf5b into master Sep 12, 2023
13 checks passed
@JukkaL JukkaL deleted the mypyc-tuple-refcount branch September 12, 2023 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants