From 4696bf759a5a7390f9848f3a9cf6d7859d49b56a Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 26 Mar 2022 11:41:45 +0000 Subject: [PATCH 1/3] add PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS --- Include/opcode.h | 38 ++++++++++++++++++++------------------ Lib/opcode.py | 1 + Python/ceval.c | 32 ++++++++++++++++++++++++++++++++ Python/opcode_targets.h | 18 +++++++++--------- Python/specialize.c | 4 ++++ main.py | 6 ++++++ 6 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 main.py diff --git a/Include/opcode.h b/Include/opcode.h index dfc7b72e3cdc68..464f9936a3805c 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -165,24 +165,25 @@ extern "C" { #define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST 76 #define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 77 #define PRECALL_NO_KW_METHOD_DESCRIPTOR_O 78 -#define PRECALL_NO_KW_STR_1 79 -#define PRECALL_NO_KW_TUPLE_1 80 -#define PRECALL_NO_KW_TYPE_1 81 -#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 STORE_FAST__LOAD_FAST 158 -#define STORE_FAST__STORE_FAST 159 -#define STORE_SUBSCR_ADAPTIVE 161 -#define STORE_SUBSCR_DICT 167 -#define STORE_SUBSCR_LIST_INT 168 -#define UNPACK_SEQUENCE_ADAPTIVE 169 -#define UNPACK_SEQUENCE_LIST 170 -#define UNPACK_SEQUENCE_TUPLE 173 -#define UNPACK_SEQUENCE_TWO_TUPLE 174 +#define PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 79 +#define PRECALL_NO_KW_STR_1 80 +#define PRECALL_NO_KW_TUPLE_1 81 +#define PRECALL_NO_KW_TYPE_1 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 STORE_FAST__LOAD_FAST 159 +#define STORE_FAST__STORE_FAST 161 +#define STORE_SUBSCR_ADAPTIVE 167 +#define STORE_SUBSCR_DICT 168 +#define STORE_SUBSCR_LIST_INT 169 +#define UNPACK_SEQUENCE_ADAPTIVE 170 +#define UNPACK_SEQUENCE_LIST 173 +#define UNPACK_SEQUENCE_TUPLE 174 +#define UNPACK_SEQUENCE_TWO_TUPLE 175 #define DO_TRACING 255 extern const uint8_t _PyOpcode_Caches[256]; @@ -347,6 +348,7 @@ const uint8_t _PyOpcode_Deopt[256] = { [PRECALL_BOUND_METHOD] = PRECALL, [PRECALL_BUILTIN_CLASS] = PRECALL, [PRECALL_BUILTIN_FAST_WITH_KEYWORDS] = PRECALL, + [PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = PRECALL, [PRECALL_NO_KW_BUILTIN_FAST] = PRECALL, [PRECALL_NO_KW_BUILTIN_O] = PRECALL, [PRECALL_NO_KW_ISINSTANCE] = PRECALL, diff --git a/Lib/opcode.py b/Lib/opcode.py index 7a52c13579af7c..444b63b8a2e461 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -302,6 +302,7 @@ def jabs_op(name, op, entries=0): "PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST", "PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS", "PRECALL_NO_KW_METHOD_DESCRIPTOR_O", + "PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS", "PRECALL_NO_KW_STR_1", "PRECALL_NO_KW_TUPLE_1", "PRECALL_NO_KW_TYPE_1", diff --git a/Python/ceval.c b/Python/ceval.c index 4824b192f4e48b..8b59f4233a7b23 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -5090,6 +5090,38 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int DISPATCH(); } + TARGET(PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS) { + int is_meth = is_method(stack_pointer, oparg); + int total_args = oparg + is_meth; + PyObject *callable = PEEK(total_args + 1); + DEOPT_IF(!Py_IS_TYPE(callable, &PyMethodDescr_Type), PRECALL); + PyMethodDef *meth = ((PyMethodDescrObject *)callable)->d_method; + DEOPT_IF(meth->ml_flags != (METH_FASTCALL|METH_KEYWORDS), PRECALL); + STAT_INC(PRECALL, hit); + SKIP_CALL(); + int nargs = total_args-1; + STACK_SHRINK(nargs); + _PyCFunctionFastWithKeywords cfunc = (_PyCFunctionFastWithKeywords)(void(*)(void))meth->ml_meth; + PyObject *self = TOP(); + PyObject *res = cfunc(self, stack_pointer, nargs - KWNAMES_LEN(), call_shape.kwnames); + assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); + call_shape.kwnames = NULL; + + /* Free the arguments. */ + for (int i = 0; i < nargs; i++) { + Py_DECREF(stack_pointer[i]); + } + Py_DECREF(self); + STACK_SHRINK(2-is_meth); + SET_TOP(res); + Py_DECREF(callable); + if (res == NULL) { + goto error; + } + CHECK_EVAL_BREAKER(); + DISPATCH(); + } + TARGET(PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS) { assert(call_shape.kwnames == NULL); assert(oparg == 0 || oparg == 1); diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 2aa6471abf99a1..75de4a912ba5f2 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -78,9 +78,9 @@ static void *opcode_targets[256] = { &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST, &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS, &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_O, + &&TARGET_PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, &&TARGET_PRECALL_NO_KW_STR_1, &&TARGET_PRECALL_NO_KW_TUPLE_1, - &&TARGET_PRECALL_NO_KW_TYPE_1, &&TARGET_LIST_TO_TUPLE, &&TARGET_RETURN_VALUE, &&TARGET_IMPORT_STAR, @@ -139,39 +139,40 @@ static void *opcode_targets[256] = { &&TARGET_LOAD_DEREF, &&TARGET_STORE_DEREF, &&TARGET_DELETE_DEREF, + &&TARGET_PRECALL_NO_KW_TYPE_1, &&TARGET_PRECALL_PYFUNC, - &&TARGET_RESUME_QUICK, &&TARGET_CALL_FUNCTION_EX, - &&TARGET_STORE_ATTR_ADAPTIVE, + &&TARGET_RESUME_QUICK, &&TARGET_EXTENDED_ARG, &&TARGET_LIST_APPEND, &&TARGET_SET_ADD, &&TARGET_MAP_ADD, &&TARGET_LOAD_CLASSDEREF, &&TARGET_COPY_FREE_VARS, - &&TARGET_STORE_ATTR_INSTANCE_VALUE, + &&TARGET_STORE_ATTR_ADAPTIVE, &&TARGET_RESUME, &&TARGET_MATCH_CLASS, + &&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_STORE_ATTR_SLOT, - &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_FORMAT_VALUE, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_STRING, + &&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_FAST__LOAD_FAST, - &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_LOAD_METHOD, - &&TARGET_STORE_SUBSCR_ADAPTIVE, + &&TARGET_STORE_FAST__STORE_FAST, &&TARGET_LIST_EXTEND, &&TARGET_SET_UPDATE, &&TARGET_DICT_MERGE, &&TARGET_DICT_UPDATE, &&TARGET_PRECALL, + &&TARGET_STORE_SUBSCR_ADAPTIVE, &&TARGET_STORE_SUBSCR_DICT, &&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_UNPACK_SEQUENCE_ADAPTIVE, - &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_CALL, &&TARGET_KW_NAMES, + &&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_UNPACK_SEQUENCE_TUPLE, &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&_unknown_opcode, @@ -253,6 +254,5 @@ static void *opcode_targets[256] = { &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, - &&_unknown_opcode, &&TARGET_DO_TRACING }; diff --git a/Python/specialize.c b/Python/specialize.c index 720bc7f241586b..5839d7629466d6 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -1446,6 +1446,10 @@ specialize_method_descriptor(PyMethodDescrObject *descr, _Py_CODEUNIT *instr, _Py_SET_OPCODE(*instr, PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST); return 0; } + case METH_FASTCALL|METH_KEYWORDS: { + _Py_SET_OPCODE(*instr, PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS); + return 0; + } } SPECIALIZATION_FAIL(PRECALL, builtin_call_fail_kind(descr->d_method->ml_flags)); return -1; diff --git a/main.py b/main.py new file mode 100644 index 00000000000000..397c5fdb198fc1 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +def func(): + b = bytearray() + b.translate(b'a'*256) + +for i in range(10): + func() \ No newline at end of file From d54df621c1448385603a9ebda19fcb6269f5e3d0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 26 Mar 2022 11:55:53 +0000 Subject: [PATCH 2/3] regen-all --- Include/opcode.h | 18 +++++++++--------- Lib/opcode.py | 2 +- Python/opcode_targets.h | 6 +++--- main.py | 6 ------ 4 files changed, 13 insertions(+), 19 deletions(-) delete mode 100644 main.py diff --git a/Include/opcode.h b/Include/opcode.h index 464f9936a3805c..0ce7c158bbd588 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -157,15 +157,15 @@ extern "C" { #define PRECALL_BOUND_METHOD 62 #define PRECALL_BUILTIN_CLASS 63 #define PRECALL_BUILTIN_FAST_WITH_KEYWORDS 64 -#define PRECALL_NO_KW_BUILTIN_FAST 65 -#define PRECALL_NO_KW_BUILTIN_O 66 -#define PRECALL_NO_KW_ISINSTANCE 67 -#define PRECALL_NO_KW_LEN 72 -#define PRECALL_NO_KW_LIST_APPEND 73 -#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST 76 -#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 77 -#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O 78 -#define PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 79 +#define PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 65 +#define PRECALL_NO_KW_BUILTIN_FAST 66 +#define PRECALL_NO_KW_BUILTIN_O 67 +#define PRECALL_NO_KW_ISINSTANCE 72 +#define PRECALL_NO_KW_LEN 73 +#define PRECALL_NO_KW_LIST_APPEND 76 +#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST 77 +#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 78 +#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O 79 #define PRECALL_NO_KW_STR_1 80 #define PRECALL_NO_KW_TUPLE_1 81 #define PRECALL_NO_KW_TYPE_1 140 diff --git a/Lib/opcode.py b/Lib/opcode.py index 444b63b8a2e461..0b463d3f183aad 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -294,6 +294,7 @@ def jabs_op(name, op, entries=0): "PRECALL_BOUND_METHOD", "PRECALL_BUILTIN_CLASS", "PRECALL_BUILTIN_FAST_WITH_KEYWORDS", + "PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS", "PRECALL_NO_KW_BUILTIN_FAST", "PRECALL_NO_KW_BUILTIN_O", "PRECALL_NO_KW_ISINSTANCE", @@ -302,7 +303,6 @@ def jabs_op(name, op, entries=0): "PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST", "PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS", "PRECALL_NO_KW_METHOD_DESCRIPTOR_O", - "PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS", "PRECALL_NO_KW_STR_1", "PRECALL_NO_KW_TUPLE_1", "PRECALL_NO_KW_TYPE_1", diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index 75de4a912ba5f2..dbcacee7e02055 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -64,21 +64,21 @@ static void *opcode_targets[256] = { &&TARGET_PRECALL_BOUND_METHOD, &&TARGET_PRECALL_BUILTIN_CLASS, &&TARGET_PRECALL_BUILTIN_FAST_WITH_KEYWORDS, + &&TARGET_PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, &&TARGET_PRECALL_NO_KW_BUILTIN_FAST, &&TARGET_PRECALL_NO_KW_BUILTIN_O, - &&TARGET_PRECALL_NO_KW_ISINSTANCE, &&TARGET_GET_ITER, &&TARGET_GET_YIELD_FROM_ITER, &&TARGET_PRINT_EXPR, &&TARGET_LOAD_BUILD_CLASS, + &&TARGET_PRECALL_NO_KW_ISINSTANCE, &&TARGET_PRECALL_NO_KW_LEN, - &&TARGET_PRECALL_NO_KW_LIST_APPEND, &&TARGET_LOAD_ASSERTION_ERROR, &&TARGET_RETURN_GENERATOR, + &&TARGET_PRECALL_NO_KW_LIST_APPEND, &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST, &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS, &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_O, - &&TARGET_PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, &&TARGET_PRECALL_NO_KW_STR_1, &&TARGET_PRECALL_NO_KW_TUPLE_1, &&TARGET_LIST_TO_TUPLE, diff --git a/main.py b/main.py deleted file mode 100644 index 397c5fdb198fc1..00000000000000 --- a/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def func(): - b = bytearray() - b.translate(b'a'*256) - -for i in range(10): - func() \ No newline at end of file From 5ee20bfae895cba53532ea79f28c397477570ce6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 26 Mar 2022 12:21:54 +0000 Subject: [PATCH 3/3] =?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-03-26-12-21-53.bpo-47127.Mh86RB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-03-26-12-21-53.bpo-47127.Mh86RB.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-26-12-21-53.bpo-47127.Mh86RB.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-26-12-21-53.bpo-47127.Mh86RB.rst new file mode 100644 index 00000000000000..c4ec9774429d4b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-03-26-12-21-53.bpo-47127.Mh86RB.rst @@ -0,0 +1 @@ +Speed up calls to c functions with keyword arguments by 25% with specialization. Patch by Kumar Aditya.