Skip to content

Conversation

@reidenong
Copy link
Contributor

@reidenong reidenong commented Jan 28, 2026

  1. Implements unique reference tracking to references in the JIT Optimizer.
  2. Uses the unique reference tracking to eliminate unnecessary reference counting for uniquely referenced tuples. ex:
x, y, z = f()

Some notes:

  • Only implements the unique reference tracking (and relevant optimization) for tuples in _BUILD_TUPLE, to be extended to other objects in the future
  • Objects are deemed as not uniquely referenced when they are made aliases or duplicated, in uops of _LOAD_FAST_* and _COPY.

Would appreciate any feedback.
Thanks

@reidenong
Copy link
Contributor Author

reidenong commented Jan 28, 2026

Discussed with @Fidget-Spinner, will reopen the PR after implementing PyStackRef_CLOSE_SPECIALIZED for _UNPACK_SEQUENCE_UNIQUE_TUPLE

@reidenong reidenong marked this pull request as draft January 28, 2026 18:38
@reidenong reidenong marked this pull request as ready for review January 29, 2026 11:04
@Fidget-Spinner
Copy link
Member

Windows Ci failure looks possibly related, can you please look into it?

@reidenong reidenong marked this pull request as draft January 31, 2026 06:39
@reidenong reidenong marked this pull request as ready for review January 31, 2026 16:36
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

This is excellent. Just 2 comments, then I think it's safe to merge.

static inline JitOptRef
PyJitRef_StripBorrowInfo(JitOptRef ref)
{
if (PyJitRef_IsUnique(ref)) return ref;
Copy link
Member

Choose a reason for hiding this comment

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

Please follow PEP 7 and use curly braces around this.


op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
assert(oparg > 0);
bottom = PyJitRef_IsUnique(bottom) ? PyJitRef_StripReferenceInfo(bottom) : bottom;
Copy link
Member

Choose a reason for hiding this comment

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

We only need to strip unique info here, not the borrow info too. This way we still get refcount elimination with the borrow.

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.

3 participants