assert(!_PyType_HasFeature(metatype, Py_TPFLAGS_INLINE_VALUES));
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))
+ {
+ PyUnstable_Object_EnableDeferredRefcount(value);
+ }
+#endif
+
PyObject *old_value = NULL;
PyObject *descr = _PyType_LookupRef(metatype, name);
if (descr != NULL) {
import sys
import threading
import time
+from dataclasses import dataclass
# The iterations in individual benchmarks are scaled by this factor.
WORK_SCALE = 100
_ = tmp.x
+@dataclass
+class MyDataClass:
+ x: int
+ y: int
+ z: int
+
+@register_benchmark
+def instantiate_dataclass():
+ for _ in range(1000 * WORK_SCALE):
+ obj = MyDataClass(x=1, y=2, z=3)
+
def bench_one_thread(func):
t0 = time.perf_counter_ns()
func()