-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-120608: Make reversed iterator work with free-threading #120971
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
Like we discussed in #120496, I do not think that we should make reversed thread safe. |
How impact for the single thread performance? I am +0 with this change. |
Maybe I misunderstood or the description of the PR is not clear enough. Without this PR the interpreter can lock or freeze in the free-threaded build. As a side effect of this PR the |
Performance impact (bit noisy on my system):
Summary: Update: we could perhaps remove the atomics from this PR and keep the part where we do not clear |
The change requires some performance overhead, I think that we don't need to make |
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.
Without this PR the interpreter can lock or freeze in the free-threaded build
Ok, thanks I missed that.
The relaxed atomics loads and stores are free, but the _Py_atomic_add_ssize is relatively expensive. We want to avoid that, even at the cost of potentially returning the same item multiple times in a race condition.
- Use relaxed atomics to load/store
ro->index
. (Or you can use theFT_
macros) - Update the NULL checks for
ro->seq
to instead check ifro->index
is negative (inreversed_len
andreversed_reduce
)
Co-authored-by: Sam Gross <[email protected]>
Should we also update the
I believe the behaviour on the free-threaded build makes more sense and that the behaviour on current main is a workaround for the case where |
Yes, I think both
I would prefer to avoid changing the behavior, even if it doesn't make as much sense. |
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.
Looks good. Two minor suggestions.
Lib/test/test_reversed.py
Outdated
_ = [t.start() for t in worker_threads] | ||
_ = [t.join() for t in worker_threads] |
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 the preference I've heard from other core developers is to use for
loops instead of using list comprehensions for their side-effects.
Misc/NEWS.d/next/Core and Builtins/2024-06-24-20-08-55.gh-issue-120608.d75n8U.rst
Outdated
Show resolved
Hide resolved
…e-120608.d75n8U.rst Co-authored-by: Sam Gross <[email protected]>
This reverts commit c4c1ea9.
The merge with |
…hon#120971) Co-authored-by: Sam Gross <[email protected]> Co-authored-by: Kumar Aditya <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
In this PR we make the
reversed
iterator thread-safe. In the free-threading build the indexro->index
needs to be loaded and updated atomically. Also we need to prevent the pointerro->seq
from being released in one the thread while one of the other threads is still using it.Update: based on review comments this PR makes the
reversed
work under free-threading, but it is not guaranteed to be thread-safereversed
iterator, so a new one was added,