From dcfdae88f0b451f0035da6a55ce1bb63ae3846fc Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Sun, 6 Apr 2025 23:23:53 +0100 Subject: [PATCH 1/2] gh-100239: more stats for BINARY_OP/SUBSCR specialization --- Include/cpython/pystats.h | 2 +- Python/specialize.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Include/cpython/pystats.h b/Include/cpython/pystats.h index f16391f8437ec1..7c1459bde8f1cf 100644 --- a/Include/cpython/pystats.h +++ b/Include/cpython/pystats.h @@ -31,7 +31,7 @@ #define PYSTATS_MAX_UOP_ID 512 -#define SPECIALIZATION_FAILURE_KINDS 44 +#define SPECIALIZATION_FAILURE_KINDS 50 /* Stats for determining who is calling PyEval_EvalFrame */ #define EVAL_CALL_TOTAL 0 diff --git a/Python/specialize.c b/Python/specialize.c index f73f322f0c9725..1b5e6d1967c08b 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -604,6 +604,12 @@ _PyCode_Quicken(_Py_CODEUNIT *instructions, Py_ssize_t size, int enable_counters #define SPEC_FAIL_BINARY_OP_SUBSCR_DEQUE 42 #define SPEC_FAIL_BINARY_OP_SUBSCR_ENUMDICT 43 #define SPEC_FAIL_BINARY_OP_SUBSCR_STACKSUMMARY 44 +#define SPEC_FAIL_BINARY_OP_SUBSCR_DEFAULTDICT 45 +#define SPEC_FAIL_BINARY_OP_SUBSCR_COUNTER 46 +#define SPEC_FAIL_BINARY_OP_SUBSCR_ORDEREDDICT 47 +#define SPEC_FAIL_BINARY_OP_SUBSCR_BYTES 48 +#define SPEC_FAIL_BINARY_OP_SUBSCR_STRUCTTIME 49 +#define SPEC_FAIL_BINARY_OP_SUBSCR_RANGE 50 /* Calls */ @@ -2370,6 +2376,14 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs) return SPEC_FAIL_BINARY_OP_SUBSCR_MAPPINGPROXY; } + if (PyObject_TypeCheck(lhs, &PyBytes_Type)) { + return SPEC_FAIL_BINARY_OP_SUBSCR_BYTES; + } + + if (PyObject_TypeCheck(lhs, &PyRange_Type)) { + return SPEC_FAIL_BINARY_OP_SUBSCR_RANGE; + } + if (strcmp(container_type->tp_name, "array.array") == 0) { return SPEC_FAIL_BINARY_OP_SUBSCR_ARRAY; } @@ -2390,9 +2404,26 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs) return SPEC_FAIL_BINARY_OP_SUBSCR_STACKSUMMARY; } + if (strcmp(container_type->tp_name, "collections.defaultdict") == 0) { + return SPEC_FAIL_BINARY_OP_SUBSCR_DEFAULTDICT; + } + + if (strcmp(container_type->tp_name, "Counter") == 0) { + return SPEC_FAIL_BINARY_OP_SUBSCR_COUNTER; + } + + if (strcmp(container_type->tp_name, "collections.OrderedDict") == 0) { + return SPEC_FAIL_BINARY_OP_SUBSCR_ORDEREDDICT; + } + + if (strcmp(container_type->tp_name, "time.struct_time") == 0) { + return SPEC_FAIL_BINARY_OP_SUBSCR_STRUCTTIME; + } + if (PySlice_Check(rhs)) { return SPEC_FAIL_BINARY_OP_SUBSCR_OTHER_SLICE; } + return SPEC_FAIL_BINARY_OP_SUBSCR; } Py_UNREACHABLE(); From 9f3dec6c95c57e309b5c025b36dcbbb71a80b7b1 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 7 Apr 2025 16:32:57 +0100 Subject: [PATCH 2/2] whitespace --- Python/specialize.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/specialize.c b/Python/specialize.c index 1b5e6d1967c08b..498dcd3e483058 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -2423,7 +2423,6 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs) if (PySlice_Check(rhs)) { return SPEC_FAIL_BINARY_OP_SUBSCR_OTHER_SLICE; } - return SPEC_FAIL_BINARY_OP_SUBSCR; } Py_UNREACHABLE();