-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-112087: Make list_repr and list_length to be thread-safe #114582
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@encukou said that making
Py_SIZEto be thread-safe for free-threaded build would be beneficial rather than implementinglist_lengthwith macro if possible :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could. I don't know enough about the grand plan to know for sure :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pushing the atomic load down to
Py_SIZE()is reasonable and will probably simplify call sites.That was something I tried and then abandoned in nogil-3.9 because at the time the macro was also used as an l-value like
Py_SIZE(ob) = 1. Fortunately, that's no longer a concern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could just do the atomic load in
PyList_GET_SIZE()and consistently use that within this file. I think I'd prefer that.There are some loops that use
Py_SIZE()on immutable objects that might slow down if we makePy_SIZEuse an atomic load. For example, incodeobject.c:cpython/Objects/codeobject.c
Lines 433 to 436 in 30b7b4f
The compiler will currently lift the
Py_SIZE()load outside the loop. But it won't do that optimization with an atomic load.It's hard to know if these sorts of things will make a difference overall, but it's often easier to avoid the potential performance issues in the first place than trying to find, benchmark, and fix the issues later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my opinion keeps changing as I read through
listobject.c. Changing all thePy_SIZE()calls toPyList_GET_SIZE()seems like it would be a lot of noise. To be honest, I think there are a lot of reasonable approaches, and whatever you decide to do is fine with me. Here is my current thinking:Py_SIZE()calls are reads from functions that will be within critical sections in the future. Those are fine as is.ob_sizeoutside of a critical section, it should use an atomic load. Either directly or indirectly, such as by callinglist_length().For
list_repr, my inclination would be to push the zero-size check down into the critical section, but using an atomic load also seems fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to @vstinner for fixing this! 👏
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. well, from the view of maintaining implementation detail as not changed as possible for the list object itself, updating PyList_GET_SIZE() to be an atomic operation is worth doing. It's good negotiation point I guess.
If we need to make Py_SIZE() as atomic operation, we can handle it later.