Skip to content

Commit 27580c1

Browse files
committed
Replace PyObject_CallFunctionObjArgs() with fastcall
* PyObject_CallFunctionObjArgs(func, NULL) => _PyObject_CallNoArg(func) * PyObject_CallFunctionObjArgs(func, arg, NULL) => _PyObject_CallArg1(func, arg) PyObject_CallFunctionObjArgs() allocates 40 bytes on the C stack and requires extra work to "parse" C arguments to build a C array of PyObject*. _PyObject_CallNoArg() and _PyObject_CallArg1() are simpler and don't allocate memory on the C stack. This change is part of the fastcall project. The change on listsort() is related to the issue #23507.
1 parent 8be1c39 commit 27580c1

33 files changed

+71
-83
lines changed

Modules/_asynciomodule.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,7 @@ static PyObject *
721721
_asyncio_Future__repr_info_impl(FutureObj *self)
722722
/*[clinic end generated code: output=fa69e901bd176cfb input=f21504d8e2ae1ca2]*/
723723
{
724-
return PyObject_CallFunctionObjArgs(
725-
asyncio_future_repr_info_func, self, NULL);
724+
return _PyObject_CallArg1(asyncio_future_repr_info_func, self);
726725
}
727726

728727
/*[clinic input]
@@ -1535,8 +1534,7 @@ static PyObject *
15351534
_asyncio_Task__repr_info_impl(TaskObj *self)
15361535
/*[clinic end generated code: output=6a490eb66d5ba34b input=3c6d051ed3ddec8b]*/
15371536
{
1538-
return PyObject_CallFunctionObjArgs(
1539-
asyncio_task_repr_info_func, self, NULL);
1537+
return _PyObject_CallArg1(asyncio_task_repr_info_func, self);
15401538
}
15411539

15421540
/*[clinic input]
@@ -1896,7 +1894,7 @@ task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...)
18961894
return NULL;
18971895
}
18981896

1899-
PyObject *e = PyObject_CallFunctionObjArgs(et, msg, NULL);
1897+
PyObject *e = _PyObject_CallArg1(et, msg);
19001898
Py_DECREF(msg);
19011899
if (e == NULL) {
19021900
return NULL;
@@ -1946,7 +1944,7 @@ task_step_impl(TaskObj *task, PyObject *exc)
19461944

19471945
if (!exc) {
19481946
/* exc was not a CancelledError */
1949-
exc = PyObject_CallFunctionObjArgs(asyncio_CancelledError, NULL);
1947+
exc = _PyObject_CallNoArg(asyncio_CancelledError);
19501948
if (!exc) {
19511949
goto fail;
19521950
}
@@ -2176,7 +2174,7 @@ task_step_impl(TaskObj *task, PyObject *exc)
21762174
}
21772175

21782176
/* Check if `result` is a generator */
2179-
o = PyObject_CallFunctionObjArgs(inspect_isgenerator, result, NULL);
2177+
o = _PyObject_CallArg1(inspect_isgenerator, result);
21802178
if (o == NULL) {
21812179
/* An exception in inspect.isgenerator */
21822180
goto fail;

Modules/_csv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ csv_writerow(WriterObj *self, PyObject *seq)
12591259
(void *) self->rec, self->rec_len);
12601260
if (line == NULL)
12611261
return NULL;
1262-
result = PyObject_CallFunctionObjArgs(self->writeline, line, NULL);
1262+
result = _PyObject_CallArg1(self->writeline, line);
12631263
Py_DECREF(line);
12641264
return result;
12651265
}

Modules/_elementtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,7 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
24572457
PyObject *event = PyTuple_Pack(2, action, node);
24582458
if (event == NULL)
24592459
return -1;
2460-
res = PyObject_CallFunctionObjArgs(self->events_append, event, NULL);
2460+
res = _PyObject_CallArg1(self->events_append, event);
24612461
Py_DECREF(event);
24622462
if (res == NULL)
24632463
return -1;

Modules/_json.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,14 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
813813
*next_idx_ptr = idx + 1;
814814

