From 454c2668039df5ed947b7ffec4c923d35ea8e93d Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Mon, 31 Jan 2022 20:13:11 -0500 Subject: [PATCH 1/4] Add more stats for COMPARE_OP in specialize.c --- Python/specialize.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Python/specialize.c b/Python/specialize.c index aec94d9e60be41..6b1dec95898ce0 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -533,6 +533,14 @@ initial_counter_value(void) { #define SPEC_FAIL_STRING_COMPARE 13 #define SPEC_FAIL_NOT_FOLLOWED_BY_COND_JUMP 14 #define SPEC_FAIL_BIG_INT 15 +#define SPEC_FAIL_COMPARE_BYTES 16 +#define SPEC_FAIL_COMPARE_TUPLE 17 +#define SPEC_FAIL_COMPARE_LIST 18 +#define SPEC_FAIL_COMPARE_SET 19 +#define SPEC_FAIL_COMPARE_BOOL 20 +#define SPEC_FAIL_COMPARE_BASEOBJECT 21 +#define SPEC_FAIL_COMPARE_FLOAT_LONG 22 +#define SPEC_FAIL_COMPARE_LONG_FLOAT 23 static int specialize_module_load_attr( @@ -1764,6 +1772,16 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, when_to_jump_mask = (1 | 2 | 4) & ~when_to_jump_mask; } if (Py_TYPE(lhs) != Py_TYPE(rhs)) { +#ifdef Py_STATS + if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_FLOAT_LONG); + goto failure; + } + if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_LONG_FLOAT); + goto failure; + } +#endif SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_DIFFERENT_TYPES); goto failure; } @@ -1794,6 +1812,32 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, goto success; } } +#ifdef Py_STATS + if (PyBytes_CheckExact(lhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BYTES); + goto failure; + } + if (PyTuple_CheckExact(lhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_TUPLE); + goto failure; + } + if (PyList_CheckExact(lhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_LIST); + goto failure; + } + if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_SET); + goto failure; + } + if (PyBool_Check(lhs)) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BOOL); + goto failure; + } + if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) { + SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BASEOBJECT); + goto failure; + } +#endif SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_OTHER); failure: STAT_INC(COMPARE_OP, failure); From f698338ea10f6aa586dbe95abc0f7ae6458fefa1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 1 Feb 2022 01:17:30 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2022-02-01-01-17-28.bpo-45885.CjyNf_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-01-01-17-28.bpo-45885.CjyNf_.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-01-01-17-28.bpo-45885.CjyNf_.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-01-01-17-28.bpo-45885.CjyNf_.rst new file mode 100644 index 00000000000000..6395bd1f18d5e1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-01-01-17-28.bpo-45885.CjyNf_.rst @@ -0,0 +1 @@ +Added more fined-grained specialization failure stats regarding the ``COMPARE_OP`` bytecode. \ No newline at end of file From d45a06594f7c0aeec6267a8bf1674f544b376166 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Wed, 2 Feb 2022 11:14:38 -0500 Subject: [PATCH 3/4] Factor out a function --- Python/specialize.c | 76 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/Python/specialize.c b/Python/specialize.c index 6b1dec95898ce0..a1a60207749636 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1741,6 +1741,42 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, adaptive->counter = initial_counter_value(); } + +static int +compare_op_fail_kind(PyObject *lhs, PyObject *rhs) +{ +#ifdef Py_STATS + if (Py_TYPE(lhs) != Py_TYPE(rhs)) { + if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) { + return SPEC_FAIL_COMPARE_FLOAT_LONG; + } + if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) { + return SPEC_FAIL_COMPARE_LONG_FLOAT; + } + return SPEC_FAIL_DIFFERENT_TYPES; + } + if (PyBytes_CheckExact(lhs)) { + return SPEC_FAIL_COMPARE_BYTES; + } + if (PyTuple_CheckExact(lhs)) { + return SPEC_FAIL_COMPARE_TUPLE; + } + if (PyList_CheckExact(lhs)) { + return SPEC_FAIL_COMPARE_LIST; + } + if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) { + return SPEC_FAIL_COMPARE_SET; + } + if (PyBool_Check(lhs)) { + return SPEC_FAIL_COMPARE_BOOL; + } + if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) { + return SPEC_FAIL_COMPARE_BASEOBJECT; + } +#endif + return SPEC_FAIL_OTHER; +} + static int compare_masks[] = { // 1-bit: jump if less than // 2-bit: jump if equal @@ -1772,17 +1808,7 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, when_to_jump_mask = (1 | 2 | 4) & ~when_to_jump_mask; } if (Py_TYPE(lhs) != Py_TYPE(rhs)) { -#ifdef Py_STATS - if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_FLOAT_LONG); - goto failure; - } - if (PyLong_CheckExact(lhs) && PyFloat_CheckExact(rhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_LONG_FLOAT); - goto failure; - } -#endif - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_DIFFERENT_TYPES); + SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs)); goto failure; } if (PyFloat_CheckExact(lhs)) { @@ -1812,33 +1838,7 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, goto success; } } -#ifdef Py_STATS - if (PyBytes_CheckExact(lhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BYTES); - goto failure; - } - if (PyTuple_CheckExact(lhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_TUPLE); - goto failure; - } - if (PyList_CheckExact(lhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_LIST); - goto failure; - } - if (PySet_CheckExact(lhs) || PyFrozenSet_CheckExact(lhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_SET); - goto failure; - } - if (PyBool_Check(lhs)) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BOOL); - goto failure; - } - if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) { - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_BASEOBJECT); - goto failure; - } -#endif - SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_OTHER); + SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs)); failure: STAT_INC(COMPARE_OP, failure); cache_backoff(adaptive); From 0d7a8ed5ff556557d0e694c1f73ea975a3d1e3c7 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Wed, 2 Feb 2022 12:17:15 -0500 Subject: [PATCH 4/4] surround the whole function --- Python/specialize.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/specialize.c b/Python/specialize.c index a1a60207749636..b3793eead89352 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1742,10 +1742,10 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, } +#ifdef Py_STATS static int compare_op_fail_kind(PyObject *lhs, PyObject *rhs) { -#ifdef Py_STATS if (Py_TYPE(lhs) != Py_TYPE(rhs)) { if (PyFloat_CheckExact(lhs) && PyLong_CheckExact(rhs)) { return SPEC_FAIL_COMPARE_FLOAT_LONG; @@ -1773,9 +1773,10 @@ compare_op_fail_kind(PyObject *lhs, PyObject *rhs) if (Py_TYPE(lhs)->tp_richcompare == PyBaseObject_Type.tp_richcompare) { return SPEC_FAIL_COMPARE_BASEOBJECT; } -#endif return SPEC_FAIL_OTHER; } +#endif + static int compare_masks[] = { // 1-bit: jump if less than