Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Use _PyObject_GetMethodStackRef for slot_tp_new
  • Loading branch information
eendebakpt committed Nov 15, 2025
commit 9ca166c171423184e28f74a782dec2bf46b25760
13 changes: 10 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10852,13 +10852,20 @@ 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);
int meth_found = _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);
//Py_DECREF(func);
_PyThreadState_PopCStackRef(tstate, &c_stackref);
return result;
}

Expand Down
24 changes: 23 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,31 @@
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 namedtuple():
for i in range(1000 * WORK_SCALE):
_ = Foo(x=1)

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