Skip to content

Concurrent reads from cell objects (LOAD_DEREF) do not scale well #123358

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

Closed
colesbury opened this issue Aug 26, 2024 · 2 comments
Closed

Concurrent reads from cell objects (LOAD_DEREF) do not scale well #123358

colesbury opened this issue Aug 26, 2024 · 2 comments
Assignees
Labels

Comments

@colesbury
Copy link
Contributor

colesbury commented Aug 26, 2024

Concurrent accesses from multiple threads to the same cell object do not scale well in the free-threaded build. There are two problems:

  1. LOAD_DEREF calls PyCell_GetRef, which acquires the per-object lock on the cell instance. We'll need to use _Py_TryXGetRef or similar to safely access the value.

  2. PyCell_GetRef returns a strong reference and LOAD_DEREF will need a _PyStackRef to handle objects that use deferred reference counting.

Linked PRs

@colesbury colesbury added performance Performance or resource usage topic-free-threading labels Aug 26, 2024
@colesbury
Copy link
Contributor Author

This came up in the context of wjakob/nanobind#695

@corona10
Copy link
Member

@colesbury Can I take a look at it?

@corona10 corona10 self-assigned this Sep 24, 2024
colesbury added a commit to colesbury/cpython that referenced this issue Mar 24, 2025
colesbury added a commit that referenced this issue Mar 26, 2025
Concurrent accesses from multiple threads to the same `cell` object did not
scale well in the free-threaded build. Use `_PyStackRef` and optimistically
avoid locking to improve scaling.

With the locks around cell reads gone, some of the free threading tests were
prone to starvation: the readers were able to run in a tight loop and the
writer threads weren't scheduled frequently enough to make timely progress.
Adjust the tests to avoid this.

Co-authored-by: Donghee Na <[email protected]>
@github-project-automation github-project-automation bot moved this from Todo to Done in Sprint 2024 Mar 26, 2025
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 1, 2025
Concurrent accesses from multiple threads to the same `cell` object did not
scale well in the free-threaded build. Use `_PyStackRef` and optimistically
avoid locking to improve scaling.

With the locks around cell reads gone, some of the free threading tests were
prone to starvation: the readers were able to run in a tight loop and the
writer threads weren't scheduled frequently enough to make timely progress.
Adjust the tests to avoid this.

Co-authored-by: Donghee Na <[email protected]>
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Concurrent accesses from multiple threads to the same `cell` object did not
scale well in the free-threaded build. Use `_PyStackRef` and optimistically
avoid locking to improve scaling.

With the locks around cell reads gone, some of the free threading tests were
prone to starvation: the readers were able to run in a tight loop and the
writer threads weren't scheduled frequently enough to make timely progress.
Adjust the tests to avoid this.

Co-authored-by: Donghee Na <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

5 participants
@colesbury @corona10 and others