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

Skip to content

Commit 84b2bed

Browse files
committed
Squash a few calls to the hideously expensive PyObject_CallObject(o,a)
-- replace then with slightly faster PyObject_Call(o,a,NULL). (The difference is that the latter requires a to be a tuple; the former allows other values and wraps them in a tuple if necessary; it involves two more levels of C function calls to accomplish all that.)
1 parent c13f724 commit 84b2bed

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

Modules/pyexpat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ readinst(char *buf, int buf_size, PyObject *meth)
829829

830830
PyTuple_SET_ITEM(arg, 0, bytes);
831831

832-
if ((str = PyObject_CallObject(meth, arg)) == NULL)
832+
if ((str = PyObject_Call(meth, arg, NULL)) == NULL)
833833
goto finally;
834834

835835
/* XXX what to do if it returns a Unicode string? */

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ PyObject_CallFunction(PyObject *callable, char *format, ...)
17271727
return NULL;
17281728
args = a;
17291729
}
1730-
retval = PyObject_CallObject(callable, args);
1730+
retval = PyObject_Call(callable, args, NULL);
17311731

17321732
Py_DECREF(args);
17331733

@@ -1774,7 +1774,7 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
17741774
args = a;
17751775
}
17761776

1777-
retval = PyObject_CallObject(func, args);
1777+
retval = PyObject_Call(func, args, NULL);
17781778

17791779
Py_DECREF(args);
17801780
Py_DECREF(func);

Objects/iterobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ static PyObject *
163163
calliter_iternext(calliterobject *it)
164164
{
165165
if (it->it_callable != NULL) {
166-
PyObject *result = PyObject_CallObject(it->it_callable, NULL);
166+
PyObject *args = PyTuple_New(0);
167+
PyObject *result;
168+
if (args == NULL)
169+
return NULL;
170+
result = PyObject_Call(it->it_callable, args, NULL);
171+
Py_DECREF(args);
167172
if (result != NULL) {
168173
int ok;
169174
ok = PyObject_RichCompareBool(result,

Objects/typeobject.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,7 @@ SLOT0(slot_nb_absolute, "__abs__")
32573257
static int
32583258
slot_nb_nonzero(PyObject *self)
32593259
{
3260-
PyObject *func, *res;
3260+
PyObject *func, *res, *args;
32613261
static PyObject *nonzero_str, *len_str;
32623262

32633263
func = lookup_maybe(self, "__nonzero__", &nonzero_str);
@@ -3272,7 +3272,11 @@ slot_nb_nonzero(PyObject *self)
32723272
return 1;
32733273
}
32743274
}
3275-
res = PyObject_CallObject(func, NULL);
3275+
args = res = PyTuple_New(0);
3276+
if (args != NULL) {
3277+
res = PyObject_Call(func, args, NULL);
3278+
Py_DECREF(args);
3279+
}
32763280
Py_DECREF(func);
32773281
if (res == NULL)
32783282
return -1;
@@ -3651,9 +3655,14 @@ slot_tp_iter(PyObject *self)
36513655

36523656
func = lookup_method(self, "__iter__", &iter_str);
36533657
if (func != NULL) {
3654-
res = PyObject_CallObject(func, NULL);
3655-
Py_DECREF(func);
3656-
return res;
3658+
PyObject *args;
3659+
args = res = PyTuple_New(0);
3660+
if (args != NULL) {
3661+
res = PyObject_Call(func, args, NULL);
3662+
Py_DECREF(args);
3663+
}
3664+
Py_DECREF(func);
3665+
return res;
36573666
}
36583667
PyErr_Clear();
36593668
func = lookup_method(self, "__getitem__", &getitem_str);

Parser/tokenizer.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,16 @@ fp_readl(char *s, int size, struct tok_state *tok)
327327
#ifndef Py_USING_UNICODE
328328
/* In a non-Unicode built, this should never be called. */
329329
Py_FatalError("fp_readl should not be called in this build.");
330-
return NULL;
330+
return NULL; /* Keep compiler happy (not reachable) */
331331
#else
332332
PyObject* utf8;
333333
PyObject* buf = tok->decoding_buffer;
334334
if (buf == NULL) {
335-
buf = PyObject_CallObject(tok->decoding_readline, NULL);
335+
PyObject *args = PyTuple_New(0);
336+
if (args == NULL)
337+
return error_ret(tok);
338+
buf = PyObject_Call(tok->decoding_readline, args, NULL);
339+
Py_DECREF(args);
336340
if (buf == NULL)
337341
return error_ret(tok);
338342
} else {
@@ -464,7 +468,14 @@ decoding_feof(struct tok_state *tok)
464468
} else {
465469
PyObject* buf = tok->decoding_buffer;
466470
if (buf == NULL) {
467-
buf = PyObject_CallObject(tok->decoding_readline, NULL);
471+
PyObject *args = PyTuple_New(0);
472+
if (args == NULL) {
473+
error_ret(tok);
474+
return 1;
475+
}
476+
buf = PyObject_Call(tok->decoding_readline,
477+
args, NULL);
478+
Py_DECREF(args);
468479
if (buf == NULL) {
469480
error_ret(tok);
470481
return 1;

0 commit comments

Comments
 (0)