Repository navigation
Conversation
|
The C API function to set vectorcalls (https://docs.python.org/3/c-api/function.html#c.PyFunction_SetVectorcall) does a stop the world which seems like makes this unnecessary. Can see avoiding the stop the world being good, but would be better to me to improve the general path than make every vectorcall a little slower. |
Unfortunately using that consistently is going to be too slow. The worst case scenario here is lambdas/closures which typically get created a lot. Stopping the world to set it on all of those would be pretty bad. But I'm not sure there is much of a need to stop the world to set vector call? The entry point should have identical behavior as it not being replaced and so the place where it matters is around specialization / the interpreter avoiding interpreting it. And if you lose the race there it's not really different than losing the race to stopping the world. So maybe a relaxed load should be used everywhere? |
|
So lets make it faster; direct setting the pointer and then fixing the race because it was direct set raises other issues. If just setting without a stop the world is safe, lets make the C Function do that safe thing. Doing atomics always / everywhere does have an impact on performance; there are reasons choose other mechanisms in a lot of these places. |
Okay, I'll update the PR to include the C function too and see if there other places that need it. In this case because the atomics are relaxed from a hardware perspective on most platforms they don't cost more although they could inhibit some compiler optimizations. |
Vectorcall can be updated on another thread in the free-threaded build if a JIT like CinderX or Numba were to compile the function. This should be ready using a relaxed atomic.
It is documented that this update should preserve the behavior of the existing vectorcall function. Therefore if we race against it and execute it in the interpreter instead of running the new version immediately the race should be tolerable - we'll pick it up eventually. This updates the specialization reads of version / vectorcall entry point to use an acquire read before the read of the vectorcall and the clearing of the version to do a release store - this makes sure we don't inadvertantly specializing with a bad version / vectorcall combo.
It also drops the cleared version as it's not being used for anything.