Skip to content
50 changes: 26 additions & 24 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def jabs_op(name, op):
"CALL_NO_KW_PY_SIMPLE",
"CALL_NO_KW_LIST_APPEND",
"CALL_NO_KW_METHOD_DESCRIPTOR_O",
"CALL_NO_KW_TYPE_1",
"CALL_NO_KW_BUILTIN_CLASS_1",
"CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
"JUMP_ABSOLUTE_QUICK",
"LOAD_ATTR_ADAPTIVE",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Specialize the CALL_FUNCTION instruction for calls to builtin types with a
single argument. Speeds up ``range(x)``, ``list(x)``, and specifically
``type(obj)``.
35 changes: 35 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4854,6 +4854,41 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
goto start_frame;
}

TARGET(CALL_NO_KW_TYPE_1) {
assert(STACK_ADJUST_IS_RESET);
assert(GET_CACHE()->adaptive.original_oparg == 1);
PyObject *obj = TOP();
PyObject *callable = SECOND();
DEOPT_IF(callable != (PyObject *)&PyType_Type, CALL_NO_KW);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if you like using the Py_IS_TYPE inline function, but just mentioning it here and leaving it up to you :).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll skip on the obfuscation macro 🙂

PyObject *res = Py_NewRef(Py_TYPE(obj));
STACK_SHRINK(1);
Py_DECREF(callable);
Py_DECREF(obj);
SET_TOP(res);
DISPATCH();
}

TARGET(CALL_NO_KW_BUILTIN_CLASS_1) {
assert(STACK_ADJUST_IS_RESET);
SpecializedCacheEntry *caches = GET_CACHE();
_PyAdaptiveEntry *cache0 = &caches[0].adaptive;
assert(cache0->original_oparg == 1);
PyObject *callable = SECOND();
PyObject *arg = TOP();
DEOPT_IF(!PyType_Check(callable), CALL_NO_KW);
PyTypeObject *tp = (PyTypeObject *)callable;
DEOPT_IF(tp->tp_version_tag != cache0->version, CALL_NO_KW);
STACK_SHRINK(1);
PyObject *res = tp->tp_vectorcall((PyObject *)tp, stack_pointer, 1, NULL);
SET_TOP(res);
Py_DECREF(tp);
Py_DECREF(arg);
if (res == NULL) {
goto error;
}
DISPATCH();
}

TARGET(CALL_NO_KW_BUILTIN_O) {
assert(cframe.use_tracing == 0);
assert(STACK_ADJUST_IS_RESET);
Expand Down
28 changes: 14 additions & 14 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,10 @@ initial_counter_value(void) {
#define SPEC_FAIL_PYCFUNCTION_NOARGS 16
#define SPEC_FAIL_BAD_CALL_FLAGS 17
#define SPEC_FAIL_CLASS 18
#define SPEC_FAIL_C_METHOD_CALL 19
#define SPEC_FAIL_METHDESCR_NON_METHOD 20
#define SPEC_FAIL_PYTHON_CLASS 19
#define SPEC_FAIL_C_METHOD_CALL 20
#define SPEC_FAIL_METHDESCR_NON_METHOD 21
#define SPEC_FAIL_METHOD_CALL_CLASS 22

/* COMPARE_OP */
#define SPEC_FAIL_STRING_COMPARE 13
Expand Down Expand Up @@ -1263,6 +1265,27 @@ specialize_class_call(
PyObject *callable, _Py_CODEUNIT *instr,
int nargs, SpecializedCacheEntry *cache)
{
assert(PyType_Check(callable));
PyTypeObject *tp = (PyTypeObject *)callable;
if (_Py_OPCODE(instr[-1]) == PRECALL_METHOD) {
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_METHOD_CALL_CLASS);
return -1;
}
if (tp->tp_new == PyBaseObject_Type.tp_new) {
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_PYTHON_CLASS);
return -1;
}
if (nargs == 1) {
if (tp == &PyType_Type) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

*instr = _Py_MAKECODEUNIT(CALL_NO_KW_TYPE_1, _Py_OPARG(*instr));
return 0;
}
if ((tp->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) && tp->tp_vectorcall != NULL) {
cache->adaptive.version = tp->tp_version_tag;
*instr = _Py_MAKECODEUNIT(CALL_NO_KW_BUILTIN_CLASS_1, _Py_OPARG(*instr));
return 0;
}
}
SPECIALIZATION_FAIL(CALL_NO_KW, SPEC_FAIL_CLASS);
return -1;
}
Expand Down