gh-120608: Make reversed iterator work with free-threading#120971
gh-120608: Make reversed iterator work with free-threading#120971ambv merged 33 commits intopython:mainfrom
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 |
colesbury
left a comment
There was a problem hiding this comment.
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->seqto instead check ifro->indexis negative (inreversed_lenandreversed_reduce)
Co-authored-by: Sam Gross <colesbury@gmail.com>
|
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. |
Co-authored-by: Sam Gross <colesbury@gmail.com>
colesbury
left a comment
There was a problem hiding this comment.
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.
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 <colesbury@gmail.com>
This reverts commit c4c1ea9.
|
The merge with |
In this PR we make the
reversediterator thread-safe. In the free-threading build the indexro->indexneeds to be loaded and updated atomically. Also we need to prevent the pointerro->seqfrom 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
reversedwork under free-threading, but it is not guaranteed to be thread-safereversediterator, so a new one was added,