For a thread not reading/writing global state? 0. All objects that it wants access to has already been acquired. And since no other thread wants to steal objects back, the yield no-ops. That is not to say it is free. The runtime still must confirm these things remain true. I believe the fast path can be implemented as:
A single 64bit (atomic) read from the object
A single 64bit read from thread local storage
A branch predictable branch on not equals
This does both the yielding checks and the ownership checks at the same time. Which I feel is about as lightweight of a check that can be done. I believe that PEP-703 has a fast path of a single CAS the equivalent path.
For when you weren’t already the owner, most of the time you can gain ownership with a CAS. But sometimes you need to wait for a different thread to hit a yield. The main tradeoff of this design compared to a mutex, is that there are more situations where you have to wait on other threads. So it would perform worse with contended objects.
This is executed for most bytecodes. So yes, very frequent.
Define “reading”. For example, would calling the print() function count as reading global state? What about calling another function out of the current module?
If writing global state is the only concern, then this MIGHT be doable, but if acquisition has to happen every time any non-function-local is referenced, this is sounding untenable.
How will it know that no other thread wants the objects back? Will this require a single global lock? One lock check for every object currently held? Because the former is basically back to having a GIL, the latter is exactly what I’m asking about (massive locking overhead). Unless the check “does any other thread want these objects?” is done in a lock-free way (would need to prove correctness here) then it’s going to be expensive.
Sounds like this requires one per object, then? For every object that you make any use of that isn’t local to your thread - and there are a LOT of those - it has to do this check?
Moving this to the Help category instead of Ideas, since at this point it doesn’t seem like a viable alternative proposal to the current GIL work. You can get help with these ideas in the Help category, then make a new topic in the Ideas category once you have a focused technical proposal that can compare with the current work.
Note that there are decades of detailed discussion and different implementation attempts. Sam is likely familiar with the various ideas and put a lot of consideration into the approach he settled on. To start participating in this problem space constructively at this point requires a strong familiarity with that history. A viable alternate proposal at this point should be thorough and technical at a low level, with corresponding work in an implementation.