Skip to content

Commit a86342d

Browse files
committed
Optimize BINARY_OP_SUBSCR_LIST_SLICE
1 parent 704b915 commit a86342d

File tree

10 files changed

+109
-63
lines changed

10 files changed

+109
-63
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,6 +3660,22 @@ 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+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3671+
self.assertEqual(res, TIER2_THRESHOLD)
3672+
uops = get_opnames(ex)
3673+
3674+
self.assertIn("_BINARY_OP_SUBSCR_LIST_SLICE", uops)
3675+
self.assertNotIn("_GUARD_TOS_LIST", uops)
3676+
self.assertEqual(count_ops(ex, "_POP_TOP"), 3)
3677+
self.assertEqual(count_ops(ex, "_POP_TOP_NOP"), 4)
3678+
36633679
def test_is_op(self):
36643680
def test_is_false(n):
36653681
a = object()

Modules/_testinternalcapi/test_cases.c.h

Lines changed: 24 additions & 16 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: 11 additions & 17 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: 24 additions & 16 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) {

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)