8000 gh-134761: Use deferred reference counting for `threading` concurrency primitives by ZeroIntensity · Pull Request #134762 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134761: Use deferred reference counting for threading concurrency primitives #134762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Document and test sys._defer_refcount()
  • Loading branch information
ZeroIntensity committed May 26, 2025
commit 6093acb1bd747e4e6bd6e067828bc1a2d44c05ac
19 changes: 19 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ always available. Unless explicitly noted otherwise, all variables are read-only
This function is specific to CPython. The exact output format is not
defined here, and may change.

.. function:: _defer_refcount(op)
Copy link
Member

Choose a reason for hiding this comment

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

Did not take a look overall but not sure it is worth to to expose it through the documentation since this is not a public api.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm a little bit on the fence about it. We do this already for some other functions in sys: _is_gil_enabled, _is_interned, _is_immortal, and a few others. Basically, it's a way to expose implementation details that are useful at a Python level.

Overall, I think this is worth documenting. There's no other way to improve scaling from Python right now, and it's similar to using an unstable C API (in the sense that it could be removed in any version).

Copy link
Contributor

Choose a reason for hiding this comment

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

What about documenting this in https://github.com/python/cpython/tree/main/InternalDocs somewhere? I fully agree with removing this from the normal documentation (for the reasons mentioned), but as a user/developer/tester I find the python api to _defer_refcount (and others like _is_immortal) useful and some documentation is always nice.


Enable deferred reference counting on *op*, mitigating reference count
contention on :term:`free threaded <free threading>` builds of Python.

Return :const:`True` if deferred reference counting was enabled on *op*,
and :const:`False` otherwise.

.. versionadded:: next

.. impl-detail::

This function should be used for specialized purposes only.
It is not guaranteed to exist in all implementations of Python.

.. seealso::

:c:func:`PyUnstable_Object_EnableDeferredRefcount`


.. data:: dllhandle

Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,25 @@ def test_pystats(self):
def test_disable_gil_abi(self):
self.assertEqual('t' in sys.abiflags, support.Py_GIL_DISABLED)

@test.support.cpython_only
@unittest.skipUnless(hasattr(sys, '_defer_refcount'), "requires _defer_refcount()")
def test_defer_refcount(self):
_testinternalcapi = import_helper.import_module('_testinternalcapi')

class Test:
pass

ref = Test()
if support.Py_GIL_DISABLED:
self.assertTrue(sys._defer_refcount(ref))
self.assertTrue(_testinternalcapi.has_deferred_refcount(ref))
self.assertFalse(sys._defer_refcount(ref))
self.assertFalse(sys._defer_refcount(42))
else:
self.assertFalse(sys._defer_refcount(ref))
self.assertFalse(_testinternalcapi.has_deferred_refcount(ref))
self.assertFalse(sys._defer_refcount(42))


@test.support.cpython_only
class UnraisableHookTest(unittest.TestCase):
Expand Down
Loading
0