bpo-46841: Use *inline* caching for BINARY_OP (GH-31543)

This commit is contained in:
Brandt Bucher 2022-02-25 04:11:34 -08:00 committed by GitHub
parent 18b5dd68c6
commit 0f41aac109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 429 additions and 351 deletions

View file

@ -5,7 +5,7 @@
/* Each instruction in a code object is a fixed-width value,
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
* opcode allows for larger values but the current limit is 3 uses
* of EXTENDED_ARG (see Python/wordcode_helpers.h), for a maximum
* of EXTENDED_ARG (see Python/compile.c), for a maximum
* 32-bit value. This aligns with the note in Python/compile.c
* (compiler_addop_i_line) indicating that the max oparg value is
* 2**32 - 1, rather than INT_MAX.

View file

@ -64,6 +64,13 @@ typedef union {
#define INSTRUCTIONS_PER_ENTRY (sizeof(SpecializedCacheEntry)/sizeof(_Py_CODEUNIT))
typedef struct {
_Py_CODEUNIT counter;
} _PyBinaryOpCache;
#define INLINE_CACHE_ENTRIES_BINARY_OP \
(sizeof(_PyBinaryOpCache) / sizeof(_Py_CODEUNIT))
/* Maximum size of code to quicken, in code units. */
#define MAX_SIZE_TO_QUICKEN 5000
@ -276,7 +283,7 @@ int _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
int _Py_Specialize_Precall(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
PyObject *kwnames, SpecializedCacheEntry *cache, PyObject *builtins);
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
SpecializedCacheEntry *cache);
int oparg);
void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
SpecializedCacheEntry *cache);

145
Include/opcode.h generated
View file

