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
20 changes: 15 additions & 5 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6289,6 +6289,13 @@ _PyType_LookupStackRefAndVersion(PyTypeObject *type, PyObject *name, _PyStackRef

PyObject *res_obj = PyStackRef_AsPyObjectBorrow(*out);
#if Py_GIL_DISABLED
// Optimistically enable deferred refcounting on descriptors
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in type_setattro? That seems like a more logical place to me, and it's where we enable deferred reference counting on function objects.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that only work when the descriptor is assigned to the class after class is already created?

if (res_obj != NULL &&
PyType_IS_GC(Py_TYPE(res_obj)) &&
!_PyObject_HasDeferredRefcount(res_obj) &&
Py_TYPE(res_obj)->tp_descr_get != NULL) {
PyUnstable_Object_EnableDeferredRefcount(res_obj);
}
update_cache_gil_disabled(entry, name, version_tag, res_obj);
#else
PyObject *old_value = update_cache(entry, name, version_tag, res_obj);
Expand Down Expand Up @@ -10995,10 +11002,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 @@ -11010,9 +11019,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