GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-103323: Get the "Current" Thread State from a Thread-Local Variable by ericsnowcurrently · Pull Request #103324 · python/cpython Conversation
|
|
Per the benchmarks, this change is a little faster (less than 1%) on Linux/GCC. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
🤖 New build scheduled with the buildbot fleet by @ericsnowcurrently for commit feb8ef5 🤖 If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It might be worth trying to see what is the performance impact of storing the interpreter state in TLS as well. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
* main: (53 commits) pythongh-102498 Clean up unused variables and imports in the email module (python#102482) pythongh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (python#99244) pythongh-99032: datetime docs: Encoding is no longer relevant (python#93365) pythongh-94300: Update datetime.strptime documentation (python#95318) pythongh-103776: Remove explicit uses of $(SHELL) from Makefile (pythonGH-103778) pythongh-87092: fix a few cases of incorrect error handling in compiler (python#103456) pythonGH-103727: Avoid advancing tokenizer too far in f-string mode (pythonGH-103775) Revert "Add tests for empty range equality (python#103751)" (python#103770) pythongh-94518: Port 23-argument `_posixsubprocess.fork_exec` to Argument Clinic (python#94519) pythonGH-65022: Fix description of copyreg.pickle function (python#102656) pythongh-103323: Get the "Current" Thread State from a Thread-Local Variable (pythongh-103324) pythongh-91687: modernize dataclass example typing (python#103773) pythongh-103746: Test `types.UnionType` and `Literal` types together (python#103747) pythongh-103765: Fix 'Warning: py:class reference target not found: ModuleSpec' (pythonGH-103769) pythongh-87452: Improve the Popen.returncode docs Removed unnecessary escaping of asterisks (python#103714) pythonGH-102973: Slim down Fedora packages in the dev container (python#103283) pythongh-103091: Add PyUnstable_Type_AssignVersionTag (python#103095) Add tests for empty range equality (python#103751) pythongh-103712: Increase the length of the type name in AttributeError messages (python#103713) ...
* superopt: (82 commits) pythongh-101517: fix line number propagation in code generated for except* (python#103550) pythongh-103780: Use patch instead of mock in asyncio unix events test (python#103782) pythongh-102498 Clean up unused variables and imports in the email module (python#102482) pythongh-99184: Bypass instance attribute access in `repr` of `weakref.ref` (python#99244) pythongh-99032: datetime docs: Encoding is no longer relevant (python#93365) pythongh-94300: Update datetime.strptime documentation (python#95318) pythongh-103776: Remove explicit uses of $(SHELL) from Makefile (pythonGH-103778) pythongh-87092: fix a few cases of incorrect error handling in compiler (python#103456) pythonGH-103727: Avoid advancing tokenizer too far in f-string mode (pythonGH-103775) Revert "Add tests for empty range equality (python#103751)" (python#103770) pythongh-94518: Port 23-argument `_posixsubprocess.fork_exec` to Argument Clinic (python#94519) pythonGH-65022: Fix description of copyreg.pickle function (python#102656) pythongh-103323: Get the "Current" Thread State from a Thread-Local Variable (pythongh-103324) pythongh-91687: modernize dataclass example typing (python#103773) pythongh-103746: Test `types.UnionType` and `Literal` types together (python#103747) pythongh-103765: Fix 'Warning: py:class reference target not found: ModuleSpec' (pythonGH-103769) pythongh-87452: Improve the Popen.returncode docs Removed unnecessary escaping of asterisks (python#103714) pythonGH-102973: Slim down Fedora packages in the dev container (python#103283) pythongh-103091: Add PyUnstable_Type_AssignVersionTag (python#103095) ...
|
FTR, on Windows this introduced a ~2% performance regression, and on MacOS there's ~3% regression. Note that these penalties may be partially mitigated by passing the current thread state as an argument throughout the internal C-API (where currently we only do so in some places). The implementation here is also relatively naïve. There are likely opportunities to improve performance via compiler-specific directives. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| static inline PyThreadState* | ||
| _PyRuntimeState_GetThreadState(_PyRuntimeState *Py_UNUSED(runtime)) | ||
| { | ||
| return _PyThreadState_GET(); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This function no longer makes sense: I wrote PR #104171 to remove it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I prepared this change for years in advance:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
In the Python 3.9 and 3.10 era, I moved multiple global states to the "interpreter state" (interp): https://pythondev.readthedocs.io/subinterpreters.html#done These changes caused various crashes in third party C extensions which use the C API with the GIL released (!). For example, calling |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
It might be interesting to check the hot code calling _PyThreadState_GET() and see if tstate could be passed to only call _PyThreadState_GET() once. I'm not sure if it's worth it. Also, in stdlib C extensions, I would prefer to use the internal C API less rather than more :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| # define WITH_THREAD | ||
| #endif | ||
|
|
||
| #ifdef WITH_THREAD |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This test is useless. This macro is now always defined. It's only kept for backward compatibility.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Doesn't it affect WASM builds?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
See the code 3 lines above:
#ifndef WITH_THREAD # define WITH_THREAD #endif
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| #if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) | ||
| extern _Py_thread_local PyThreadState *_Py_tss_tstate; | ||
| #endif | ||
| PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What is the use case for this new _PyThreadState_GetCurrent() function? There is already PyThreadState_Get(). How is it different?
The API to get the current thread state is already complicated and has a complicated history: https://pythondev.readthedocs.io/pystate.html
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's there because I couldn't find a way to mix PyAPI_DATA with _Py_thread_local. Looks like that's the same issue you ran into in 2020.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Does this change fix indirectly the PyGILState API for subinterpreters? See: #59956 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks @ericsnowcurrently for taking care of this very old project! It seems like the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Python has required thread local support since 3.12 (see GH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
…onGH-140690) Python has required thread local support since 3.12 (see pythonGH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.