Skip to content

Commit c18d831

Browse files
authored
gh-100188: Reduce misses in BINARY_SUBSCR_(LIST/TUPLE)_INT (#100189)
Don't specialize if the index is negative.
1 parent 44892d4 commit c18d831

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``BINARY_SUBSCR_LIST_INT`` and ``BINARY_SUBSCR_TUPLE_INT``
2+
instructions are no longer used for negative integers because
3+
those instructions always miss when encountering negative integers.

Python/specialize.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -1302,17 +1302,25 @@ _Py_Specialize_BinarySubscr(
13021302
PyTypeObject *container_type = Py_TYPE(container);
13031303
if (container_type == &PyList_Type) {
13041304
if (PyLong_CheckExact(sub)) {
1305-
_py_set_opcode(instr, BINARY_SUBSCR_LIST_INT);
1306-
goto success;
1305+
if (Py_SIZE(sub) == 0 || Py_SIZE(sub) == 1) {
1306+
_py_set_opcode(instr, BINARY_SUBSCR_LIST_INT);
1307+
goto success;
1308+
}
1309+
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
1310+
goto fail;
13071311
}
13081312
SPECIALIZATION_FAIL(BINARY_SUBSCR,
13091313
PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_LIST_SLICE : SPEC_FAIL_OTHER);
13101314
goto fail;
13111315
}
13121316
if (container_type == &PyTuple_Type) {
13131317
if (PyLong_CheckExact(sub)) {
1314-
_py_set_opcode(instr, BINARY_SUBSCR_TUPLE_INT);
1315-
goto success;
1318+
if (Py_SIZE(sub) == 0 || Py_SIZE(sub) == 1) {
1319+
_py_set_opcode(instr, BINARY_SUBSCR_TUPLE_INT);
1320+
goto success;
1321+
}
1322+
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
1323+
goto fail;
13161324
}
13171325
SPECIALIZATION_FAIL(BINARY_SUBSCR,
13181326
PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_TUPLE_SLICE : SPEC_FAIL_OTHER);

0 commit comments

Comments
 (0)