static inline struct _Py_freelists *
_Py_freelists_GET(void)
{
- PyThreadState *tstate = _PyThreadState_GET();
#ifdef Py_DEBUG
- _Py_EnsureTstateNotNULL(tstate);
+ _Py_AssertHoldsTstate();
#endif
#ifdef Py_GIL_DISABLED
+ PyThreadState *tstate = _PyThreadState_GET();
return &((_PyThreadStateImpl*)tstate)->freelists;
#else
- return &tstate->interp->object_state.freelists;
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return &interp->object_state.freelists;
#endif
}
#if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
extern _Py_thread_local PyThreadState *_Py_tss_tstate;
+extern _Py_thread_local PyInterpreterState *_Py_tss_interp;
#endif
#ifndef NDEBUG
See also PyInterpreterState_Get()
and _PyGILState_GetInterpreterStateUnsafe(). */
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
- PyThreadState *tstate = _PyThreadState_GET();
#ifdef Py_DEBUG
+ PyThreadState *tstate = _PyThreadState_GET();
_Py_EnsureTstateNotNULL(tstate);
#endif
- return tstate->interp;
+#if !defined(Py_BUILD_CORE_MODULE)
+ return _Py_tss_interp;
+#else
+ return _PyThreadState_GET()->interp;
+#endif
}
--- /dev/null
+Speed up accessing interpreter state by caching it in a thread local variable. Patch by Kumar Aditya.
also known as a "gilstate." */
_Py_thread_local PyThreadState *_Py_tss_gilstate = NULL;
+/* The interpreter of the attached thread state,
+ and is same as tstate->interp. */
+_Py_thread_local PyInterpreterState *_Py_tss_interp = NULL;
+
static inline PyThreadState *
current_fast_get(void)
{
{
assert(tstate != NULL);
_Py_tss_tstate = tstate;
+ assert(tstate->interp != NULL);
+ _Py_tss_interp = tstate->interp;
}
static inline void
current_fast_clear(_PyRuntimeState *Py_UNUSED(runtime))
{
_Py_tss_tstate = NULL;
+ _Py_tss_interp = NULL;
}
#define tstate_verify_not_active(tstate) \
PyInterpreterState*
PyInterpreterState_Get(void)
{
- PyThreadState *tstate = current_fast_get();
- _Py_EnsureTstateNotNULL(tstate);
- PyInterpreterState *interp = tstate->interp;
+ _Py_AssertHoldsTstate();
+ PyInterpreterState *interp = _Py_tss_interp;
if (interp == NULL) {
Py_FatalError("no current interpreter");
}
Python/import.c - pkgcontext -
Python/pystate.c - _Py_tss_tstate -
Python/pystate.c - _Py_tss_gilstate -
+Python/pystate.c - _Py_tss_interp -
##-----------------------
## should be const