Skip to content

Commit

Permalink
gh-100188: Reduce misses in BINARY_SUBSCR_(LIST/TUPLE)_INT (#100189)
Browse files Browse the repository at this point in the history
Don't specialize if the index is negative.
  • Loading branch information
sweeneyde authored Dec 20, 2022
1 parent 44892d4 commit c18d831
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ``BINARY_SUBSCR_LIST_INT`` and ``BINARY_SUBSCR_TUPLE_INT``
instructions are no longer used for negative integers because
those instructions always miss when encountering negative integers.
16 changes: 12 additions & 4 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,17 +1302,25 @@ _Py_Specialize_BinarySubscr(
PyTypeObject *container_type = Py_TYPE(container);
if (container_type == &PyList_Type) {
if (PyLong_CheckExact(sub)) {
_py_set_opcode(instr, BINARY_SUBSCR_LIST_INT);
goto success;
if (Py_SIZE(sub) == 0 || Py_SIZE(sub) == 1) {
_py_set_opcode(instr, BINARY_SUBSCR_LIST_INT);
goto success;
}
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
}
SPECIALIZATION_FAIL(BINARY_SUBSCR,
PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_LIST_SLICE : SPEC_FAIL_OTHER);
goto fail;
}
if (container_type == &PyTuple_Type) {
if (PyLong_CheckExact(sub)) {
_py_set_opcode(instr, BINARY_SUBSCR_TUPLE_INT);
goto success;
if (Py_SIZE(sub) == 0 || Py_SIZE(sub) == 1) {
_py_set_opcode(instr, BINARY_SUBSCR_TUPLE_INT);
goto success;
}
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_RANGE);
goto fail;
}
SPECIALIZATION_FAIL(BINARY_SUBSCR,
PySlice_Check(sub) ? SPEC_FAIL_SUBSCR_TUPLE_SLICE : SPEC_FAIL_OTHER);
Expand Down

0 comments on commit c18d831

Please sign in to comment.