FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Draft: gh-139103: Use _PyObject_GetMethodStackRef for slot_tp_new by eendebakpt · Pull Request #141603 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL