-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-112087: Make __sizeof__ and listiter_{len, next} to be threadsafe #114843
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
Conversation
|
colesbury
left a comment
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.
Regarding iterators: list (and dict) iterators current clear it->it_seq field when the iterator is exhausted. This will make it harder to make the iterators thread-safe because it_seq is mutable. In the free-threaded build, I think we should avoid clearing the it_seq field until the iterator is deallocated.
Objects/listobject.c
Outdated
| #ifdef Py_GIL_DISABLED | ||
| #define LOAD_SSIZE_ATOMIC_AS_POSSIBLE(value) _Py_atomic_load_ssize_relaxed(&value) | ||
| #define STORE_SSIZE_ATOMIC_AS_POSSIBLE(value, new_value) _Py_atomic_store_ssize_relaxed(&value, new_value) |
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.
The name is a bit confusing to me -- I'm not sure what AS_POSSIBLE means here. Maybe just LOAD_SSIZE(). It's not very descriptive, but at least it's short.
The style I've seen more commonly in CPython looks like:
#ifdef Py_GIL_DISABLED
# define LOAD_SSIZE_ATOMIC_AS_POSSIBLE(value) _Py_atomic_load_ssize_relaxed(&value)
#endif
@colesbury |
| self.assertEqual( | ||
| run("reversed", orig["reversed"](list(range(8)))), | ||
| (iter, ([],)) | ||
| (reversed, ([],)) |
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.
This test was added from #101769, not sure that it will be okay to change the implementation detail.
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 it is OK, see my other comment.
|
@colesbury Since it touched on the implementation details, I think that we should listen to other core developers' opinions, too.
@gvanrossum Tier 1Tier 2 |
| if (list == NULL) | ||
| return NULL; | ||
| return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list); | ||
| return Py_BuildValue("N(N)", iter, list); |
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.
This is the change causing the behavior difference in the test. Previously, an empty iterator would always reduce to the iter builtin, now it will reduce to reversed if it was a reverse iterator. IMO the new behavior is more correct (consistent with the non-empty behavior) and we can consider this a bugfix.
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.
Okay in that case, we need to create backport patches for this.
|
@gvanrossum @colesbury gentle ping :) |
colesbury
left a comment
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 _GUARD_NOT_EXHAUSTED_LIST also needs to be adjusted for the free-threaded build because seq == NULL no longer signifies an exhausted iterator for the free-threaded build. Like with _ITER_JUMP_LIST, I think the deopt can be reduced to:
DEOPT_IF((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq));
Objects/listobject.c
Outdated
| #ifdef Py_GIL_DISABLED | ||
| #define LOAD_SSIZE(value) _Py_atomic_load_ssize_relaxed(&value) |
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.
The indentation I've seen more commonly in CPython looks like:
#ifdef Py_GIL_DISABLED
# define LOAD_SSIZE(value) _Py_atomic_load_ssize_relaxed(&value)
...
#endif
I trust Sam. My queue is very long and my proessing capacity is reduced. |
e91da7e to
bec617a
Compare
colesbury
left a comment
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.
LGTM
listobjects thread-safe in--disable-gilbuilds #112087