Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of concurrent object construction by using ``_PyObject_GetMethodStackRef`` in ``slot_tp_new``.
13 changes: 9 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10888,13 +10888,18 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyThreadState *tstate = _PyThreadState_GET();
PyObject *func, *result;

func = PyObject_GetAttr((PyObject *)type, &_Py_ID(__new__));
if (func == NULL) {
_PyCStackRef c_stackref;
_PyThreadState_PushCStackRef(tstate, &c_stackref);
_PyObject_GetMethodStackRef(tstate, (PyObject *)type, &_Py_ID(__new__), &c_stackref.ref);

if (PyStackRef_IsNull(c_stackref.ref)) {
_PyThreadState_PopCStackRef(tstate, &c_stackref);
return NULL;
}

func = PyStackRef_AsPyObjectBorrow(c_stackref.ref);
assert(func != NULL);
result = _PyObject_Call_Prepend(tstate, func, (PyObject *)type, args, kwds);
Py_DECREF(func);
_PyThreadState_PopCStackRef(tstate, &c_stackref);
return result;
}

Expand Down
19 changes: 18 additions & 1 deletion Tools/ftscalingbench/ftscalingbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import threading
import time
from operator import methodcaller
from typing import NamedTuple
from collections import namedtuple

# The iterations in individual benchmarks are scaled by this factor.
WORK_SCALE = 100
Expand All @@ -38,11 +40,26 @@
in_queues = []
out_queues = []


def register_benchmark(func):
ALL_BENCHMARKS[func.__name__] = func
return func

class Foo(NamedTuple):
x: int


Bar = namedtuple('Bar', ['x'])

@register_benchmark
def typing_namedtuple():
for i in range(1000 * WORK_SCALE):
_ = Foo(x=1)

@register_benchmark
def namedtuple():
for i in range(1000 * WORK_SCALE):
_ = Bar(x=1)

@register_benchmark
def object_cfunction():
accu = 0
Expand Down
Loading