Skip to content

Commit 6c8ca1c

Browse files
authored
gh-134584: Optimize _BINARY_OP_SUBSCR_LIST_SLICE (GH-144659)
1 parent 40a82ab commit 6c8ca1c

File tree

11 files changed

+108
-60
lines changed

11 files changed

+108
-60
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,6 +3660,23 @@ def testfunc(n):
36603660
self.assertLessEqual(count_ops(ex, "_POP_TOP_INT"), 1)
36613661
self.assertIn("_POP_TOP_NOP", uops)
36623662

3663+
def test_binary_subscr_list_slice(self):
3664+
def testfunc(n):
3665+
x = 0
3666+
for _ in range(n):
3667+
l = [1, 2, 3]
3668+
x += l[0:1][0]
3669+
return x
3670+
3671+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3672+
self.assertEqual(res, TIER2_THRESHOLD)
3673+
uops = get_opnames(ex)
3674+
3675+
self.assertIn("_BINARY_OP_SUBSCR_LIST_SLICE", uops)
3676+
self.assertNotIn("_GUARD_TOS_LIST", uops)
3677+
self.assertEqual(count_ops(ex, "_POP_TOP"), 3)
3678+
self.assertEqual(count_ops(ex, "_POP_TOP_NOP"), 4)
3679+
36633680
def test_is_op(self):
36643681
def test_is_false(n):
36653682
a = object()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize and eliminate ref-counting in ``_BINARY_OP_SUBSCR_LIST_SLICE``

Modules/_testinternalcapi/test_cases.c.h

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

Python/bytecodes.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,9 @@ dummy_func(
938938
}
939939

940940
macro(BINARY_OP_SUBSCR_LIST_SLICE) =
941-
_GUARD_TOS_SLICE + _GUARD_NOS_LIST + unused/5 + _BINARY_OP_SUBSCR_LIST_SLICE;
941+
_GUARD_TOS_SLICE + _GUARD_NOS_LIST + unused/5 + _BINARY_OP_SUBSCR_LIST_SLICE + POP_TOP + POP_TOP;
942942

943-
op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res)) {
943+
op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res, ls, ss)) {
944944
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
945945
PyObject *list = PyStackRef_AsPyObjectBorrow(list_st);
946946

@@ -949,9 +949,13 @@ dummy_func(
949949

950950
PyObject *res_o = _PyList_SliceSubscript(list, sub);
951951
STAT_INC(BINARY_OP, hit);
952-
DECREF_INPUTS();
953-
ERROR_IF(res_o == NULL);
952+
if (res_o == NULL) {
953+
ERROR_NO_POP();
954+
}
954955
res = PyStackRef_FromPyObjectSteal(res_o);
956+
ls = list_st;
957+
ss = sub_st;
958+
INPUTS_DEAD();
955959
}
956960

957961
macro(BINARY_OP_SUBSCR_STR_INT) =

Python/executor_cases.c.h

Lines changed: 10 additions & 16 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: 23 additions & 15 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ dummy_func(void) {
422422
ss = sub_st;
423423
}
424424

425+
op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res, ls, ss)) {
426+
res = sym_new_type(ctx, &PyList_Type);
427+
ls = list_st;
428+
ss = sub_st;
429+
}
430+
425431
op(_TO_BOOL, (value -- res)) {
426432
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
427433
if (!already_bool) {

0 commit comments

Comments
 (0)