Skip to content

Commit e07e291

Browse files
committed
Defer reference count mutable class attributes
Mutable class attributes would not scale with more threads, adding defered reference counting to resolve it. ./python_main.exe Tools/ftscalingbench/ftscalingbench.py class_attribute Running benchmarks with 18 threads class_attribute 6.3x slower ./python.exe Tools/ftscalingbench/ftscalingbench.py class_attribute Running benchmarks with 18 threads class_attribute 8.0x faster
1 parent 9d231cb commit e07e291

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Python/specialize.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,9 @@ specialize_class_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
12161216
// special case for enums which has Py_TYPE(descr) == cls
12171217
// so guarding on type version is sufficient
12181218
if (Py_TYPE(descr) != cls) {
1219+
#ifdef Py_GIL_DISABLED
1220+
maybe_enable_deferred_ref_count(descr);
1221+
#endif
12191222
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_MUTABLE_CLASS);
12201223
Py_XDECREF(descr);
12211224
return -1;

Tools/ftscalingbench/ftscalingbench.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,25 @@ def enum_attr():
326326
MyEnum.Z
327327

328328

329+
class _SharedAttrValue:
330+
pass
331+
332+
class MyClassWithSharedAttr:
333+
# A class attribute whose value is an instance of a *different* class.
334+
# Reading it from multiple threads did not scale because LOAD_ATTR_CLASS
335+
# cannot specialize this case (the value's type is not the owner class),
336+
# leaving the shared value's reference count contended on every read.
337+
attr = _SharedAttrValue()
338+
339+
@register_benchmark
340+
def class_attribute():
341+
obj = MyClassWithSharedAttr
342+
for _ in range(1000 * WORK_SCALE):
343+
obj.attr
344+
obj.attr
345+
obj.attr
346+
347+
329348
def bench_one_thread(func):
330349
t0 = time.perf_counter_ns()
331350
func()

0 commit comments

Comments
 (0)