call_method() and call_maybe() now use fast call

Issue #27128. The call_method() and call_maybe() functions of typeobject.c now
use fast call for empty format string to avoid the creation of a temporary
empty tuple.
This commit is contained in:
Victor Stinner 2016-08-19 18:05:37 +02:00
parent 94463c980e
commit f736c261a2
1 changed files with 26 additions and 20 deletions

View File

@ -1424,7 +1424,7 @@ static PyObject *
call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...) call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
{ {
va_list va; va_list va;
PyObject *args, *func = 0, *retval; PyObject *func = NULL, *retval;
func = lookup_maybe(o, nameid); func = lookup_maybe(o, nameid);
if (func == NULL) { if (func == NULL) {
@ -1434,22 +1434,25 @@ call_method(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
} }
if (format && *format) { if (format && *format) {
PyObject *args;
va_start(va, format); va_start(va, format);
args = Py_VaBuildValue(format, va); args = Py_VaBuildValue(format, va);
va_end(va); va_end(va);
if (args == NULL) {
Py_DECREF(func);
return NULL;
}
assert(PyTuple_Check(args));
retval = PyObject_Call(func, args, NULL);
Py_DECREF(args);
} }
else { else {
args = PyTuple_New(0); retval = _PyObject_FastCall(func, NULL, 0, NULL);
}
if (args == NULL) {
Py_DECREF(func);
return NULL;
} }
assert(PyTuple_Check(args));
retval = PyObject_Call(func, args, NULL);
Py_DECREF(args);
Py_DECREF(func); Py_DECREF(func);
return retval; return retval;
@ -1461,7 +1464,7 @@ static PyObject *
call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...) call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
{ {
va_list va; va_list va;
PyObject *args, *func = 0, *retval; PyObject *func = NULL, *retval;
func = lookup_maybe(o, nameid); func = lookup_maybe(o, nameid);
if (func == NULL) { if (func == NULL) {
@ -1471,22 +1474,25 @@ call_maybe(PyObject *o, _Py_Identifier *nameid, const char *format, ...)
} }
if (format && *format) { if (format && *format) {
PyObject *args;
va_start(va, format); va_start(va, format);
args = Py_VaBuildValue(format, va); args = Py_VaBuildValue(format, va);
va_end(va); va_end(va);
if (args == NULL) {
Py_DECREF(func);
return NULL;
}
assert(PyTuple_Check(args));
retval = PyObject_Call(func, args, NULL);
Py_DECREF(args);
} }
else { else {
args = PyTuple_New(0); retval = _PyObject_FastCall(func, NULL, 0, NULL);
}
if (args == NULL) {
Py_DECREF(func);
return NULL;
} }
assert(PyTuple_Check(args));
retval = PyObject_Call(func, args, NULL);
Py_DECREF(args);
Py_DECREF(func); Py_DECREF(func);
return retval; return retval;