Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8c0432

Browse files
committed
Move body of CALL_FUNCTION opcode into helper function.
This makes the code much easier to ready, because it is at a sane indentation level. On my box this shows a 1-2% speedup, which means nothing, except that I'm not going to worry about the performance effects of the change.
1 parent 84b2bed commit e8c0432

1 file changed

Lines changed: 56 additions & 54 deletions

File tree

Python/ceval.c

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
3333

3434
/* Forward declarations */
3535
static PyObject *eval_frame(PyFrameObject *);
36+
static PyObject *call_function(PyObject ***, int);
3637
static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
3738
static PyObject *fast_cfunction(PyObject *, PyObject ***, int);
3839
static PyObject *do_call(PyObject *, PyObject ***, int, int);
@@ -1964,60 +1965,11 @@ eval_frame(PyFrameObject *f)
19641965
continue;
19651966

19661967
case CALL_FUNCTION:
1967-
{
1968-
int na = oparg & 0xff;
1969-
int nk = (oparg>>8) & 0xff;
1970-
int n = na + 2 * nk;
1971-
PyObject **pfunc = stack_pointer - n - 1;
1972-
PyObject *func = *pfunc;
1973-
1974-
/* Always dispatch PyCFunction first, because
1975-
these are presumed to be the most frequent
1976-
callable object.
1977-
*/
1978-
if (PyCFunction_Check(func) && nk == 0) {
1979-
int flags = PyCFunction_GET_FLAGS(func);
1980-
if (flags & (METH_VARARGS | METH_KEYWORDS)) {
1981-
PyObject *callargs;
1982-
callargs = load_args(&stack_pointer, na);
1983-
x = PyCFunction_Call(func, callargs, NULL);
1984-
Py_XDECREF(callargs);
1985-
} else
1986-
x = fast_cfunction(func,
1987-
&stack_pointer, na);
1988-
} else {
1989-
if (PyMethod_Check(func)
1990-
&& PyMethod_GET_SELF(func) != NULL) {
1991-
/* optimize access to bound methods */
1992-
PyObject *self = PyMethod_GET_SELF(func);
1993-
Py_INCREF(self);
1994-
func = PyMethod_GET_FUNCTION(func);
1995-
Py_INCREF(func);
1996-
Py_DECREF(*pfunc);
1997-
*pfunc = self;
1998-
na++;
1999-
n++;
2000-
} else
2001-
Py_INCREF(func);
2002-
if (PyFunction_Check(func)) {
2003-
x = fast_function(func, &stack_pointer,
2004-
n, na, nk);
2005-
} else {
2006-
x = do_call(func, &stack_pointer,
2007-
na, nk);
2008-
}
2009-
Py_DECREF(func);
2010-
}
2011-
2012-
while (stack_pointer > pfunc) {
2013-
w = POP();
2014-
Py_DECREF(w);
2015-
}
2016-
PUSH(x);
2017-
if (x != NULL)
2018-
continue;
2019-
break;
2020-
}
1968+
x = call_function(&stack_pointer, oparg);
1969+
PUSH(x);
1970+
if (x != NULL)
1971+
continue;
1972+
break;
20211973

20221974
case CALL_FUNCTION_VAR:
20231975
case CALL_FUNCTION_KW:
@@ -3194,6 +3146,56 @@ PyEval_GetFuncDesc(PyObject *func)
31943146

31953147
#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
31963148

3149+
static PyObject *
3150+
call_function(PyObject ***pp_stack, int oparg)
3151+
{
3152+
int na = oparg & 0xff;
3153+
int nk = (oparg>>8) & 0xff;
3154+
int n = na + 2 * nk;
3155+
PyObject **pfunc = (*pp_stack) - n - 1;
3156+
PyObject *func = *pfunc;
3157+
PyObject *x, *w;
3158+
3159+
/* Always dispatch PyCFunction first, because
3160+
these are presumed to be the most frequent
3161+
callable object.
3162+
*/
3163+
if (PyCFunction_Check(func) && nk == 0) {
3164+
int flags = PyCFunction_GET_FLAGS(func);
3165+
if (flags & (METH_VARARGS | METH_KEYWORDS)) {
3166+
PyObject *callargs;
3167+
callargs = load_args(pp_stack, na);
3168+
x = PyCFunction_Call(func, callargs, NULL);
3169+
Py_XDECREF(callargs);
3170+
} else
3171+
x = fast_cfunction(func, pp_stack, na);
3172+
} else {
3173+
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3174+
/* optimize access to bound methods */
3175+
PyObject *self = PyMethod_GET_SELF(func);
3176+
Py_INCREF(self);
3177+
func = PyMethod_GET_FUNCTION(func);
3178+
Py_INCREF(func);
3179+
Py_DECREF(*pfunc);
3180+
*pfunc = self;
3181+
na++;
3182+
n++;
3183+
} else
3184+
Py_INCREF(func);
3185+
if (PyFunction_Check(func))
3186+
x = fast_function(func, pp_stack, n, na, nk);
3187+
else
3188+
x = do_call(func, pp_stack, na, nk);
3189+
Py_DECREF(func);
3190+
}
3191+
3192+
while ((*pp_stack) > pfunc) {
3193+
w = EXT_POP(*pp_stack);
3194+
Py_DECREF(w);
3195+
}
3196+
return x;
3197+
}
3198+
31973199
/* The two fast_xxx() functions optimize calls for which no argument
31983200
tuple is necessary; the objects are passed directly from the stack.
31993201
fast_cfunction() is called for METH_OLDARGS functions.

0 commit comments

Comments
 (0)