Skip to content

Commit 674ab66

Browse files
authored
bpo-45885: Add more stats for COMPARE_OP in specialize.c (GH-31040)
1 parent e0433c1 commit 674ab66

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added more fined-grained specialization failure stats regarding the ``COMPARE_OP`` bytecode.

Python/specialize.c

+47-2
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ initial_counter_value(void) {
547547
#define SPEC_FAIL_STRING_COMPARE 13
548548
#define SPEC_FAIL_NOT_FOLLOWED_BY_COND_JUMP 14
549549
#define SPEC_FAIL_BIG_INT 15
550+
#define SPEC_FAIL_COMPARE_BYTES 16
551+
#define SPEC_FAIL_COMPARE_TUPLE 17
552+
#define SPEC_FAIL_COMPARE_LIST 18
553+
#define SPEC_FAIL_COMPARE_SET 19
554+
#define SPEC_FAIL_COMPARE_BOOL 20
555+
#define SPEC_FAIL_COMPARE_BASEOBJECT 21
556+
#define SPEC_FAIL_COMPARE_FLOAT_LONG 22
557+
#define SPEC_FAIL_COMPARE_LONG_FLOAT 23
550558

551559
/* FOR_ITER */
552560
#define SPEC_FAIL_ITER_GENERATOR 10
@@ -1764,6 +1772,43 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
17641772
adaptive->counter = initial_counter_value();
17651773
}
17661774

1775+
1776+
#ifdef Py_STATS
1777+
static int
1778+
compare_op_fail_kind(PyObject *lhs, PyObject *rhs)
1779+
{
1780+
if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
1781+
if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) {
1782+
return SPEC_FAIL_COMPARE_FLOAT_LONG;
1783+
}
1784+
if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) {
1785+
return SPEC_FAIL_COMPARE_LONG_FLOAT;
1786+
}
1787+
return SPEC_FAIL_DIFFERENT_TYPES;
1788+
}
1789+
if (PyBytes_CheckExact(lhs)) {
1790+
return SPEC_FAIL_COMPARE_BYTES;
1791+
}
1792+
if (PyTuple_CheckExact(lhs)) {
1793+
return SPEC_FAIL_COMPARE_TUPLE;
1794+
}
1795+
if (PyList_CheckExact(lhs)) {
1796+
return SPEC_FAIL_COMPARE_LIST;
1797+
}
1798+
if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) {
1799+
return SPEC_FAIL_COMPARE_SET;
1800+
}
1801+
if (PyBool_Check(lhs)) {
1802+
return SPEC_FAIL_COMPARE_BOOL;
1803+
}
1804+
if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) {
1805+
return SPEC_FAIL_COMPARE_BASEOBJECT;
1806+
}
1807+
return SPEC_FAIL_OTHER;
1808+
}
1809+
#endif
1810+
1811+
17671812
static int compare_masks[] = {
17681813
// 1-bit: jump if less than
17691814
// 2-bit: jump if equal
@@ -1795,7 +1840,7 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
17951840
when_to_jump_mask = (1 | 2 | 4) & ~when_to_jump_mask;
17961841
}
17971842
if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
1798-
SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_DIFFERENT_TYPES);
1843+
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
17991844
goto failure;
18001845
}
18011846
if (PyFloat_CheckExact(lhs)) {
@@ -1825,7 +1870,7 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
18251870
goto success;
18261871
}
18271872
}
1828-
SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_OTHER);
1873+
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
18291874
failure:
18301875
STAT_INC(COMPARE_OP, failure);
18311876
cache_backoff(adaptive);

0 commit comments

Comments
 (0)