]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-115103: Delay reuse of mimalloc pages that store PyObjects (#115435)
authorSam Gross <colesbury@gmail.com>
Wed, 6 Mar 2024 14:42:11 +0000 (09:42 -0500)
committerGitHub <noreply@github.com>
Wed, 6 Mar 2024 14:42:11 +0000 (09:42 -0500)
commitc012c8ab7bb72a733bd98be5df32e262b9045f1a
treec3f39b0baae9a5b28816e768d788f0ec8d60ee0c
parent02ee475ee3ce9468d44758df2cd79df9f0926303
gh-115103: Delay reuse of mimalloc pages that store PyObjects (#115435)

This implements the delayed reuse of mimalloc pages that contain Python
objects in the free-threaded build.

Allocations of the same size class are grouped in data structures called
pages. These are different from operating system pages. For thread-safety, we
want to ensure that memory used to store PyObjects remains valid as long as
there may be concurrent lock-free readers; we want to delay using it for
other size classes, in other heaps, or returning it to the operating system.

When a mimalloc page becomes empty, instead of immediately freeing it, we tag
it with a QSBR goal and insert it into a per-thread state linked list of
pages to be freed. When mimalloc needs a fresh page, we process the queue and
free any still empty pages that are now deemed safe to be freed. Pages
waiting to be freed are still available for allocations of the same size
class and allocating from a page prevent it from being freed. There is
additional logic to handle abandoned pages when threads exit.
Include/internal/mimalloc/mimalloc/types.h
Include/internal/pycore_mimalloc.h
Include/internal/pycore_qsbr.h
Objects/mimalloc/heap.c
Objects/mimalloc/page.c
Objects/mimalloc/segment.c
Objects/obmalloc.c
Python/pystate.c
Python/qsbr.c