815815
if (has_pairs_hook) {
816-
val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
816+
val = _PyObject_CallArg1(s->object_pairs_hook, rval);
817817
Py_DECREF(rval);
818818
return val;
819819
}
820820

821821
/* if object_hook is not None: rval = object_hook(rval) */
822822
if (s->object_hook != Py_None) {
823-
val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
823+
val = _PyObject_CallArg1(s->object_hook, rval);
824824
Py_DECREF(rval);
825825
return val;
826826
}
@@ -924,7 +924,7 @@ _parse_constant(PyScannerObject *s, const char *constant, Py_ssize_t idx, Py_ssi
924924
return NULL;
925925

926926
/* rval = parse_constant(constant) */
927-
rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
927+
rval = _PyObject_CallArg1(s->parse_constant, cstr);
928928
idx += PyUnicode_GET_LENGTH(cstr);
929929
Py_DECREF(cstr);
930930
*next_idx_ptr = idx;
@@ -1023,7 +1023,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
10231023
idx - start);
10241024
if (numstr == NULL)
10251025
return NULL;
1026-
rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
1026+
rval = _PyObject_CallArg1(custom_func, numstr);
10271027
}
10281028
else {
10291029
Py_ssize_t i, n;
@@ -1475,7 +1475,7 @@ encoder_encode_string(PyEncoderObject *s, PyObject *obj)
14751475
if (s->fast_encode)
14761476
return s->fast_encode(NULL, obj);
14771477
else
1478-
return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
1478+
return _PyObject_CallArg1(s->encoder, obj);
14791479
}
14801480

