Commit 4fb1e7a0 authored by Jeroen Demeyer's avatar Jeroen Demeyer Committed by Stefan Behnel

Simplify and optimize calls, using vectorcall on Python 3.8 (GH-2999)

* Do not use unsafe _PyMethodDef_RawFastCallKeywords
* Simplify and optimize calls, using vectorcall on Python 3.8
parent b28b849b
...@@ -6039,8 +6039,6 @@ class PyMethodCallNode(SimpleCallNode): ...@@ -6039,8 +6039,6 @@ class PyMethodCallNode(SimpleCallNode):
self_arg = code.funcstate.allocate_temp(py_object_type, manage_ref=True) self_arg = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
code.putln("%s = NULL;" % self_arg) code.putln("%s = NULL;" % self_arg)
arg_offset_cname = None
if len(args) > 1:
arg_offset_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False) arg_offset_cname = code.funcstate.allocate_temp(PyrexTypes.c_int_type, manage_ref=False)
code.putln("%s = 0;" % arg_offset_cname) code.putln("%s = 0;" % arg_offset_cname)
...@@ -6075,122 +6073,41 @@ class PyMethodCallNode(SimpleCallNode): ...@@ -6075,122 +6073,41 @@ class PyMethodCallNode(SimpleCallNode):
code.put_incref("function", py_object_type) code.put_incref("function", py_object_type)
# free method object as early to possible to enable reuse from CPython's freelist # free method object as early to possible to enable reuse from CPython's freelist
code.put_decref_set(function, "function") code.put_decref_set(function, "function")
if len(args) > 1:
code.putln("%s = 1;" % arg_offset_cname) code.putln("%s = 1;" % arg_offset_cname)
code.putln("}") code.putln("}")
code.putln("}") code.putln("}")
if not args: # actually call the function
# fastest special case: try to avoid tuple creation
code.globalstate.use_utility_code( code.globalstate.use_utility_code(
UtilityCode.load_cached("PyObjectCallNoArg", "ObjectHandling.c")) UtilityCode.load_cached("PyObjectFastCall", "ObjectHandling.c"))
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyObjectCallOneArg", "ObjectHandling.c")) code.putln("{")
code.putln( code.putln("PyObject *__pyx_callargs[%d] = {%s, %s};" % (
"%s = (%s) ? __Pyx_PyObject_CallOneArg(%s, %s) : __Pyx_PyObject_CallNoArg(%s);" % (
self.result(), self_arg,
function, self_arg,
function))
code.put_xdecref_clear(self_arg, py_object_type)
code.funcstate.release_temp(self_arg)
code.putln(code.error_goto_if_null(self.result(), self.pos))
code.put_gotref(self.py_result())
elif len(args) == 1:
# fastest special case: try to avoid tuple creation
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyObjectCall2Args", "ObjectHandling.c"))
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyObjectCallOneArg", "ObjectHandling.c"))
arg = args[0]
code.putln(
"%s = (%s) ? __Pyx_PyObject_Call2Args(%s, %s, %s) : __Pyx_PyObject_CallOneArg(%s, %s);" % (
self.result(), self_arg,
function, self_arg, arg.py_result(),
function, arg.py_result()))
code.put_xdecref_clear(self_arg, py_object_type)
code.funcstate.release_temp(self_arg)
arg.generate_disposal_code(code)
arg.free_temps(code)
code.putln(code.error_goto_if_null(self.result(), self.pos))
code.put_gotref(self.py_result())
else:
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyFunctionFastCall", "ObjectHandling.c"))
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyCFunctionFastCall", "ObjectHandling.c"))
for test_func, call_prefix in [('PyFunction_Check', 'Py'), ('__Pyx_PyFastCFunction_Check', 'PyC')]:
code.putln("#if CYTHON_FAST_%sCALL" % call_prefix.upper())
code.putln("if (%s(%s)) {" % (test_func, function))
code.putln("PyObject *%s[%d] = {%s, %s};" % (
Naming.quick_temp_cname,
len(args)+1, len(args)+1,
self_arg, self_arg,
', '.join(arg.py_result() for arg in args))) ', '.join(arg.py_result() for arg in args)))
code.putln("%s = __Pyx_%sFunction_FastCall(%s, %s+1-%s, %d+%s); %s" % ( code.putln("%s = __Pyx_PyObject_FastCall(%s, __pyx_callargs+1-%s, %d+%s);" % (
self.result(), self.result(),
call_prefix,
function, function,
Naming.quick_temp_cname,
arg_offset_cname, arg_offset_cname,
len(args), len(args),
arg_offset_cname, arg_offset_cname))
code.error_goto_if_null(self.result(), self.pos)))
code.put_xdecref_clear(self_arg, py_object_type)
code.put_gotref(self.py_result())
for arg in args:
arg.generate_disposal_code(code)
code.putln("} else")
code.putln("#endif")
code.putln("{") code.put_xdecref_clear(self_arg, py_object_type)
args_tuple = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
code.putln("%s = PyTuple_New(%d+%s); %s" % (
args_tuple, len(args), arg_offset_cname,
code.error_goto_if_null(args_tuple, self.pos)))
code.put_gotref(args_tuple)
if len(args) > 1:
code.putln("if (%s) {" % self_arg)
code.putln("__Pyx_GIVEREF(%s); PyTuple_SET_ITEM(%s, 0, %s); %s = NULL;" % (
self_arg, args_tuple, self_arg, self_arg)) # stealing owned ref in this case
code.funcstate.release_temp(self_arg) code.funcstate.release_temp(self_arg)
if len(args) > 1:
code.putln("}")
for i, arg in enumerate(args):
arg.make_owned_reference(code)
code.put_giveref(arg.py_result())
code.putln("PyTuple_SET_ITEM(%s, %d+%s, %s);" % (
args_tuple, i, arg_offset_cname, arg.py_result()))
if len(args) > 1:
code.funcstate.release_temp(arg_offset_cname)
for arg in args: for arg in args:
arg.generate_post_assignment_code(code) arg.generate_disposal_code(code)
arg.free_temps(code) arg.free_temps(code)
code.putln(code.error_goto_if_null(self.result(), self.pos))
code.globalstate.use_utility_code(
UtilityCode.load_cached("PyObjectCall", "ObjectHandling.c"))
code.putln(
"%s = __Pyx_PyObject_Call(%s, %s, NULL); %s" % (
self.result(),
function, args_tuple,
code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result()) code.put_gotref(self.py_result())
code.put_decref_clear(args_tuple, py_object_type)
code.funcstate.release_temp(args_tuple)
if len(args) == 1:
code.putln("}")
code.putln("}") # !CYTHON_FAST_PYCALL
if reuse_function_temp: if reuse_function_temp:
self.function.generate_disposal_code(code) self.function.generate_disposal_code(code)
self.function.free_temps(code) self.function.free_temps(code)
else: else:
code.put_decref_clear(function, py_object_type) code.put_decref_clear(function, py_object_type)
code.funcstate.release_temp(function) code.funcstate.release_temp(function)
code.putln("}")
class InlinedDefNodeCallNode(CallNode): class InlinedDefNodeCallNode(CallNode):
......
...@@ -194,14 +194,6 @@ static PyObject *__Pyx__Coroutine_GetAwaitableIter(PyObject *obj) { ...@@ -194,14 +194,6 @@ static PyObject *__Pyx__Coroutine_GetAwaitableIter(PyObject *obj) {
PyObject *method = NULL; PyObject *method = NULL;
int is_method = __Pyx_PyObject_GetMethod(obj, PYIDENT("__await__"), &method); int is_method = __Pyx_PyObject_GetMethod(obj, PYIDENT("__await__"), &method);
if (likely(is_method)) { if (likely(is_method)) {
#if PY_VERSION_HEX >= 0x030700A1 && CYTHON_UNPACK_METHODS && CYTHON_FAST_PYCCALL
if (Py_TYPE(method) == &PyMethodDescr_Type) {
PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
res = _PyMethodDef_RawFastCallKeywords(descr->d_method, obj, NULL, 0, NULL);
if (unlikely(!res))
res = _Py_CheckFunctionResult(obj, res, NULL);
} else
#endif
res = __Pyx_PyObject_CallOneArg(method, obj); res = __Pyx_PyObject_CallOneArg(method, obj);
} else if (likely(method)) { } else if (likely(method)) {
res = __Pyx_PyObject_CallNoArg(method); res = __Pyx_PyObject_CallNoArg(method);
......
...@@ -198,6 +198,10 @@ ...@@ -198,6 +198,10 @@
#define CYTHON_FAST_PYCCALL (CYTHON_FAST_PYCALL && PY_VERSION_HEX >= 0x030600B1) #define CYTHON_FAST_PYCCALL (CYTHON_FAST_PYCALL && PY_VERSION_HEX >= 0x030600B1)
#endif #endif
#if !defined(CYTHON_VECTORCALL)
#define CYTHON_VECTORCALL (CYTHON_FAST_PYCCALL && PY_VERSION_HEX >= 0x030800B1)
#endif
#if CYTHON_USE_PYLONG_INTERNALS #if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h" #include "longintrepr.h"
/* These short defines can easily conflict with other code */ /* These short defines can easily conflict with other code */
...@@ -444,12 +448,6 @@ class __Pyx_FakeReference { ...@@ -444,12 +448,6 @@ class __Pyx_FakeReference {
#define __Pyx_PyCFunctionFast _PyCFunctionFast #define __Pyx_PyCFunctionFast _PyCFunctionFast
#define __Pyx_PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords #define __Pyx_PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords
#endif #endif
#if CYTHON_FAST_PYCCALL
#define __Pyx_PyFastCFunction_Check(func) \
((PyCFunction_Check(func) && (METH_FASTCALL == (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS)))))
#else
#define __Pyx_PyFastCFunction_Check(func) 0
#endif
#if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc)
#define PyObject_Malloc(s) PyMem_Malloc(s) #define PyObject_Malloc(s) PyMem_Malloc(s)
......
This diff is collapsed.
...@@ -555,3 +555,19 @@ def for_in_iteritems_of_expression(*args, **kwargs): ...@@ -555,3 +555,19 @@ def for_in_iteritems_of_expression(*args, **kwargs):
for k, v in dict(*args, **kwargs).iteritems(): for k, v in dict(*args, **kwargs).iteritems():
result.append((k, v)) result.append((k, v))
return result return result
cdef class NotADict:
"""
>>> NotADict().listvalues() # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: descriptor 'values' for 'mappingproxy' objects doesn't apply to a 'iterdict.NotADict' object
"""
cdef long v
def __cinit__(self):
self.v = 1
itervalues = type(object.__dict__).values
def listvalues(self):
return [v for v in self.itervalues()]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment