8000 gh-134584: Eliminate redundant refcounting from _BINARY_OP_ADD_UNICODE by corona10 · Pull Request #135817 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134584: Eliminate redundant refcounting from _BINARY_OP_ADD_UNICODE #135817

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

corona10
Copy link
Member
@corona10 corona10 commented Jun 22, 2025

@corona10
Copy link
Member Author

Maybe I have to wait #135761 first

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.

Mostly LGTM.

Comment on lines 330 to 338
l = left;
r = right;
Py_DECREF(temp);
}
else {
res = sym_new_type(ctx, &PyUnicode_Type);
l = left;
r = right;
}
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to be in the branch. You can write this at the end of the instruction here.

That way, we only need to have one l = left; r = right

self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_NOP", uops)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also add

self.assertIn("_BINARY_OP_ADD_UNICODE", uops)

to make sure this is the op that's actually generated?

Should we also test for _POP_TOP_UNICODE?

Copy link
Member Author

Choose a reason for hiding this comment

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

In this code, It will be replaced to _POP_TOP_NOP because _POP_TOP_UNICODE will be optimized to _POP_TOP_NOP .

Copy link
Member

Choose a reason for hiding this comment

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

Yup, do you think it makes sense to test _POP_TOP_UNICODE separately? That is, the case when it is not optimized to _POP_TOP_NOP? (not sure how difficult it'll be to come up with a test case though)

Copy link
Member

Choose a reason for hiding this comment

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

I have a test for that in my original PR already.

Comment on lines 2367 to 2383
def test_store_fast_pop_top_specialize_unicode(self):
def random_str(n):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(n))
def testfunc(n):
y = random_str(32)
for _ in range(n):
x = y + y # _POP_TOP
x = None # _POP_TOP_NOP 8000

testfunc(TIER2_THRESHOLD)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_POP_TOP_NOP", uops)

Copy link
Member
@Fidget-Spinner Fidget-Spinner Jun 22, 2025

Choose a reason for hiding this comment

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

Sorry I thought of a better test: you can use this one but change for str, and assert that the _POP_TOP_UNICODE uop is inside

def test_float_op_refcount_elimination(self):

@Fidget-Spinner
Copy link
Member

Sorry I just merged my PR in #135761. You'll have to rebase/merge in changes!

@corona10 corona10 requested a review from Fidget-Spinner June 23, 2025 17:13
@corona10
Copy link
Member Author

@Fidget-Spinner Done!

@corona10 corona10 changed the title gh-134584: Elimiate redundant refcounting from _BINARY_OP_ADD_UNICODE gh-134584: Eliminate redundant refcounting from _BINARY_OP_ADD_UNICODE Jun 23, 2025
Copy link
Member
@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

This adds an extra field to every frame, which is likely to cost more than the improvement this PR would otherwise bring.
If we apply this change to every BINARY_OP, then it might be worth it.

Having said that, TOS caching might solve that problem.
If we can guarantee that each uop that would overflow the stack always leaves the necessary number of outputs in registers and that we never spill those values to memory,
than we can do this without the extra stack space.

Let's wait until TOS is in, and then we can investigate if that approach makes sense.

@bedevere-app
Copy link
bedevere-app bot commented Jun 24, 2025

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

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.

4 participants
0