Skip to content

gh-139103: Defer reference count mutable class attributes - #155876

Open
johng wants to merge 4 commits into
python:mainfrom
johng:worktree-ft-scaling-139103
Open

johng wants to merge 4 commits into
python:mainfrom
johng:worktree-ft-scaling-139103

Conversation

@johng

@johng johng commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Mutable class attributes would not scale with more threads, adding defered reference counting to the non owning threads reduces the churn.

./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

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
@johng
johng marked this pull request as ready for review August 15, 2026 23:33
@johng

johng commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Happy to create a new issue for this if the linked one is not close enough

Add a free-threading test that reads a class attribute holding an
instance of a different class from a non-owning thread and checks that
the failed LOAD_ATTR_CLASS specialization attempt enables deferred
reference counting on the value, and that the value is still reclaimed
by the GC once unreachable.
@johng

johng commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

CC @kumaraditya303 @colesbury thanks!

@johng

johng commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Deferred reference counting was added in the branch where mutable class attributes hit, but maybe this can be done at the top of specialize_class_load_attr if it makes sense for all branches instead

Comment thread Tools/ftscalingbench/ftscalingbench.py Outdated
Comment on lines +329 to +337
class _SharedAttrValue:
pass

class MyClassWithSharedAttr:
# A class attribute whose value is an instance of a *different* class.
# Reading it from multiple threads did not scale because LOAD_ATTR_CLASS
# cannot specialize this case (the value's type is not the owner class),
# leaving the shared value's reference count contended on every read.
attr = _SharedAttrValue()

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.

Suggested change
class _SharedAttrValue:
pass
class MyClassWithSharedAttr:
# A class attribute whose value is an instance of a *different* class.
# Reading it from multiple threads did not scale because LOAD_ATTR_CLASS
# cannot specialize this case (the value's type is not the owner class),
# leaving the shared value's reference count contended on every read.
attr = _SharedAttrValue()
class MyClassWithSharedAttr:
# instance of another heap type: LOAD_ATTR_CLASS can't specialize this
attr = MyObject()

Comment thread Lib/test/test_opcache.py Outdated
def load_class_attr():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
x = Holder.attr
assert x is value

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.

Suggested change
assert x is value

Comment thread Lib/test/test_opcache.py Outdated
Comment on lines +2273 to +2275
del load_class_attr, Holder, value
gc_collect()
self.assertIsNone(wr())

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.

I would drop this part, the PR is about enabling deferred refcounting, not about the GC still working.

@johng
johng requested a review from eendebakpt November 22, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants