Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimized switching between interpreters via a freelist.
21 changes: 21 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,10 +1390,26 @@ allocate_chunk(int size_in_bytes, _PyStackChunk* previous)
return res;
}

#ifdef HAVE_THREAD_LOCAL
_Py_thread_local static const struct _Py_freelist _Py_tss_tstates_freelist = { 0 };
#define _PyThreadState_FREELIST() ((struct _Py_freelist *) &_Py_tss_tstates_freelist)
#endif

static _PyThreadStateImpl *
alloc_threadstate(void)
{
#ifdef HAVE_THREAD_LOCAL
_PyThreadStateImpl *impl = _PyFreeList_PopMem(_PyThreadState_FREELIST());
if (impl == NULL)
{
return PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl));
}
// Zero-out the allocation again.
memset(impl, 0, sizeof(_PyThreadStateImpl));
return impl;
#else
return PyMem_RawCalloc(1, sizeof(_PyThreadStateImpl));
#endif
}

static void
Expand All @@ -1408,7 +1424,12 @@ free_threadstate(_PyThreadStateImpl *tstate)
sizeof(*tstate));
}
else {
#ifdef HAVE_THREAD_LOCAL
// Maximum of 100 thread states in the freelist
_PyFreeList_Free(_PyThreadState_FREELIST(), tstate, 100, PyMem_RawFree);
#else
PyMem_RawFree(tstate);
#endif
}
}

Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Python/pyfpe.c - PyFPE_counter -

Python/import.c - pkgcontext -
Python/pystate.c - _Py_tss_tstate -
Python/pystate.c - _Py_tss_tstates_freelist -

##-----------------------
## should be const
Expand Down
Loading