Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-134584: Eliminate redundant refcounting from TO_BOOL_STR
Signed-off-by: Manjusaka <[email protected]>
  • Loading branch information
Zheaoli committed Jan 4, 2026
commit a0895de282b7edbaf733941ea2f3d459e73e2aba
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 18 additions & 16 deletions Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,22 @@ def testfunc(n):
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 1)
self.assertIn("_POP_TOP_NOP", uops)

def test_to_bool_str(self):
def f(n):
for i in range(n):
false = i == TIER2_THRESHOLD
empty = "X"[:false]
if empty:
return 1
return 0

res, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(res, 0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_TO_BOOL_STR", uops)
self.assertIn("_POP_TOP_NOP", uops)
Comment thread
Zheaoli marked this conversation as resolved.

def test_attr_promotion_failure(self):
# We're not testing for any specific uops here, just
# testing it doesn't crash.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminate redundant refcounting from ``TO_BOOL_STR``.
8 changes: 5 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,23 +547,25 @@ dummy_func(
EXIT_IF(!PyUnicode_CheckExact(value_o));
}

op(_TO_BOOL_STR, (value -- res)) {
op(_TO_BOOL_STR, (value -- res, v)) {
STAT_INC(TO_BOOL, hit);
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
if (value_o == &_Py_STR(empty)) {
assert(_Py_IsImmortal(value_o));
v = value;
DEAD(value);
res = PyStackRef_False;
}
else {
assert(Py_SIZE(value_o));
PyStackRef_CLOSE(value);
v = value;
DEAD(value);
res = PyStackRef_True;
}
Comment thread
Zheaoli marked this conversation as resolved.
Outdated
}

macro(TO_BOOL_STR) =
_GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR;
_GUARD_TOS_UNICODE + unused/1 + unused/2 + _TO_BOOL_STR + _POP_TOP_UNICODE;

op(_REPLACE_WITH_TRUE, (value -- res)) {
PyStackRef_CLOSE(value);
Expand Down
70 changes: 63 additions & 7 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,9 @@ dummy_func(void) {
sym_set_type(value, &PyUnicode_Type);
}

op(_TO_BOOL_STR, (value -- res)) {
op(_TO_BOOL_STR, (value -- res, v)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This optimization is broken because the stack effect changed,

You need to fix optimize_to_bool to allow for an insert mode, so it should be the same as the one in her https://github.com/python/cpython/pull/143335/files (see _INSERT_1_LOAD_CONST_INLINE_BORROW)

So optimize_to_bool(this_instr, ctx, value, &res, true);, where true is for insert_mode, which will change to opcode to _INSERT_1_LOAD_CONST_INLINE_BORROW

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can do this change, as it's a little more complex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! TIL

I may need a little bit time to update the patch(lol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can do this change, as it's a little more complex

If this is possible, I would like to make this change!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have complete the code on my local branch, not push. Because I have some issue about this part

I think the patch, I will use _INSERT_1_LOAD_CONST_INLINE_BORROW but this is made in #143335

For now, I add a tier2 op named _INSERT_1_LOAD_CONST_INLINE_BORROW copy from #143335. I'm not sure if this is OK?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah just copy the op over

v = value;
if (!already_bool) {
res = sym_new_truthiness(ctx, value, true);
}
Expand Down
6 changes: 6 additions & 0 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading