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: Use per-thread refcounts for code objects #125216

Merged
merged 3 commits into from
Oct 15, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Oct 9, 2024

Use per-thread refcounting for the reference from function objects to their corresponding code object. This can be a source of contention when frequently creating nested functions. Deferred refcounting alone isn't a great fit here because these references are on the heap and may be modified by other libraries.

We will still need to address the globals and builtins dictionaries in a similar way.

Use per-thread refcounting for the reference from function objects to
their corresponding code object. This can be a source of contention when
frequently creating nested functions. Deferred refcounting alone isn't a
great fit here because these references are on the heap and may be
modified by other libraries.
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 018b2a1 🤖

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 9, 2024
@colesbury colesbury marked this pull request as ready for review October 10, 2024 13:32
@colesbury colesbury requested a review from DinoV October 10, 2024 13:32
@markshannon
Copy link
Member

This seems like we are adding more code, rather than reusing the existing code for per-thread refcounts already in existence for classes.
Can we factor out this code?

@colesbury
Copy link
Contributor Author

It is factored out and reusing existing code. That's why _Py_INCREF_CODE and _Py_DECREF_CODE are only one line each.

What concretely would you like changed?

@markshannon
Copy link
Member

Quite a lot of code is being added to pycore_object.h, but I don't see code being removed from typeobject.c or wherever the code for per-thread reference counts for classes is.
I would have though that classes could use the new code as well.

@colesbury
Copy link
Contributor Author

What would you like to see changed? pycore_object.h is +22 lines. That doesn't seem like quite a lot to me. That is:

  • Add two simple functions with one line bodies (_Py_INCREF_CODE and _Py_DECREF_CODE): +12
  • Split off the generic _Py_THREAD_INCREF_OBJECT from _Py_INCREF_TYPE: +5
  • Split off the generic _Py_THREAD_DECREF_OBJECT from _Py_DECREF_TYPE: +5

@colesbury colesbury requested a review from mpage October 15, 2024 15:25
Python/uniqueid.c Outdated Show resolved Hide resolved
static inline void
_Py_DECREF_CODE(PyCodeObject *co)
{
_Py_THREAD_DECREF_OBJECT((PyObject *)co, co->_co_unique_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

assert that co is a code object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

co is statically a PyCodeObject *. We don't typically dynamically check the type when we have the static type (e.g., in PyCode_Addr2Line)

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant it to be protect against incorrect casts, anyways it's minor.

static inline void
_Py_INCREF_CODE(PyCodeObject *co)
{
_Py_THREAD_INCREF_OBJECT((PyObject *)co, co->_co_unique_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same add assert to check it is codeobject

Co-authored-by: Kumar Aditya <[email protected]>
@colesbury
Copy link
Contributor Author

!buildbot AMD64 Fedora Stable Refleaks

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 7ad129b 🤖

The command will test the builders whose names match following regular expression: AMD64 Fedora Stable Refleaks

The builders matched are:

  • AMD64 Fedora Stable Refleaks PR

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 - just some comment fixups inline.

Include/internal/pycore_object.h Outdated Show resolved Hide resolved
Include/internal/pycore_object.h Outdated Show resolved Hide resolved
Python/uniqueid.c Outdated Show resolved Hide resolved
@colesbury colesbury merged commit 3ea488a into python:main Oct 15, 2024
35 checks passed
@colesbury colesbury deleted the gh-124218-thread-refcount branch October 15, 2024 19:06
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.

5 participants