-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
gh-123471: Make concurrent iteration over itertools.cycle safe under free-threading #131212
base: main
Are you sure you want to change the base?
Conversation
In the "Strategy for Free Threading" umbrella issue, principle 3 is:
Given that plan, can you please articulate reason for this PR. Is there currently a risk of a crash? Also what is specifically is meant by "safe under free-threading"? AFAICT there is no concurrent access use case for cycle() that wouldn't be met by the planned options to either wrap with The itertools code was highly refined and micro-optimized with painful tooling (valgrind, vtune, etc), so I'm a bit reluctant to change long stable code unless it is really necessary. Also, I was waiting to see how the essential builtin functions/classes evolved so that itertools could follow a proven model rather than being on the bleeding edge where misadventures might persist for a long time before being noticed. |
The current implementation of
cpython/Modules/itertoolsmodule.c Lines 1222 to 1225 in faa80fc
cpython/Modules/itertoolsmodule.c Lines 1218 to 1219 in faa80fc
while another thread is still using By "safe under free-threading" I mean the free-threading build will not crash. No guarantees are given about any "correctness" of the results (I can adapt the title and news entry if desired). It is true that using the planned |
Okay, this sounds reasonable :-) Thanks for explaining the purpose of the edit. Please do check that performance hasn't been negatively impacted (if so, the make an |
@rhettinger In the benchmarks I did this PR is faster for the normal build. There are two reasons for this
The gain from this PR is small though (and I would not be surprised if for different platforms or benchmark tests the gain is zero). Here are the results of one of the benchmarks I did: Benchmark code
Script to generate and plot the data
|
See #124397
cycle->it
. Instead we usecycle->index
as a check whether the iterator has been exhausted or not.cycle->firstpass
itertools.batched
in a single file.