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

Implement biased reference counting in --disable-gil builds #110481

Open
Tracked by #108219
colesbury opened this issue Oct 6, 2023 · 0 comments
Open
Tracked by #108219

Implement biased reference counting in --disable-gil builds #110481

colesbury opened this issue Oct 6, 2023 · 0 comments
Assignees
Labels
3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@colesbury
Copy link
Contributor

colesbury commented Oct 6, 2023

Feature or enhancement

CPython's current reference counting implementation would not be thread-safe without the GIL. In --disable-gil builds, we should implement biased reference counting, which is thread-safe and has lower execution overhead compared to plain atomic reference counting. The design is described in the "Biased Reference Counting" section of PEP 703.

This will require changing the PyObject struct in --disable-gil builds. I expect the --disable-gil struct to look like:

struct _object {
  uintptr_t ob_tid;
  uint16_t _padding;
  uint8_t ob_mutex;
  uint8_t ob_gc_bits;
  uint32_t ob_ref_local;
  Py_ssize_t ob_ref_shared;
  PyTypeObject *ob_type;
};

Not all the fields will be used immediately, but it will be easier to include them as part of implementing this change.

I intend to split the implementation across (at least) two PRs to make it easier to review.

  1. The first PR will add the new PyObject fields and implement the core biased reference counting operations, but not the inter-thread queue to reduce the initial complexity.
  2. A later PR will implement the inter-thread queue.

The inter-thread queue is needed eventually (before actually disabling the GIL), but since the --disable-gil builds still currently require the GIL, it doesn't have to be in the initial PR.

Linked PRs

@colesbury colesbury added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.13 new features, bugs and security fixes labels Oct 6, 2023
@colesbury colesbury self-assigned this Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant