Skip to content

Commit 1391ee6

Browse files
GH-134584: Remove redundant refcount for BINARY_OP_SUBSCR_STR_INT (#142844)
1 parent e79c391 commit 1391ee6

File tree

9 files changed

+72
-29
lines changed

9 files changed

+72
-29
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,26 @@ def testfunc(n):
18771877
self.assertNotIn("_GUARD_TOS_UNICODE", uops)
18781878
self.assertIn("_BINARY_OP_ADD_UNICODE", uops)
18791879

1880+
def test_binary_op_subscr_str_int(self):
1881+
def testfunc(n):
1882+
x = 0
1883+
s = "hello"
1884+
for _ in range(n):
1885+
c = s[1] # _BINARY_OP_SUBSCR_STR_INT
1886+
if c == 'e':
1887+
x += 1
1888+
return x
1889+
1890+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1891+
self.assertEqual(res, TIER2_THRESHOLD)
1892+
self.assertIsNotNone(ex)
1893+
uops = get_opnames(ex)
1894+
self.assertIn("_BINARY_OP_SUBSCR_STR_INT", uops)
1895+
self.assertIn("_COMPARE_OP_STR", uops)
1896+
self.assertIn("_POP_TOP_NOP", uops)
1897+
self.assertNotIn("_POP_TOP", uops)
1898+
self.assertNotIn("_POP_TOP_INT", uops)
1899+
18801900
def test_call_type_1_guards_removed(self):
18811901
def testfunc(n):
18821902
x = 0

Python/bytecodes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,9 +941,9 @@ dummy_func(
941941
}
942942

943943
macro(BINARY_OP_SUBSCR_STR_INT) =
944-
_GUARD_TOS_INT + _GUARD_NOS_UNICODE + unused/5 + _BINARY_OP_SUBSCR_STR_INT;
944+
_GUARD_TOS_INT + _GUARD_NOS_UNICODE + unused/5 + _BINARY_OP_SUBSCR_STR_INT + _POP_TOP_INT + POP_TOP;
945945

946-
op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res)) {
946+
op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res, s, i)) {
947947
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
948948
PyObject *str = PyStackRef_AsPyObjectBorrow(str_st);
949949

@@ -958,9 +958,9 @@ dummy_func(
958958
assert(c < 128);
959959
STAT_INC(BINARY_OP, hit);
960960
PyObject *res_o = (PyObject*)&_Py_SINGLETON(strings).ascii[c];
961-
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
962-
DEAD(sub_st);
963-
PyStackRef_CLOSE(str_st);
961+
INPUTS_DEAD();
962+
s = str_st;
963+
i = sub_st;
964964
res = PyStackRef_FromPyObjectBorrow(res_o);
965965
}
966966

Python/executor_cases.c.h

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 18 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,10 @@ dummy_func(void) {
329329
ctx->done = true;
330330
}
331331

332-
op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res)) {
332+
op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res, s, i)) {
333333
res = sym_new_type(ctx, &PyUnicode_Type);
334+
s = str_st;
335+
i = sub_st;
334336
}
335337

336338
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {

Python/optimizer_cases.c.h

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)