14811481
static int
@@ -1553,7 +1553,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
15531553
return -1;
15541554
}
15551555
}
1556-
newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
1556+
newobj = _PyObject_CallArg1(s->defaultfn, obj);
15571557
if (newobj == NULL) {
15581558
Py_XDECREF(ident);
15591559
return -1;

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3197,7 +3197,7 @@ _password_callback(char *buf, int size, int rwflag, void *userdata)
31973197
PySSL_END_ALLOW_THREADS_S(pw_info->thread_state);
31983198

31993199
if (pw_info->callable) {
3200-
fn_ret = PyObject_CallFunctionObjArgs(pw_info->callable, NULL);
3200+
fn_ret = _PyObject_CallNoArg(pw_info->callable);
32013201
if (!fn_ret) {
32023202
/* TODO: It would be nice to move _ctypes_add_traceback() into the
32033203
core python API, so we could use it to add a frame here */

Modules/_struct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ cache_struct(PyObject *fmt)
20462046
return s_object;
20472047
}
20482048

2049-
s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
2049+
s_object = _PyObject_CallArg1((PyObject *)(&PyStructType), fmt);
20502050
if (s_object != NULL) {
20512051
if (PyDict_Size(cache) >= MAXCACHE)
20522052
PyDict_Clear(cache);

Modules/_testbuffer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pack_from_list(PyObject *obj, PyObject *items, PyObject *format,
312312
assert(PyObject_CheckBuffer(obj));
313313
assert(PyList_Check(items) || PyTuple_Check(items));
314314

315-
structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL);
315+
structobj = _PyObject_CallArg1(Struct, format);
316316
if (structobj == NULL)
317317
return -1;
318318

@@ -406,7 +406,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt, Py_ssize_t itemsize)
406406
if (format == NULL)
407407
goto out;
408408

409-
structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL);
409+
structobj = _PyObject_CallArg1(Struct, format);
410410
if (structobj == NULL)
411411
goto out;
412412

@@ -620,7 +620,7 @@ unpack_rec(PyObject *unpack_from, char *ptr, PyObject *mview, char *item,
620620

621621
if (ndim == 0) {
622622
memcpy(item, ptr, itemsize);
623-
x = PyObject_CallFunctionObjArgs(unpack_from, mview, NULL);
623+
x = _PyObject_CallArg1(unpack_from, mview);
624624
if (x == NULL)
625625
return NULL;
626626
if (PyTuple_GET_SIZE(x) == 1) {
@@ -696,7 +696,7 @@ ndarray_as_list(NDArrayObject *nd)
696696
if (format == NULL)
697697
goto out;
698698

699-
structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL);
699+
structobj = _PyObject_CallArg1(Struct, format);
700700
Py_DECREF(format);
701701
if (structobj == NULL)
702702
goto out;
@@ -788,7 +788,7 @@ get_itemsize(PyObject *format)
788788
PyObject *tmp;
789789
Py_ssize_t itemsize;
790790

791-
tmp = PyObject_CallFunctionObjArgs(calcsize, format, NULL);
791+
tmp = _PyObject_CallArg1(calcsize, format);
792792
if (tmp == NULL)
793793
return -1;
794794
itemsize = PyLong_AsSsize_t(tmp);

Modules/gcmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
709709
assert(callback != NULL);
710710

711711
/* copy-paste of weakrefobject.c's handle_callback() */
712-
temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
712+
temp = _PyObject_CallArg1(callback, wr);
713713
if (temp == NULL)
714714
PyErr_WriteUnraisable(callback);
715715
else

Modules/itertoolsmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ groupby_next(groupbyobject *gbo)
101101
newkey = newvalue;
102102
Py_INCREF(newvalue);
103103
} else {
104-
newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
104+
newkey = _PyObject_CallArg1(gbo->keyfunc, newvalue);
105105
if (newkey == NULL) {
106106
Py_DECREF(newvalue);
107107
return NULL;
@@ -293,7 +293,7 @@ _grouper_next(_grouperobject *igo)
293293
newkey = newvalue;
294294
Py_INCREF(newvalue);
295295
} else {
296-
newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
296+
newkey = _PyObject_CallArg1(gbo->keyfunc, newvalue);
297297
if (newkey == NULL) {
298298
Py_DECREF(newvalue);
299299
return NULL;
@@ -1130,7 +1130,7 @@ dropwhile_next(dropwhileobject *lz)
11301130
if (lz->start == 1)
11311131
return item;
11321132

1133-
good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
1133+
good = _PyObject_CallArg1(lz->func, item);
11341134
if (good == NULL) {
11351135
Py_DECREF(item);
11361136
return NULL;
@@ -1296,7 +1296,7 @@ takewhile_next(takewhileobject *lz)
12961296
if (item == NULL)
12971297
return NULL;
12981298

1299-
good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
1299+
good = _PyObject_CallArg1(lz->func, item);
13001300
if (good == NULL) {
13011301
Py_DECREF(item);
13021302
return NULL;
@@ -3824,7 +3824,7 @@ filterfalse_next(filterfalseobject *lz)
38243824
ok = PyObject_IsTrue(item);
38253825
} else {
38263826
PyObject *good;
3827-
good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
3827+
good = _PyObject_CallArg1(lz->func, item);
38283828
if (good == NULL) {
38293829
Py_DECREF(item);
38303830
return NULL;

Modules/mathmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ static PyObject * math_ceil(PyObject *self, PyObject *number) {
951951
return NULL;
952952
return math_1_to_int(number, ceil, 0);
953953
}
954-
result = PyObject_CallFunctionObjArgs(method, NULL);
954+
result = _PyObject_CallNoArg(method);
955955
Py_DECREF(method);
956956
return result;
957957
}
@@ -991,7 +991,7 @@ static PyObject * math_floor(PyObject *self, PyObject *number) {
991991
return NULL;
992992
return math_1_to_int(number, floor, 0);
993993
}
994-
result = PyObject_CallFunctionObjArgs(method, NULL);
994+
result = _PyObject_CallNoArg(method);
995995
Py_DECREF(method);
996996
return result;
997997
}
@@ -1542,7 +1542,7 @@ math_trunc(PyObject *self, PyObject *number)
15421542
Py_TYPE(number)->tp_name);
15431543
return NULL;
15441544
}
1545-
result = PyObject_CallFunctionObjArgs(trunc, NULL);
1545+
result = _PyObject_CallNoArg(trunc);
15461546
Py_DECREF(trunc);
15471547
return result;
15481548
}

0 commit comments

Comments
 (0)