Skip to content
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

gh-124218: Avoid refcount contention on builtins module #125847

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Oct 22, 2024

This replaces _PyEval_BuiltinsFromGlobals with
_PyDict_LoadBuiltinsFromGlobals, which returns a new reference instead of a borrowed reference. Internally, the new function uses per-thread reference counting when possible to avoid contention on the refcount fields on the builtins module.

This replaces `_PyEval_BuiltinsFromGlobals` with
`_PyDict_LoadBuiltinsFromGlobals`, which returns a new reference
instead of a borrowed reference. Internally, the new function uses
per-thread reference counting when possible to avoid contention on the
refcount fields on the builtins module.
@colesbury colesbury added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Oct 22, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 05ae6a6 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Oct 22, 2024
@colesbury colesbury marked this pull request as ready for review October 22, 2024 17:09
@colesbury colesbury requested review from mpage and Yhg1s October 22, 2024 17:09
@colesbury
Copy link
Contributor Author

This is the final PR in the series to address the reference count contention with nested functions. After this PR, the "nested function" microbenchmark scales well in 3.14.

Copy link
Contributor

@mpage mpage left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@rruuaanng rruuaanng left a comment

Choose a reason for hiding this comment

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

LGTM

PyObject *
_PyDict_LoadBuiltinsFromGlobals(PyObject *globals)
{
if (!PyDict_Check(globals)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just add assertion as it is internal only API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some of the callers can pass in a non-dict "globals". IIRC, test_eval_code_ex.py tests for this case and expects a SystemError.

@colesbury colesbury merged commit 3c4a7fa into python:main Oct 24, 2024
54 checks passed
@colesbury colesbury deleted the gh-124218-builtins branch October 24, 2024 16:44
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…GH-125847)

This replaces `_PyEval_BuiltinsFromGlobals` with
`_PyDict_LoadBuiltinsFromGlobals`, which returns a new reference
instead of a borrowed reference. Internally, the new function uses
per-thread reference counting when possible to avoid contention on the
refcount fields on the builtins module.
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.

6 participants