From 9a45232fad065f25866676f066646b2767164848 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:26:38 +0800 Subject: [PATCH 1/3] Remove bad specializations, add fail stats --- Include/internal/pycore_descrobject.h | 2 + Include/internal/pycore_list.h | 2 - Include/internal/pycore_opcode_metadata.h | 30 +++------ Include/internal/pycore_tuple.h | 2 - Include/internal/pycore_uop_ids.h | 4 +- Include/internal/pycore_uop_metadata.h | 8 +-- Include/opcode_ids.h | 76 +++++++++++---------- Lib/_opcode_metadata.py | 80 +++++++++++------------ Objects/descrobject.c | 6 +- Objects/listobject.c | 6 +- Objects/tupleobject.c | 6 +- Python/bytecodes.c | 32 +++------ Python/ceval.c | 1 + Python/executor_cases.c.h | 48 +++----------- Python/generated_cases.c.h | 58 +++------------- Python/opcode_targets.h | 6 +- Python/optimizer_cases.c.h | 20 +----- Python/specialize.c | 44 +++++++++---- Tools/jit/template.c | 1 + 19 files changed, 160 insertions(+), 272 deletions(-) diff --git a/Include/internal/pycore_descrobject.h b/Include/internal/pycore_descrobject.h index 3cec59a68a3d2b..369d3eaaca4dd6 100644 --- a/Include/internal/pycore_descrobject.h +++ b/Include/internal/pycore_descrobject.h @@ -22,6 +22,8 @@ typedef propertyobject _PyPropertyObject; extern PyTypeObject _PyMethodWrapper_Type; +PyAPI_FUNC(int) _PyMappingProxy_Contains(PyObject *self, PyObject *key); + #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_list.h b/Include/internal/pycore_list.h index 92cab731bc253b..2a82912e41d557 100644 --- a/Include/internal/pycore_list.h +++ b/Include/internal/pycore_list.h @@ -56,8 +56,6 @@ typedef struct { PyAPI_FUNC(PyObject *)_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n); -PyAPI_FUNC(int) _PyList_Contains(PyObject *aa, PyObject *el); - #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 4c691d6a922862..98977134abc103 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -153,14 +153,10 @@ int _PyOpcode_num_popped(int opcode, int oparg) { return 2; case CONTAINS_OP_DICT: return 2; - case CONTAINS_OP_LIST: + case CONTAINS_OP_MAPPINGPROXY: return 2; case CONTAINS_OP_SET: return 2; - case CONTAINS_OP_STR: - return 2; - case CONTAINS_OP_TUPLE: - return 2; case CONVERT_VALUE: return 1; case COPY: @@ -588,14 +584,10 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { return 1; case CONTAINS_OP_DICT: return 1; - case CONTAINS_OP_LIST: + case CONTAINS_OP_MAPPINGPROXY: return 1; case CONTAINS_OP_SET: return 1; - case CONTAINS_OP_STR: - return 1; - case CONTAINS_OP_TUPLE: - return 1; case CONVERT_VALUE: return 1; case COPY: @@ -1024,10 +1016,8 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[268] = { [COMPARE_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_DICT] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_LIST] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, + [CONTAINS_OP_MAPPINGPROXY] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_SET] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_TUPLE] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONVERT_VALUE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_FLAG }, [COPY] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_PURE_FLAG }, [COPY_FREE_VARS] = { true, INSTR_FMT_IB, HAS_ARG_FLAG }, @@ -1251,10 +1241,8 @@ _PyOpcode_macro_expansion[256] = { [COMPARE_OP_STR] = { .nuops = 2, .uops = { { _GUARD_BOTH_UNICODE, 0, 0 }, { _COMPARE_OP_STR, 0, 0 } } }, [CONTAINS_OP] = { .nuops = 1, .uops = { { _CONTAINS_OP, 0, 0 } } }, [CONTAINS_OP_DICT] = { .nuops = 1, .uops = { { _CONTAINS_OP_DICT, 0, 0 } } }, - [CONTAINS_OP_LIST] = { .nuops = 1, .uops = { { _CONTAINS_OP_LIST, 0, 0 } } }, + [CONTAINS_OP_MAPPINGPROXY] = { .nuops = 1, .uops = { { _CONTAINS_OP_MAPPINGPROXY, 0, 0 } } }, [CONTAINS_OP_SET] = { .nuops = 1, .uops = { { _CONTAINS_OP_SET, 0, 0 } } }, - [CONTAINS_OP_STR] = { .nuops = 1, .uops = { { _CONTAINS_OP_STR, 0, 0 } } }, - [CONTAINS_OP_TUPLE] = { .nuops = 1, .uops = { { _CONTAINS_OP_TUPLE, 0, 0 } } }, [CONVERT_VALUE] = { .nuops = 1, .uops = { { _CONVERT_VALUE, 0, 0 } } }, [COPY] = { .nuops = 1, .uops = { { _COPY, 0, 0 } } }, [COPY_FREE_VARS] = { .nuops = 1, .uops = { { _COPY_FREE_VARS, 0, 0 } } }, @@ -1429,10 +1417,8 @@ const char *_PyOpcode_OpName[268] = { [COMPARE_OP_STR] = "COMPARE_OP_STR", [CONTAINS_OP] = "CONTAINS_OP", [CONTAINS_OP_DICT] = "CONTAINS_OP_DICT", - [CONTAINS_OP_LIST] = "CONTAINS_OP_LIST", + [CONTAINS_OP_MAPPINGPROXY] = "CONTAINS_OP_MAPPINGPROXY", [CONTAINS_OP_SET] = "CONTAINS_OP_SET", - [CONTAINS_OP_STR] = "CONTAINS_OP_STR", - [CONTAINS_OP_TUPLE] = "CONTAINS_OP_TUPLE", [CONVERT_VALUE] = "CONVERT_VALUE", [COPY] = "COPY", [COPY_FREE_VARS] = "COPY_FREE_VARS", @@ -1685,10 +1671,8 @@ const uint8_t _PyOpcode_Deopt[256] = { [COMPARE_OP_STR] = COMPARE_OP, [CONTAINS_OP] = CONTAINS_OP, [CONTAINS_OP_DICT] = CONTAINS_OP, - [CONTAINS_OP_LIST] = CONTAINS_OP, + [CONTAINS_OP_MAPPINGPROXY] = CONTAINS_OP, [CONTAINS_OP_SET] = CONTAINS_OP, - [CONTAINS_OP_STR] = CONTAINS_OP, - [CONTAINS_OP_TUPLE] = CONTAINS_OP, [CONVERT_VALUE] = CONVERT_VALUE, [COPY] = COPY, [COPY_FREE_VARS] = COPY_FREE_VARS, @@ -1875,6 +1859,8 @@ const uint8_t _PyOpcode_Deopt[256] = { case 146: \ case 147: \ case 148: \ + case 222: \ + case 223: \ case 224: \ case 225: \ case 226: \ diff --git a/Include/internal/pycore_tuple.h b/Include/internal/pycore_tuple.h index edde40495021f1..14a9e42c3a324c 100644 --- a/Include/internal/pycore_tuple.h +++ b/Include/internal/pycore_tuple.h @@ -29,8 +29,6 @@ typedef struct { PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ } _PyTupleIterObject; -PyAPI_FUNC(int) _PyTuple_Contains(PyTupleObject *a, PyObject *el); - #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 5bf10ae6499e69..61fc5fecf7cb0d 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -76,10 +76,8 @@ extern "C" { #define _COMPARE_OP_STR 328 #define _CONTAINS_OP 329 #define _CONTAINS_OP_DICT CONTAINS_OP_DICT -#define _CONTAINS_OP_LIST CONTAINS_OP_LIST +#define _CONTAINS_OP_MAPPINGPROXY CONTAINS_OP_MAPPINGPROXY #define _CONTAINS_OP_SET CONTAINS_OP_SET -#define _CONTAINS_OP_STR CONTAINS_OP_STR -#define _CONTAINS_OP_TUPLE CONTAINS_OP_TUPLE #define _CONVERT_VALUE CONVERT_VALUE #define _COPY COPY #define _COPY_FREE_VARS COPY_FREE_VARS diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index 23aead27503a7f..d5f27ccbafb185 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -154,11 +154,9 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = { [_COMPARE_OP_STR] = HAS_ARG_FLAG | HAS_ESCAPES_FLAG, [_IS_OP] = HAS_ARG_FLAG, [_CONTAINS_OP] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_LIST] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_SET] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_TUPLE] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_DICT] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_STR] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, + [_CONTAINS_OP_MAPPINGPROXY] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EG_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EXC_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_IS_NONE] = 0, @@ -314,10 +312,8 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = { [_COMPARE_OP_STR] = "_COMPARE_OP_STR", [_CONTAINS_OP] = "_CONTAINS_OP", [_CONTAINS_OP_DICT] = "_CONTAINS_OP_DICT", - [_CONTAINS_OP_LIST] = "_CONTAINS_OP_LIST", + [_CONTAINS_OP_MAPPINGPROXY] = "_CONTAINS_OP_MAPPINGPROXY", [_CONTAINS_OP_SET] = "_CONTAINS_OP_SET", - [_CONTAINS_OP_STR] = "_CONTAINS_OP_STR", - [_CONTAINS_OP_TUPLE] = "_CONTAINS_OP_TUPLE", [_CONVERT_VALUE] = "_CONVERT_VALUE", [_COPY] = "_COPY", [_COPY_FREE_VARS] = "_COPY_FREE_VARS", diff --git a/Include/opcode_ids.h b/Include/opcode_ids.h index b76ad8b2bf0291..ce65ac74b74959 100644 --- a/Include/opcode_ids.h +++ b/Include/opcode_ids.h @@ -164,46 +164,44 @@ extern "C" { #define COMPARE_OP_INT 181 #define COMPARE_OP_STR 182 #define CONTAINS_OP_DICT 183 -#define CONTAINS_OP_LIST 184 +#define CONTAINS_OP_MAPPINGPROXY 184 #define CONTAINS_OP_SET 185 -#define CONTAINS_OP_STR 186 -#define CONTAINS_OP_TUPLE 187 -#define FOR_ITER_GEN 188 -#define FOR_ITER_LIST 189 -#define FOR_ITER_RANGE 190 -#define FOR_ITER_TUPLE 191 -#define LOAD_ATTR_CLASS 192 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 193 -#define LOAD_ATTR_INSTANCE_VALUE 194 -#define LOAD_ATTR_METHOD_LAZY_DICT 195 -#define LOAD_ATTR_METHOD_NO_DICT 196 -#define LOAD_ATTR_METHOD_WITH_VALUES 197 -#define LOAD_ATTR_MODULE 198 -#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 199 -#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 200 -#define LOAD_ATTR_PROPERTY 201 -#define LOAD_ATTR_SLOT 202 -#define LOAD_ATTR_WITH_HINT 203 -#define LOAD_GLOBAL_BUILTIN 204 -#define LOAD_GLOBAL_MODULE 205 -#define LOAD_SUPER_ATTR_ATTR 206 -#define LOAD_SUPER_ATTR_METHOD 207 -#define RESUME_CHECK 208 -#define SEND_GEN 209 -#define STORE_ATTR_INSTANCE_VALUE 210 -#define STORE_ATTR_SLOT 211 -#define STORE_ATTR_WITH_HINT 212 -#define STORE_SUBSCR_DICT 213 -#define STORE_SUBSCR_LIST_INT 214 -#define TO_BOOL_ALWAYS_TRUE 215 -#define TO_BOOL_BOOL 216 -#define TO_BOOL_INT 217 -#define TO_BOOL_LIST 218 -#define TO_BOOL_NONE 219 -#define TO_BOOL_STR 220 -#define UNPACK_SEQUENCE_LIST 221 -#define UNPACK_SEQUENCE_TUPLE 222 -#define UNPACK_SEQUENCE_TWO_TUPLE 223 +#define FOR_ITER_GEN 186 +#define FOR_ITER_LIST 187 +#define FOR_ITER_RANGE 188 +#define FOR_ITER_TUPLE 189 +#define LOAD_ATTR_CLASS 190 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 191 +#define LOAD_ATTR_INSTANCE_VALUE 192 +#define LOAD_ATTR_METHOD_LAZY_DICT 193 +#define LOAD_ATTR_METHOD_NO_DICT 194 +#define LOAD_ATTR_METHOD_WITH_VALUES 195 +#define LOAD_ATTR_MODULE 196 +#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 197 +#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 198 +#define LOAD_ATTR_PROPERTY 199 +#define LOAD_ATTR_SLOT 200 +#define LOAD_ATTR_WITH_HINT 201 +#define LOAD_GLOBAL_BUILTIN 202 +#define LOAD_GLOBAL_MODULE 203 +#define LOAD_SUPER_ATTR_ATTR 204 +#define LOAD_SUPER_ATTR_METHOD 205 +#define RESUME_CHECK 206 +#define SEND_GEN 207 +#define STORE_ATTR_INSTANCE_VALUE 208 +#define STORE_ATTR_SLOT 209 +#define STORE_ATTR_WITH_HINT 210 +#define STORE_SUBSCR_DICT 211 +#define STORE_SUBSCR_LIST_INT 212 +#define TO_BOOL_ALWAYS_TRUE 213 +#define TO_BOOL_BOOL 214 +#define TO_BOOL_INT 215 +#define TO_BOOL_LIST 216 +#define TO_BOOL_NONE 217 +#define TO_BOOL_STR 218 +#define UNPACK_SEQUENCE_LIST 219 +#define UNPACK_SEQUENCE_TUPLE 220 +#define UNPACK_SEQUENCE_TWO_TUPLE 221 #define INSTRUMENTED_RESUME 236 #define INSTRUMENTED_END_FOR 237 #define INSTRUMENTED_END_SEND 238 diff --git a/Lib/_opcode_metadata.py b/Lib/_opcode_metadata.py index 90dfaea46ca0d6..3dc3ccb0138a11 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -76,11 +76,9 @@ "COMPARE_OP_STR", ], "CONTAINS_OP": [ - "CONTAINS_OP_LIST", "CONTAINS_OP_SET", - "CONTAINS_OP_TUPLE", "CONTAINS_OP_DICT", - "CONTAINS_OP_STR", + "CONTAINS_OP_MAPPINGPROXY", ], "FOR_ITER": [ "FOR_ITER_LIST", @@ -146,46 +144,44 @@ 'COMPARE_OP_INT': 181, 'COMPARE_OP_STR': 182, 'CONTAINS_OP_DICT': 183, - 'CONTAINS_OP_LIST': 184, + 'CONTAINS_OP_MAPPINGPROXY': 184, 'CONTAINS_OP_SET': 185, - 'CONTAINS_OP_STR': 186, - 'CONTAINS_OP_TUPLE': 187, - 'FOR_ITER_GEN': 188, - 'FOR_ITER_LIST': 189, - 'FOR_ITER_RANGE': 190, - 'FOR_ITER_TUPLE': 191, - 'LOAD_ATTR_CLASS': 192, - 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 193, - 'LOAD_ATTR_INSTANCE_VALUE': 194, - 'LOAD_ATTR_METHOD_LAZY_DICT': 195, - 'LOAD_ATTR_METHOD_NO_DICT': 196, - 'LOAD_ATTR_METHOD_WITH_VALUES': 197, - 'LOAD_ATTR_MODULE': 198, - 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 199, - 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 200, - 'LOAD_ATTR_PROPERTY': 201, - 'LOAD_ATTR_SLOT': 202, - 'LOAD_ATTR_WITH_HINT': 203, - 'LOAD_GLOBAL_BUILTIN': 204, - 'LOAD_GLOBAL_MODULE': 205, - 'LOAD_SUPER_ATTR_ATTR': 206, - 'LOAD_SUPER_ATTR_METHOD': 207, - 'RESUME_CHECK': 208, - 'SEND_GEN': 209, - 'STORE_ATTR_INSTANCE_VALUE': 210, - 'STORE_ATTR_SLOT': 211, - 'STORE_ATTR_WITH_HINT': 212, - 'STORE_SUBSCR_DICT': 213, - 'STORE_SUBSCR_LIST_INT': 214, - 'TO_BOOL_ALWAYS_TRUE': 215, - 'TO_BOOL_BOOL': 216, - 'TO_BOOL_INT': 217, - 'TO_BOOL_LIST': 218, - 'TO_BOOL_NONE': 219, - 'TO_BOOL_STR': 220, - 'UNPACK_SEQUENCE_LIST': 221, - 'UNPACK_SEQUENCE_TUPLE': 222, - 'UNPACK_SEQUENCE_TWO_TUPLE': 223, + 'FOR_ITER_GEN': 186, + 'FOR_ITER_LIST': 187, + 'FOR_ITER_RANGE': 188, + 'FOR_ITER_TUPLE': 189, + 'LOAD_ATTR_CLASS': 190, + 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 191, + 'LOAD_ATTR_INSTANCE_VALUE': 192, + 'LOAD_ATTR_METHOD_LAZY_DICT': 193, + 'LOAD_ATTR_METHOD_NO_DICT': 194, + 'LOAD_ATTR_METHOD_WITH_VALUES': 195, + 'LOAD_ATTR_MODULE': 196, + 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 197, + 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 198, + 'LOAD_ATTR_PROPERTY': 199, + 'LOAD_ATTR_SLOT': 200, + 'LOAD_ATTR_WITH_HINT': 201, + 'LOAD_GLOBAL_BUILTIN': 202, + 'LOAD_GLOBAL_MODULE': 203, + 'LOAD_SUPER_ATTR_ATTR': 204, + 'LOAD_SUPER_ATTR_METHOD': 205, + 'RESUME_CHECK': 206, + 'SEND_GEN': 207, + 'STORE_ATTR_INSTANCE_VALUE': 208, + 'STORE_ATTR_SLOT': 209, + 'STORE_ATTR_WITH_HINT': 210, + 'STORE_SUBSCR_DICT': 211, + 'STORE_SUBSCR_LIST_INT': 212, + 'TO_BOOL_ALWAYS_TRUE': 213, + 'TO_BOOL_BOOL': 214, + 'TO_BOOL_INT': 215, + 'TO_BOOL_LIST': 216, + 'TO_BOOL_NONE': 217, + 'TO_BOOL_STR': 218, + 'UNPACK_SEQUENCE_LIST': 219, + 'UNPACK_SEQUENCE_TUPLE': 220, + 'UNPACK_SEQUENCE_TWO_TUPLE': 221, } opmap = { diff --git a/Objects/descrobject.c b/Objects/descrobject.c index df546a090c28e4..4074c94054deec 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1083,8 +1083,8 @@ static PyNumberMethods mappingproxy_as_number = { .nb_inplace_or = mappingproxy_ior, }; -static int -mappingproxy_contains(PyObject *self, PyObject *key) +int +_PyMappingProxy_Contains(PyObject *self, PyObject *key) { mappingproxyobject *pp = (mappingproxyobject *)self; if (PyDict_CheckExact(pp->mapping)) @@ -1101,7 +1101,7 @@ static PySequenceMethods mappingproxy_as_sequence = { 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ - mappingproxy_contains, /* sq_contains */ + _PyMappingProxy_Contains, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; diff --git a/Objects/listobject.c b/Objects/listobject.c index db8eedf18c556e..e013383a712bc8 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -524,8 +524,8 @@ list_length(PyObject *a) return PyList_GET_SIZE(a); } -int -_PyList_Contains(PyObject *aa, PyObject *el) +static int +list_contains(PyObject *aa, PyObject *el) { for (Py_ssize_t i = 0; ; i++) { @@ -3147,7 +3147,7 @@ static PySequenceMethods list_as_sequence = { 0, /* sq_slice */ list_ass_item, /* sq_ass_item */ 0, /* sq_ass_slice */ - _PyList_Contains, /* sq_contains */ + list_contains, /* sq_contains */ list_inplace_concat, /* sq_inplace_concat */ list_inplace_repeat, /* sq_inplace_repeat */ }; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 5bbc0279c21d8d..d9dc00da368a84 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -349,8 +349,8 @@ tuplelength(PyTupleObject *a) return Py_SIZE(a); } -int -_PyTuple_Contains(PyTupleObject *a, PyObject *el) +static int +tuplecontains(PyTupleObject *a, PyObject *el) { Py_ssize_t i; int cmp; @@ -758,7 +758,7 @@ static PySequenceMethods tuple_as_sequence = { 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ - (objobjproc)_PyTuple_Contains, /* sq_contains */ + (objobjproc)tuplecontains, /* sq_contains */ }; static PyObject* diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 3276a4a9644b8f..db73ac07bcc9f7 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2238,11 +2238,9 @@ dummy_func( } family(CONTAINS_OP, INLINE_CACHE_ENTRIES_CONTAINS_OP) = { - CONTAINS_OP_LIST, CONTAINS_OP_SET, - CONTAINS_OP_TUPLE, CONTAINS_OP_DICT, - CONTAINS_OP_STR, + CONTAINS_OP_MAPPINGPROXY, }; op(_CONTAINS_OP, (left, right -- b)) { @@ -2266,41 +2264,29 @@ dummy_func( macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP; - inst(CONTAINS_OP_LIST, (unused/1, left, right -- b)) { - DEOPT_IF(!PyList_CheckExact(right)); - int res = _PyList_Contains(right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CONTAINS_OP_SET, (unused/1, left, right -- b)) { - DEOPT_IF(!PySet_CheckExact(right)); + DEOPT_IF(!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right))); + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; } - inst(CONTAINS_OP_TUPLE, (unused/1, left, right -- b)) { - DEOPT_IF(!PyTuple_CheckExact(right)); - int res = _PyTuple_Contains((PyTupleObject *)right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CONTAINS_OP_DICT, (unused/1, left, right -- b)) { DEOPT_IF(!PyDict_CheckExact(right)); + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; } - inst(CONTAINS_OP_STR, (unused/1, left, right -- b)) { - DEOPT_IF(!PyUnicode_CheckExact(right)); - int res = PyUnicode_Contains(right, left); + inst(CONTAINS_OP_MAPPINGPROXY, (unused/1, left, right -- b)) { + DEOPT_IF(Py_TYPE(right) != &PyDictProxy_Type); + STAT_INC(CONTAINS_OP, hit); + int res = _PyMappingProxy_Contains(right, left); DECREF_INPUTS(); ERROR_IF(res < 0, error); b = (res ^ oparg) ? Py_True : Py_False; diff --git a/Python/ceval.c b/Python/ceval.c index f817f288903694..f65502c37428b0 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -28,6 +28,7 @@ #include "pycore_typeobject.h" // _PySuper_Lookup() #include "pycore_uop_ids.h" // Uops #include "pycore_pyerrors.h" +#include "pycore_descrobject.h" #include "pycore_dict.h" #include "dictobject.h" diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 2e7b970b4ddb9e..48c88ce72dbb3a 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2189,24 +2189,6 @@ break; } - case _CONTAINS_OP_LIST: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (!PyList_CheckExact(right)) goto deoptimize; - int res = _PyList_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_SET: { PyObject *right; PyObject *left; @@ -2214,7 +2196,9 @@ oparg = CURRENT_OPARG(); right = stack_pointer[-1]; left = stack_pointer[-2]; - if (!PySet_CheckExact(right)) goto deoptimize; + if (!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right))) goto deoptimize; + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); Py_DECREF(left); Py_DECREF(right); @@ -2225,24 +2209,6 @@ break; } - case _CONTAINS_OP_TUPLE: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (!PyTuple_CheckExact(right)) goto deoptimize; - int res = _PyTuple_Contains((PyTupleObject *)right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_DICT: { PyObject *right; PyObject *left; @@ -2251,6 +2217,7 @@ right = stack_pointer[-1]; left = stack_pointer[-2]; if (!PyDict_CheckExact(right)) goto deoptimize; + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); Py_DECREF(left); Py_DECREF(right); @@ -2261,15 +2228,16 @@ break; } - case _CONTAINS_OP_STR: { + case _CONTAINS_OP_MAPPINGPROXY: { PyObject *right; PyObject *left; PyObject *b; oparg = CURRENT_OPARG(); right = stack_pointer[-1]; left = stack_pointer[-2]; - if (!PyUnicode_CheckExact(right)) goto deoptimize; - int res = PyUnicode_Contains(right, left); + if (Py_TYPE(right) != &PyDictProxy_Type) goto deoptimize; + STAT_INC(CONTAINS_OP, hit); + int res = _PyMappingProxy_Contains(right, left); Py_DECREF(left); Py_DECREF(right); if (res < 0) goto pop_2_error_tier_two; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 54c48617fd4df5..67da86bf2d2d2c 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2176,6 +2176,7 @@ right = stack_pointer[-1]; left = stack_pointer[-2]; DEOPT_IF(!PyDict_CheckExact(right), CONTAINS_OP); + STAT_INC(CONTAINS_OP, hit); int res = PyDict_Contains(right, left); Py_DECREF(left); Py_DECREF(right); @@ -2186,10 +2187,10 @@ DISPATCH(); } - TARGET(CONTAINS_OP_LIST) { + TARGET(CONTAINS_OP_MAPPINGPROXY) { frame->instr_ptr = next_instr; next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_LIST); + INSTRUCTION_STATS(CONTAINS_OP_MAPPINGPROXY); static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); PyObject *right; PyObject *left; @@ -2197,8 +2198,9 @@ /* Skip 1 cache entry */ right = stack_pointer[-1]; left = stack_pointer[-2]; - DEOPT_IF(!PyList_CheckExact(right), CONTAINS_OP); - int res = _PyList_Contains(right, left); + DEOPT_IF(Py_TYPE(right) != &PyDictProxy_Type, CONTAINS_OP); + STAT_INC(CONTAINS_OP, hit); + int res = _PyMappingProxy_Contains(right, left); Py_DECREF(left); Py_DECREF(right); if (res < 0) goto pop_2_error; @@ -2219,7 +2221,9 @@ /* Skip 1 cache entry */ right = stack_pointer[-1]; left = stack_pointer[-2]; - DEOPT_IF(!PySet_CheckExact(right), CONTAINS_OP); + DEOPT_IF(!(PySet_CheckExact(right) || PyFrozenSet_CheckExact(right)), CONTAINS_OP); + STAT_INC(CONTAINS_OP, hit); + // Note: both set and frozenset use the same seq_contains method! int res = _PySet_Contains((PySetObject *)right, left); Py_DECREF(left); Py_DECREF(right); @@ -2230,50 +2234,6 @@ DISPATCH(); } - TARGET(CONTAINS_OP_STR) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_STR); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(!PyUnicode_CheckExact(right), CONTAINS_OP); - int res = PyUnicode_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - - TARGET(CONTAINS_OP_TUPLE) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_TUPLE); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(!PyTuple_CheckExact(right), CONTAINS_OP); - int res = _PyTuple_Contains((PyTupleObject *)right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - TARGET(CONVERT_VALUE) { frame->instr_ptr = next_instr; next_instr += 1; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 6b3846d46296ad..ab83199dbd2f8f 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -183,10 +183,8 @@ static void *opcode_targets[256] = { &&TARGET_COMPARE_OP_INT, &&TARGET_COMPARE_OP_STR, &&TARGET_CONTAINS_OP_DICT, - &&TARGET_CONTAINS_OP_LIST, + &&TARGET_CONTAINS_OP_MAPPINGPROXY, &&TARGET_CONTAINS_OP_SET, - &&TARGET_CONTAINS_OP_STR, - &&TARGET_CONTAINS_OP_TUPLE, &&TARGET_FOR_ITER_GEN, &&TARGET_FOR_ITER_LIST, &&TARGET_FOR_ITER_RANGE, @@ -235,6 +233,8 @@ static void *opcode_targets[256] = { &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, + &&_unknown_opcode, + &&_unknown_opcode, &&TARGET_INSTRUMENTED_RESUME, &&TARGET_INSTRUMENTED_END_FOR, &&TARGET_INSTRUMENTED_END_SEND, diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index a19010cc403681..5b20437ba95678 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1206,15 +1206,6 @@ break; } - case _CONTAINS_OP_LIST: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_SET: { _Py_UopsSymbol *b; b = sym_new_unknown(ctx); @@ -1224,15 +1215,6 @@ break; } - case _CONTAINS_OP_TUPLE: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CONTAINS_OP_DICT: { _Py_UopsSymbol *b; b = sym_new_unknown(ctx); @@ -1242,7 +1224,7 @@ break; } - case _CONTAINS_OP_STR: { + case _CONTAINS_OP_MAPPINGPROXY: { _Py_UopsSymbol *b; b = sym_new_unknown(ctx); if (b == NULL) goto out_of_space; diff --git a/Python/specialize.c b/Python/specialize.c index 5d339b8a476cf5..464bfcf7ac1a99 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -600,6 +600,12 @@ _PyCode_Quicken(PyCodeObject *code) #define SPEC_FAIL_TO_BOOL_SET 17 #define SPEC_FAIL_TO_BOOL_TUPLE 18 +// CONTAINS_OP +#define SPEC_FAIL_CONTAINS_OP_STR 9 +#define SPEC_FAIL_CONTAINS_OP_TUPLE 10 +#define SPEC_FAIL_CONTAINS_OP_LIST 11 +#define SPEC_FAIL_CONTAINS_OP_USER_CLASS 12 + static int function_kind(PyCodeObject *code); static bool function_check_args(PyObject *o, int expected_argcount, int opcode); static uint32_t function_get_version(PyObject *o, int opcode); @@ -2568,28 +2574,40 @@ _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr) assert(ENABLE_SPECIALIZATION); assert(_PyOpcode_Caches[CONTAINS_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP); _PyContainsOpCache *cache = (_PyContainsOpCache *)(instr + 1); - if (PyUnicode_CheckExact(value)) { - instr->op.code = CONTAINS_OP_STR; - goto success; - } - if (PyList_CheckExact(value)) { - instr->op.code = CONTAINS_OP_LIST; - goto success; - } - if (PyTuple_CheckExact(value)) { - instr->op.code = CONTAINS_OP_TUPLE; - goto success; - } if (PyDict_CheckExact(value)) { instr->op.code = CONTAINS_OP_DICT; goto success; } - if (PySet_CheckExact(value)) { + if (PySet_CheckExact(value) || PyFrozenSet_CheckExact(value)) { instr->op.code = CONTAINS_OP_SET; goto success; } + if (Py_TYPE(value) == &PyDictProxy_Type) { + instr->op.code = CONTAINS_OP_MAPPINGPROXY; + goto success; + } +#ifdef Py_STATS + if (PyUnicode_CheckExact(value)) { + SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_STR); + goto failure; + } + if (PyList_CheckExact(value)) { + SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_LIST); + goto failure; + } + if (PyTuple_CheckExact(value)) { + SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_TUPLE); + goto failure; + } + if (PyType_Check(value)) { + SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_USER_CLASS); + goto failure; + } + SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_OTHER); +#endif // Py_STATS +failure: STAT_INC(CONTAINS_OP, failure); instr->op.code = CONTAINS_OP; cache->counter = adaptive_counter_backoff(cache->counter); diff --git a/Tools/jit/template.c b/Tools/jit/template.c index 8aaf4581de362d..504e6c875525ae 100644 --- a/Tools/jit/template.c +++ b/Tools/jit/template.c @@ -13,6 +13,7 @@ #include "pycore_range.h" #include "pycore_setobject.h" #include "pycore_sliceobject.h" +#include "pycore_descrobject.h" #include "ceval_macros.h" From b0b4a8d7057060ef1f082de01adaf750a620c322 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:31:53 +0800 Subject: [PATCH 2/3] fix jit build --- Python/jit.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/jit.c b/Python/jit.c index dae25166b1f106..83fb3e3bf3ab41 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -15,6 +15,7 @@ #include "pycore_setobject.h" #include "pycore_sliceobject.h" #include "pycore_jit.h" +#include "pycore_descrobject.h" #include "jit_stencils.h" From 56cf6b93ab2a9441db61d6f5e072841150af02c3 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:58:20 +0800 Subject: [PATCH 3/3] Address review --- Include/internal/pycore_descrobject.h | 2 - Include/internal/pycore_opcode_metadata.h | 9 +-- Include/internal/pycore_uop_ids.h | 1 - Include/internal/pycore_uop_metadata.h | 2 - Include/opcode_ids.h | 75 +++++++++++----------- Lib/_opcode_metadata.py | 76 +++++++++++------------ Objects/descrobject.c | 6 +- Python/bytecodes.c | 10 --- Python/ceval.c | 1 - Python/executor_cases.c.h | 19 ------ Python/generated_cases.c.h | 23 ------- Python/jit.c | 1 - Python/opcode_targets.h | 2 +- Python/optimizer_cases.c.h | 9 --- Python/specialize.c | 44 ++++++------- 15 files changed, 98 insertions(+), 182 deletions(-) diff --git a/Include/internal/pycore_descrobject.h b/Include/internal/pycore_descrobject.h index 369d3eaaca4dd6..3cec59a68a3d2b 100644 --- a/Include/internal/pycore_descrobject.h +++ b/Include/internal/pycore_descrobject.h @@ -22,8 +22,6 @@ typedef propertyobject _PyPropertyObject; extern PyTypeObject _PyMethodWrapper_Type; -PyAPI_FUNC(int) _PyMappingProxy_Contains(PyObject *self, PyObject *key); - #ifdef __cplusplus } #endif diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h index 98977134abc103..efb731f1863283 100644 --- a/Include/internal/pycore_opcode_metadata.h +++ b/Include/internal/pycore_opcode_metadata.h @@ -153,8 +153,6 @@ int _PyOpcode_num_popped(int opcode, int oparg) { return 2; case CONTAINS_OP_DICT: return 2; - case CONTAINS_OP_MAPPINGPROXY: - return 2; case CONTAINS_OP_SET: return 2; case CONVERT_VALUE: @@ -584,8 +582,6 @@ int _PyOpcode_num_pushed(int opcode, int oparg) { return 1; case CONTAINS_OP_DICT: return 1; - case CONTAINS_OP_MAPPINGPROXY: - return 1; case CONTAINS_OP_SET: return 1; case CONVERT_VALUE: @@ -1016,7 +1012,6 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[268] = { [COMPARE_OP_STR] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_EXIT_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_DICT] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, - [CONTAINS_OP_MAPPINGPROXY] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONTAINS_OP_SET] = { true, INSTR_FMT_IBC, HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG }, [CONVERT_VALUE] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_ERROR_FLAG }, [COPY] = { true, INSTR_FMT_IB, HAS_ARG_FLAG | HAS_PURE_FLAG }, @@ -1241,7 +1236,6 @@ _PyOpcode_macro_expansion[256] = { [COMPARE_OP_STR] = { .nuops = 2, .uops = { { _GUARD_BOTH_UNICODE, 0, 0 }, { _COMPARE_OP_STR, 0, 0 } } }, [CONTAINS_OP] = { .nuops = 1, .uops = { { _CONTAINS_OP, 0, 0 } } }, [CONTAINS_OP_DICT] = { .nuops = 1, .uops = { { _CONTAINS_OP_DICT, 0, 0 } } }, - [CONTAINS_OP_MAPPINGPROXY] = { .nuops = 1, .uops = { { _CONTAINS_OP_MAPPINGPROXY, 0, 0 } } }, [CONTAINS_OP_SET] = { .nuops = 1, .uops = { { _CONTAINS_OP_SET, 0, 0 } } }, [CONVERT_VALUE] = { .nuops = 1, .uops = { { _CONVERT_VALUE, 0, 0 } } }, [COPY] = { .nuops = 1, .uops = { { _COPY, 0, 0 } } }, @@ -1417,7 +1411,6 @@ const char *_PyOpcode_OpName[268] = { [COMPARE_OP_STR] = "COMPARE_OP_STR", [CONTAINS_OP] = "CONTAINS_OP", [CONTAINS_OP_DICT] = "CONTAINS_OP_DICT", - [CONTAINS_OP_MAPPINGPROXY] = "CONTAINS_OP_MAPPINGPROXY", [CONTAINS_OP_SET] = "CONTAINS_OP_SET", [CONVERT_VALUE] = "CONVERT_VALUE", [COPY] = "COPY", @@ -1671,7 +1664,6 @@ const uint8_t _PyOpcode_Deopt[256] = { [COMPARE_OP_STR] = COMPARE_OP, [CONTAINS_OP] = CONTAINS_OP, [CONTAINS_OP_DICT] = CONTAINS_OP, - [CONTAINS_OP_MAPPINGPROXY] = CONTAINS_OP, [CONTAINS_OP_SET] = CONTAINS_OP, [CONVERT_VALUE] = CONVERT_VALUE, [COPY] = COPY, @@ -1859,6 +1851,7 @@ const uint8_t _PyOpcode_Deopt[256] = { case 146: \ case 147: \ case 148: \ + case 221: \ case 222: \ case 223: \ case 224: \ diff --git a/Include/internal/pycore_uop_ids.h b/Include/internal/pycore_uop_ids.h index 61fc5fecf7cb0d..1209d736abe696 100644 --- a/Include/internal/pycore_uop_ids.h +++ b/Include/internal/pycore_uop_ids.h @@ -76,7 +76,6 @@ extern "C" { #define _COMPARE_OP_STR 328 #define _CONTAINS_OP 329 #define _CONTAINS_OP_DICT CONTAINS_OP_DICT -#define _CONTAINS_OP_MAPPINGPROXY CONTAINS_OP_MAPPINGPROXY #define _CONTAINS_OP_SET CONTAINS_OP_SET #define _CONVERT_VALUE CONVERT_VALUE #define _COPY COPY diff --git a/Include/internal/pycore_uop_metadata.h b/Include/internal/pycore_uop_metadata.h index d5f27ccbafb185..7f984d88299b62 100644 --- a/Include/internal/pycore_uop_metadata.h +++ b/Include/internal/pycore_uop_metadata.h @@ -156,7 +156,6 @@ const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = { [_CONTAINS_OP] = HAS_ARG_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_SET] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CONTAINS_OP_DICT] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, - [_CONTAINS_OP_MAPPINGPROXY] = HAS_ARG_FLAG | HAS_DEOPT_FLAG | HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EG_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_CHECK_EXC_MATCH] = HAS_ERROR_FLAG | HAS_ESCAPES_FLAG, [_IS_NONE] = 0, @@ -312,7 +311,6 @@ const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = { [_COMPARE_OP_STR] = "_COMPARE_OP_STR", [_CONTAINS_OP] = "_CONTAINS_OP", [_CONTAINS_OP_DICT] = "_CONTAINS_OP_DICT", - [_CONTAINS_OP_MAPPINGPROXY] = "_CONTAINS_OP_MAPPINGPROXY", [_CONTAINS_OP_SET] = "_CONTAINS_OP_SET", [_CONVERT_VALUE] = "_CONVERT_VALUE", [_COPY] = "_COPY", diff --git a/Include/opcode_ids.h b/Include/opcode_ids.h index ce65ac74b74959..185205c6870edc 100644 --- a/Include/opcode_ids.h +++ b/Include/opcode_ids.h @@ -164,44 +164,43 @@ extern "C" { #define COMPARE_OP_INT 181 #define COMPARE_OP_STR 182 #define CONTAINS_OP_DICT 183 -#define CONTAINS_OP_MAPPINGPROXY 184 -#define CONTAINS_OP_SET 185 -#define FOR_ITER_GEN 186 -#define FOR_ITER_LIST 187 -#define FOR_ITER_RANGE 188 -#define FOR_ITER_TUPLE 189 -#define LOAD_ATTR_CLASS 190 -#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 191 -#define LOAD_ATTR_INSTANCE_VALUE 192 -#define LOAD_ATTR_METHOD_LAZY_DICT 193 -#define LOAD_ATTR_METHOD_NO_DICT 194 -#define LOAD_ATTR_METHOD_WITH_VALUES 195 -#define LOAD_ATTR_MODULE 196 -#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 197 -#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 198 -#define LOAD_ATTR_PROPERTY 199 -#define LOAD_ATTR_SLOT 200 -#define LOAD_ATTR_WITH_HINT 201 -#define LOAD_GLOBAL_BUILTIN 202 -#define LOAD_GLOBAL_MODULE 203 -#define LOAD_SUPER_ATTR_ATTR 204 -#define LOAD_SUPER_ATTR_METHOD 205 -#define RESUME_CHECK 206 -#define SEND_GEN 207 -#define STORE_ATTR_INSTANCE_VALUE 208 -#define STORE_ATTR_SLOT 209 -#define STORE_ATTR_WITH_HINT 210 -#define STORE_SUBSCR_DICT 211 -#define STORE_SUBSCR_LIST_INT 212 -#define TO_BOOL_ALWAYS_TRUE 213 -#define TO_BOOL_BOOL 214 -#define TO_BOOL_INT 215 -#define TO_BOOL_LIST 216 -#define TO_BOOL_NONE 217 -#define TO_BOOL_STR 218 -#define UNPACK_SEQUENCE_LIST 219 -#define UNPACK_SEQUENCE_TUPLE 220 -#define UNPACK_SEQUENCE_TWO_TUPLE 221 +#define CONTAINS_OP_SET 184 +#define FOR_ITER_GEN 185 +#define FOR_ITER_LIST 186 +#define FOR_ITER_RANGE 187 +#define FOR_ITER_TUPLE 188 +#define LOAD_ATTR_CLASS 189 +#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 190 +#define LOAD_ATTR_INSTANCE_VALUE 191 +#define LOAD_ATTR_METHOD_LAZY_DICT 192 +#define LOAD_ATTR_METHOD_NO_DICT 193 +#define LOAD_ATTR_METHOD_WITH_VALUES 194 +#define LOAD_ATTR_MODULE 195 +#define LOAD_ATTR_NONDESCRIPTOR_NO_DICT 196 +#define LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES 197 +#define LOAD_ATTR_PROPERTY 198 +#define LOAD_ATTR_SLOT 199 +#define LOAD_ATTR_WITH_HINT 200 +#define LOAD_GLOBAL_BUILTIN 201 +#define LOAD_GLOBAL_MODULE 202 +#define LOAD_SUPER_ATTR_ATTR 203 +#define LOAD_SUPER_ATTR_METHOD 204 +#define RESUME_CHECK 205 +#define SEND_GEN 206 +#define STORE_ATTR_INSTANCE_VALUE 207 +#define STORE_ATTR_SLOT 208 +#define STORE_ATTR_WITH_HINT 209 +#define STORE_SUBSCR_DICT 210 +#define STORE_SUBSCR_LIST_INT 211 +#define TO_BOOL_ALWAYS_TRUE 212 +#define TO_BOOL_BOOL 213 +#define TO_BOOL_INT 214 +#define TO_BOOL_LIST 215 +#define TO_BOOL_NONE 216 +#define TO_BOOL_STR 217 +#define UNPACK_SEQUENCE_LIST 218 +#define UNPACK_SEQUENCE_TUPLE 219 +#define UNPACK_SEQUENCE_TWO_TUPLE 220 #define INSTRUMENTED_RESUME 236 #define INSTRUMENTED_END_FOR 237 #define INSTRUMENTED_END_SEND 238 diff --git a/Lib/_opcode_metadata.py b/Lib/_opcode_metadata.py index 3dc3ccb0138a11..b5bafe6302bc9e 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -78,7 +78,6 @@ "CONTAINS_OP": [ "CONTAINS_OP_SET", "CONTAINS_OP_DICT", - "CONTAINS_OP_MAPPINGPROXY", ], "FOR_ITER": [ "FOR_ITER_LIST", @@ -144,44 +143,43 @@ 'COMPARE_OP_INT': 181, 'COMPARE_OP_STR': 182, 'CONTAINS_OP_DICT': 183, - 'CONTAINS_OP_MAPPINGPROXY': 184, - 'CONTAINS_OP_SET': 185, - 'FOR_ITER_GEN': 186, - 'FOR_ITER_LIST': 187, - 'FOR_ITER_RANGE': 188, - 'FOR_ITER_TUPLE': 189, - 'LOAD_ATTR_CLASS': 190, - 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 191, - 'LOAD_ATTR_INSTANCE_VALUE': 192, - 'LOAD_ATTR_METHOD_LAZY_DICT': 193, - 'LOAD_ATTR_METHOD_NO_DICT': 194, - 'LOAD_ATTR_METHOD_WITH_VALUES': 195, - 'LOAD_ATTR_MODULE': 196, - 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 197, - 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 198, - 'LOAD_ATTR_PROPERTY': 199, - 'LOAD_ATTR_SLOT': 200, - 'LOAD_ATTR_WITH_HINT': 201, - 'LOAD_GLOBAL_BUILTIN': 202, - 'LOAD_GLOBAL_MODULE': 203, - 'LOAD_SUPER_ATTR_ATTR': 204, - 'LOAD_SUPER_ATTR_METHOD': 205, - 'RESUME_CHECK': 206, - 'SEND_GEN': 207, - 'STORE_ATTR_INSTANCE_VALUE': 208, - 'STORE_ATTR_SLOT': 209, - 'STORE_ATTR_WITH_HINT': 210, - 'STORE_SUBSCR_DICT': 211, - 'STORE_SUBSCR_LIST_INT': 212, - 'TO_BOOL_ALWAYS_TRUE': 213, - 'TO_BOOL_BOOL': 214, - 'TO_BOOL_INT': 215, - 'TO_BOOL_LIST': 216, - 'TO_BOOL_NONE': 217, - 'TO_BOOL_STR': 218, - 'UNPACK_SEQUENCE_LIST': 219, - 'UNPACK_SEQUENCE_TUPLE': 220, - 'UNPACK_SEQUENCE_TWO_TUPLE': 221, + 'CONTAINS_OP_SET': 184, + 'FOR_ITER_GEN': 185, + 'FOR_ITER_LIST': 186, + 'FOR_ITER_RANGE': 187, + 'FOR_ITER_TUPLE': 188, + 'LOAD_ATTR_CLASS': 189, + 'LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN': 190, + 'LOAD_ATTR_INSTANCE_VALUE': 191, + 'LOAD_ATTR_METHOD_LAZY_DICT': 192, + 'LOAD_ATTR_METHOD_NO_DICT': 193, + 'LOAD_ATTR_METHOD_WITH_VALUES': 194, + 'LOAD_ATTR_MODULE': 195, + 'LOAD_ATTR_NONDESCRIPTOR_NO_DICT': 196, + 'LOAD_ATTR_NONDESCRIPTOR_WITH_VALUES': 197, + 'LOAD_ATTR_PROPERTY': 198, + 'LOAD_ATTR_SLOT': 199, + 'LOAD_ATTR_WITH_HINT': 200, + 'LOAD_GLOBAL_BUILTIN': 201, + 'LOAD_GLOBAL_MODULE': 202, + 'LOAD_SUPER_ATTR_ATTR': 203, + 'LOAD_SUPER_ATTR_METHOD': 204, + 'RESUME_CHECK': 205, + 'SEND_GEN': 206, + 'STORE_ATTR_INSTANCE_VALUE': 207, + 'STORE_ATTR_SLOT': 208, + 'STORE_ATTR_WITH_HINT': 209, + 'STORE_SUBSCR_DICT': 210, + 'STORE_SUBSCR_LIST_INT': 211, + 'TO_BOOL_ALWAYS_TRUE': 212, + 'TO_BOOL_BOOL': 213, + 'TO_BOOL_INT': 214, + 'TO_BOOL_LIST': 215, + 'TO_BOOL_NONE': 216, + 'TO_BOOL_STR': 217, + 'UNPACK_SEQUENCE_LIST': 218, + 'UNPACK_SEQUENCE_TUPLE': 219, + 'UNPACK_SEQUENCE_TWO_TUPLE': 220, } opmap = { diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 4074c94054deec..df546a090c28e4 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1083,8 +1083,8 @@ static PyNumberMethods mappingproxy_as_number = { .nb_inplace_or = mappingproxy_ior, }; -int -_PyMappingProxy_Contains(PyObject *self, PyObject *key) +static int +mappingproxy_contains(PyObject *self, PyObject *key) { mappingproxyobject *pp = (mappingproxyobject *)self; if (PyDict_CheckExact(pp->mapping)) @@ -1101,7 +1101,7 @@ static PySequenceMethods mappingproxy_as_sequence = { 0, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ - _PyMappingProxy_Contains, /* sq_contains */ + mappingproxy_contains, /* sq_contains */ 0, /* sq_inplace_concat */ 0, /* sq_inplace_repeat */ }; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index db73ac07bcc9f7..0397d96e3f63ba 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2240,7 +2240,6 @@ dummy_func( family(CONTAINS_OP, INLINE_CACHE_ENTRIES_CONTAINS_OP) = { CONTAINS_OP_SET, CONTAINS_OP_DICT, - CONTAINS_OP_MAPPINGPROXY, }; op(_CONTAINS_OP, (left, right -- b)) { @@ -2283,15 +2282,6 @@ dummy_func( b = (res ^ oparg) ? Py_True : Py_False; } - inst(CONTAINS_OP_MAPPINGPROXY, (unused/1, left, right -- b)) { - DEOPT_IF(Py_TYPE(right) != &PyDictProxy_Type); - STAT_INC(CONTAINS_OP, hit); - int res = _PyMappingProxy_Contains(right, left); - DECREF_INPUTS(); - ERROR_IF(res < 0, error); - b = (res ^ oparg) ? Py_True : Py_False; - } - inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) { if (_PyEval_CheckExceptStarTypeValid(tstate, match_type) < 0) { DECREF_INPUTS(); diff --git a/Python/ceval.c b/Python/ceval.c index f65502c37428b0..f817f288903694 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -28,7 +28,6 @@ #include "pycore_typeobject.h" // _PySuper_Lookup() #include "pycore_uop_ids.h" // Uops #include "pycore_pyerrors.h" -#include "pycore_descrobject.h" #include "pycore_dict.h" #include "dictobject.h" diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 48c88ce72dbb3a..26ac1593d534e4 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2228,25 +2228,6 @@ break; } - case _CONTAINS_OP_MAPPINGPROXY: { - PyObject *right; - PyObject *left; - PyObject *b; - oparg = CURRENT_OPARG(); - right = stack_pointer[-1]; - left = stack_pointer[-2]; - if (Py_TYPE(right) != &PyDictProxy_Type) goto deoptimize; - STAT_INC(CONTAINS_OP, hit); - int res = _PyMappingProxy_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error_tier_two; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CHECK_EG_MATCH: { PyObject *match_type; PyObject *exc_value; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 67da86bf2d2d2c..f5d125c63b10ca 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2187,29 +2187,6 @@ DISPATCH(); } - TARGET(CONTAINS_OP_MAPPINGPROXY) { - frame->instr_ptr = next_instr; - next_instr += 2; - INSTRUCTION_STATS(CONTAINS_OP_MAPPINGPROXY); - static_assert(INLINE_CACHE_ENTRIES_CONTAINS_OP == 1, "incorrect cache size"); - PyObject *right; - PyObject *left; - PyObject *b; - /* Skip 1 cache entry */ - right = stack_pointer[-1]; - left = stack_pointer[-2]; - DEOPT_IF(Py_TYPE(right) != &PyDictProxy_Type, CONTAINS_OP); - STAT_INC(CONTAINS_OP, hit); - int res = _PyMappingProxy_Contains(right, left); - Py_DECREF(left); - Py_DECREF(right); - if (res < 0) goto pop_2_error; - b = (res ^ oparg) ? Py_True : Py_False; - stack_pointer[-2] = b; - stack_pointer += -1; - DISPATCH(); - } - TARGET(CONTAINS_OP_SET) { frame->instr_ptr = next_instr; next_instr += 2; diff --git a/Python/jit.c b/Python/jit.c index 83fb3e3bf3ab41..dae25166b1f106 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -15,7 +15,6 @@ #include "pycore_setobject.h" #include "pycore_sliceobject.h" #include "pycore_jit.h" -#include "pycore_descrobject.h" #include "jit_stencils.h" diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index ab83199dbd2f8f..4061ba33cea53e 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -183,7 +183,6 @@ static void *opcode_targets[256] = { &&TARGET_COMPARE_OP_INT, &&TARGET_COMPARE_OP_STR, &&TARGET_CONTAINS_OP_DICT, - &&TARGET_CONTAINS_OP_MAPPINGPROXY, &&TARGET_CONTAINS_OP_SET, &&TARGET_FOR_ITER_GEN, &&TARGET_FOR_ITER_LIST, @@ -235,6 +234,7 @@ static void *opcode_targets[256] = { &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, + &&_unknown_opcode, &&TARGET_INSTRUMENTED_RESUME, &&TARGET_INSTRUMENTED_END_FOR, &&TARGET_INSTRUMENTED_END_SEND, diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 5b20437ba95678..7e8d677fd6030d 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1224,15 +1224,6 @@ break; } - case _CONTAINS_OP_MAPPINGPROXY: { - _Py_UopsSymbol *b; - b = sym_new_unknown(ctx); - if (b == NULL) goto out_of_space; - stack_pointer[-2] = b; - stack_pointer += -1; - break; - } - case _CHECK_EG_MATCH: { _Py_UopsSymbol *rest; _Py_UopsSymbol *match; diff --git a/Python/specialize.c b/Python/specialize.c index 464bfcf7ac1a99..b1f9eb756c3665 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -2568,6 +2568,24 @@ _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr) cache->counter = adaptive_counter_cooldown(); } +#ifdef Py_STATS +static int containsop_fail_kind(PyObject *value) { + if (PyUnicode_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_STR; + } + if (PyList_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_LIST; + } + if (PyTuple_CheckExact(value)) { + return SPEC_FAIL_CONTAINS_OP_TUPLE; + } + if (PyType_Check(value)) { + return SPEC_FAIL_CONTAINS_OP_USER_CLASS; + } + return SPEC_FAIL_OTHER; +} +#endif // Py_STATS + void _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr) { @@ -2583,31 +2601,7 @@ _Py_Specialize_ContainsOp(PyObject *value, _Py_CODEUNIT *instr) goto success; } - if (Py_TYPE(value) == &PyDictProxy_Type) { - instr->op.code = CONTAINS_OP_MAPPINGPROXY; - goto success; - } - -#ifdef Py_STATS - if (PyUnicode_CheckExact(value)) { - SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_STR); - goto failure; - } - if (PyList_CheckExact(value)) { - SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_LIST); - goto failure; - } - if (PyTuple_CheckExact(value)) { - SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_TUPLE); - goto failure; - } - if (PyType_Check(value)) { - SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_CONTAINS_OP_USER_CLASS); - goto failure; - } - SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_OTHER); -#endif // Py_STATS -failure: + SPECIALIZATION_FAIL(CONTAINS_OP, containsop_fail_kind(value)); STAT_INC(CONTAINS_OP, failure); instr->op.code = CONTAINS_OP; cache->counter = adaptive_counter_backoff(cache->counter);