Skip to content
Merged
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
36 changes: 25 additions & 11 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4841,6 +4841,18 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type)
if (type_new_set_classdictcell(dict) < 0) {
return -1;
}

#ifdef Py_GIL_DISABLED
// enable deferred reference counting on functions and descriptors
Py_ssize_t pos = 0;
PyObject *key, *value;
while (PyDict_Next(dict, &pos, &key, &value)) {
if (PyFunction_Check(value) || Py_TYPE(value)->tp_descr_get != NULL) {
PyUnstable_Object_EnableDeferredRefcount(value);
}
}
#endif

return 0;
}

Expand Down Expand Up @@ -6746,12 +6758,11 @@ type_setattro(PyObject *self, PyObject *name, PyObject *value)
assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_MANAGED_DICT));

#ifdef Py_GIL_DISABLED
// gh-139103: Enable deferred refcounting for functions assigned
// to type objects. This is important for `dataclass.__init__`,
// which is generated dynamically.
if (value != NULL &&
PyFunction_Check(value) &&
!_PyObject_HasDeferredRefcount(value))
// gh-139103: Enable deferred refcounting for functions and descriptors
// assigned to type objects. This is important for `dataclass.__init__`,
// which is generated dynamically, and for descriptor scaling on
// free-threaded builds.
if (value != NULL && (PyFunction_Check(value) || Py_TYPE(value)->tp_descr_get != NULL))
{
PyUnstable_Object_EnableDeferredRefcount(value);
}
Expand Down Expand Up @@ -11089,10 +11100,12 @@ static PyObject *
slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject *get;

get = _PyType_LookupRef(tp, &_Py_ID(__get__));
if (get == NULL) {
PyThreadState *tstate = _PyThreadState_GET();
_PyCStackRef cref;
_PyThreadState_PushCStackRef(tstate, &cref);
_PyType_LookupStackRefAndVersion(tp, &_Py_ID(__get__), &cref.ref);
if (PyStackRef_IsNull(cref.ref)) {
_PyThreadState_PopCStackRef(tstate, &cref);
#ifndef Py_GIL_DISABLED
/* Avoid further slowdowns */
if (tp->tp_descr_get == slot_tp_descr_get)
Expand All @@ -11104,9 +11117,10 @@ slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
obj = Py_None;
if (type == NULL)
type = Py_None;
PyObject *get = PyStackRef_AsPyObjectBorrow(cref.ref);
PyObject *stack[3] = {self, obj, type};
PyObject *res = PyObject_Vectorcall(get, stack, 3, NULL);
Py_DECREF(get);
_PyThreadState_PopCStackRef(tstate, &cref);
return res;
}

Expand Down
17 changes: 17 additions & 0 deletions Tools/ftscalingbench/ftscalingbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ def staticmethod_call():
for _ in range(1000 * WORK_SCALE):
obj.my_staticmethod()


class MyDescriptor:
def __get__(self, obj, objtype=None):
return 42

def __set__(self, obj, value):
pass

class MyClassWithDescriptor:
attr = MyDescriptor()

@register_benchmark
def descriptor():
obj = MyClassWithDescriptor()
for _ in range(1000 * WORK_SCALE):
obj.attr

@register_benchmark
def deepcopy():
x = {'list': [1, 2], 'tuple': (1, None)}
Expand Down
Loading