aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--Include/opcode.h54
-rw-r--r--Lib/opcode.py1
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-03-26-12-21-53.bpo-47127.Mh86RB.rst1
-rw-r--r--Python/ceval.c32
-rw-r--r--Python/opcode_targets.h22
-rw-r--r--Python/specialize.c4
6 files changed, 77 insertions, 37 deletions
diff --git a/Include/opcode.h b/Include/opcode.h
index dfc7b72e3cd..0ce7c158bbd 100644
--- a/Include/opcode.h
+++ b/Include/opcode.h
@@ -157,32 +157,33 @@ 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_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 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
+#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 7a52c13579a..0b463d3f183 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -294,6 +294,7 @@ _specializations = {
"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",
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 00000000000..c4ec9774429
--- /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.
diff --git a/Python/ceval.c b/Python/ceval.c
index 4824b192f4e..8b59f4233a7 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5090,6 +5090,38 @@ handle_eval_breaker:
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 2aa6471abf9..dbcacee7e02 100644
--- a/Python/opcode_targets.h
+++ b/Python/opcode_targets.h
@@ -64,23 +64,23 @@ 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_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 720bc7f2415..5839d762946 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;