@ -9,6 +9,7 @@ extern "C" {
/* Instruction opcodes for compiled code */
#define POP_TOP 1
#define PUSH_NULL 2
#define CACHE 3
#define NOP 9
#define UNARY_POSITIVE 10
#define UNARY_NEGATIVE 11
@ -113,76 +114,76 @@ extern "C" {
#define PRECALL 166
#define CALL 171
#define KW_NAMES 172
#define BINARY_OP_ADAPTIVE 3
#define BINARY_OP_ADD_INT 4
#define BINARY_OP_ADD_FLOAT 5
#define BINARY_OP_ADD_UNICODE 6
#define BINARY_OP_INPLACE_ADD_UNICODE 7
#define BINARY_OP_MULTIPLY_INT 8
#define BINARY_OP_MULTIPLY_FLOAT 13
#define BINARY_OP_SUBTRACT_INT 14
#define BINARY_OP_SUBTRACT_FLOAT 16
#define COMPARE_OP_ADAPTIVE 17
#define COMPARE_OP_FLOAT_JUMP 18
#define COMPARE_OP_INT_JUMP 19
#define COMPARE_OP_STR_JUMP 20
#define BINARY_SUBSCR_ADAPTIVE 21
#define BINARY_SUBSCR_GETITEM 22
#define BINARY_SUBSCR_LIST_INT 23
#define BINARY_SUBSCR_TUPLE_INT 24
#define BINARY_SUBSCR_DICT 26
#define STORE_SUBSCR_ADAPTIVE 27
#define STORE_SUBSCR_LIST_INT 28
#define STORE_SUBSCR_DICT 29
#define CALL_ADAPTIVE 34
#define CALL_PY_EXACT_ARGS 36
#define CALL_PY_WITH_DEFAULTS 37
#define JUMP_ABSOLUTE_QUICK 38
#define LOAD_ATTR_ADAPTIVE 39
#define LOAD_ATTR_INSTANCE_VALUE 40
#define LOAD_ATTR_WITH_HINT 41
#define LOAD_ATTR_SLOT 42
#define LOAD_ATTR_MODULE 43
#define LOAD_GLOBAL_ADAPTIVE 44
#define LOAD_GLOBAL_MODULE 45
#define LOAD_GLOBAL_BUILTIN 46
#define LOAD_METHOD_ADAPTIVE 47
#define LOAD_METHOD_CLASS 48
#define LOAD_METHOD_MODULE 55
#define LOAD_METHOD_NO_DICT 56
#define LOAD_METHOD_WITH_DICT 57
#define LOAD_METHOD_WITH_VALUES 58
#define PRECALL_ADAPTIVE 59
#define PRECALL_BUILTIN_CLASS 62
#define PRECALL_NO_KW_BUILTIN_O 63
#define PRECALL_NO_KW_BUILTIN_FAST 64
#define PRECALL_BUILTIN_FAST_WITH_KEYWORDS 65
#define PRECALL_NO_KW_LEN 66
#define PRECALL_NO_KW_ISINSTANCE 67
#define PRECALL_NO_KW_LIST_APPEND 72
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O 76
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 77
#define PRECALL_NO_KW_STR_1 78
#define PRECALL_NO_KW_TUPLE_1 79
#define PRECALL_NO_KW_TYPE_1 80
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST 81
#define PRECALL_BOUND_METHOD 131
#define PRECALL_PYFUNC 140
#define RESUME_QUICK 141
#define STORE_ATTR_ADAPTIVE 143
#define STORE_ATTR_INSTANCE_VALUE 150
#define STORE_ATTR_SLOT 153
#define STORE_ATTR_WITH_HINT 154
#define UNPACK_SEQUENCE_ADAPTIVE 158
#define UNPACK_SEQUENCE_LIST 159
#define UNPACK_SEQUENCE_TUPLE 161
#define UNPACK_SEQUENCE_TWO_TUPLE 167
#define LOAD_FAST__LOAD_FAST 168
#define STORE_FAST__LOAD_FAST 169
#define LOAD_FAST__LOAD_CONST 170
#define LOAD_CONST__LOAD_FAST 173
#define STORE_FAST__STORE_FAST 174
#define LOAD_FAST__LOAD_ATTR_INSTANCE_VALUE 175
#define BINARY_OP_ADAPTIVE 4
#define BINARY_OP_ADD_INT 5
#define BINARY_OP_ADD_FLOAT 6
#define BINARY_OP_ADD_UNICODE 7
#define BINARY_OP_INPLACE_ADD_UNICODE 8
#define BINARY_OP_MULTIPLY_INT 13
#define BINARY_OP_MULTIPLY_FLOAT 14
#define BINARY_OP_SUBTRACT_INT 16
#define BINARY_OP_SUBTRACT_FLOAT 17
#define COMPARE_OP_ADAPTIVE 18
#define COMPARE_OP_FLOAT_JUMP 19
#define COMPARE_OP_INT_JUMP 20
#define COMPARE_OP_STR_JUMP 21
#define BINARY_SUBSCR_ADAPTIVE 22
#define BINARY_SUBSCR_GETITEM 23
#define BINARY_SUBSCR_LIST_INT 24
#define BINARY_SUBSCR_TUPLE_INT 26
#define BINARY_SUBSCR_DICT 27
#define STORE_SUBSCR_ADAPTIVE 28
#define STORE_SUBSCR_LIST_INT 29
#define STORE_SUBSCR_DICT 34
#define CALL_ADAPTIVE 36
#define CALL_PY_EXACT_ARGS 37
#define CALL_PY_WITH_DEFAULTS 38
#define JUMP_ABSOLUTE_QUICK 39
#define LOAD_ATTR_ADAPTIVE 40
#define LOAD_ATTR_INSTANCE_VALUE 41
#define LOAD_ATTR_WITH_HINT 42
#define LOAD_ATTR_SLOT 43
#define LOAD_ATTR_MODULE 44
#define LOAD_GLOBAL_ADAPTIVE 45
#define LOAD_GLOBAL_MODULE 46
#define LOAD_GLOBAL_BUILTIN 47
#define LOAD_METHOD_ADAPTIVE 48
#define LOAD_METHOD_CLASS 55
#define LOAD_METHOD_MODULE 56
#define LOAD_METHOD_NO_DICT 57
#define LOAD_METHOD_WITH_DICT 58
#define LOAD_METHOD_WITH_VALUES 59
#define PRECALL_ADAPTIVE 62
#define PRECALL_BUILTIN_CLASS 63
#define PRECALL_NO_KW_BUILTIN_O 64
#define PRECALL_NO_KW_BUILTIN_FAST 65
#define PRECALL_BUILTIN_FAST_WITH_KEYWORDS 66
#define PRECALL_NO_KW_LEN 67
#define PRECALL_NO_KW_ISINSTANCE 72
#define PRECALL_NO_KW_LIST_APPEND 76
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O 77
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 78
#define PRECALL_NO_KW_STR_1 79
#define PRECALL_NO_KW_TUPLE_1 80
#define PRECALL_NO_KW_TYPE_1 81
#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST 131
#define PRECALL_BOUND_METHOD 140
#define PRECALL_PYFUNC 141
#define RESUME_QUICK 143
#define STORE_ATTR_ADAPTIVE 150
#define STORE_ATTR_INSTANCE_VALUE 153
#define STORE_ATTR_SLOT 154
#define STORE_ATTR_WITH_HINT 158
#define UNPACK_SEQUENCE_ADAPTIVE 159
#define UNPACK_SEQUENCE_LIST 161
#define UNPACK_SEQUENCE_TUPLE 167
#define UNPACK_SEQUENCE_TWO_TUPLE 168
#define LOAD_FAST__LOAD_FAST 169
#define STORE_FAST__LOAD_FAST 170
#define LOAD_FAST__LOAD_CONST 173
#define LOAD_CONST__LOAD_FAST 174
#define STORE_FAST__STORE_FAST 175
#define LOAD_FAST__LOAD_ATTR_INSTANCE_VALUE 176
#define DO_TRACING 255
#ifdef NEED_OPCODE_JUMP_TABLES
static uint32_t _PyOpcode_RelativeJump[8] = {
@ -239,6 +240,10 @@ static uint32_t _PyOpcode_Jump[8] = {
#define NB_INPLACE_TRUE_DIVIDE 24
#define NB_INPLACE_XOR 25
static const uint8_t _PyOpcode_InlineCacheEntries[256] = {
[BINARY_OP] = 1,
};
#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
/* Reserve some bytecodes for internal use in the compiler.