Skip to content

Commit 7a1da45

Browse files
authored
gh-142518: Improve mimalloc allocator docs (#145224)
1 parent 805ca4f commit 7a1da45

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

Doc/c-api/memory.rst

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ The following function sets, modeled after the ANSI C standard, but specifying
204204
behavior when requesting zero bytes, are available for allocating and releasing
205205
memory from the Python heap.
206206
207-
The :ref:`default memory allocator <default-memory-allocators>` uses the
208-
:ref:`pymalloc memory allocator <pymalloc>`.
207+
In the GIL-enabled build (default build) the
208+
:ref:`default memory allocator <default-memory-allocators>` uses the
209+
:ref:`pymalloc memory allocator <pymalloc>`, whereas in the
210+
:term:`free-threaded build`, the default is the
211+
:ref:`mimalloc memory allocator <mimalloc>` instead.
209212
210213
.. warning::
211214
@@ -215,6 +218,11 @@ The :ref:`default memory allocator <default-memory-allocators>` uses the
215218
216219
The default allocator is now pymalloc instead of system :c:func:`malloc`.
217220
221+
.. versionchanged:: 3.13
222+
223+
In the :term:`free-threaded <free threading>` build, the default allocator
224+
is now :ref:`mimalloc <mimalloc>`.
225+
218226
.. c:function:: void* PyMem_Malloc(size_t n)
219227
220228
Allocates *n* bytes and returns a pointer of type :c:expr:`void*` to the
@@ -340,7 +348,9 @@ memory from the Python heap.
340348
the :ref:`Customize Memory Allocators <customize-memory-allocators>` section.
341349
342350
The :ref:`default object allocator <default-memory-allocators>` uses the
343-
:ref:`pymalloc memory allocator <pymalloc>`.
351+
:ref:`pymalloc memory allocator <pymalloc>`. In the
352+
:term:`free-threaded <free threading>` build, the default is the
353+
:ref:`mimalloc memory allocator <mimalloc>` instead.
344354
345355
.. warning::
346356
@@ -420,23 +430,24 @@ Default Memory Allocators
420430
421431
Default memory allocators:
422432
423-
=============================== ==================== ================== ===================== ====================
424-
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
425-
=============================== ==================== ================== ===================== ====================
426-
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
427-
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
428-
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
429-
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
430-
=============================== ==================== ================== ===================== ====================
433+
=================================== ======================= ==================== ====================== ======================
434+
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
435+
=================================== ======================= ==================== ====================== ======================
436+
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
437+
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
438+
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
439+
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
440+
Free-threaded build ``"mimalloc"`` ``mimalloc`` ``mimalloc`` ``mimalloc``
441+
Free-threaded debug build ``"mimalloc_debug"`` ``mimalloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
442+
=================================== ======================= ==================== ====================== ======================
431443
432444
Legend:
433445
434446
* Name: value for :envvar:`PYTHONMALLOC` environment variable.
435447
* ``malloc``: system allocators from the standard C library, C functions:
436448
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`.
437449
* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`.
438-
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`. The pymalloc
439-
allocator will be used if mimalloc support isn't available.
450+
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`.
440451
* "+ debug": with :ref:`debug hooks on the Python memory allocators
441452
<pymem-debug-hooks>`.
442453
* "Debug build": :ref:`Python build in debug mode <debug-build>`.
@@ -733,9 +744,27 @@ The mimalloc allocator
733744
734745
.. versionadded:: 3.13
735746
736-
Python supports the mimalloc allocator when the underlying platform support is available.
737-
mimalloc "is a general purpose allocator with excellent performance characteristics.
738-
Initially developed by Daan Leijen for the runtime systems of the Koka and Lean languages."
747+
Python supports the `mimalloc <https://github.com/microsoft/mimalloc/>`__
748+
allocator when the underlying platform support is available.
749+
mimalloc is a general purpose allocator with excellent performance
750+
characteristics, initially developed by Daan Leijen for the runtime systems
751+
of the Koka and Lean languages.
752+
753+
Unlike :ref:`pymalloc <pymalloc>`, which is optimized for small objects (512
754+
bytes or fewer), mimalloc handles allocations of any size.
755+
756+
In the :term:`free-threaded <free threading>` build, mimalloc is the default
757+
and **required** allocator for the :c:macro:`PYMEM_DOMAIN_MEM` and
758+
:c:macro:`PYMEM_DOMAIN_OBJ` domains. It cannot be disabled in free-threaded
759+
builds. The free-threaded build uses per-thread mimalloc heaps, which allows
760+
allocation and deallocation to proceed without locking in most cases.
761+
762+
In the default (non-free-threaded) build, mimalloc is available but not the
763+
default allocator. It can be selected at runtime using
764+
:envvar:`PYTHONMALLOC`\ ``=mimalloc`` (or ``mimalloc_debug`` to include
765+
:ref:`debug hooks <pymem-debug-hooks>`). It can be disabled at build time
766+
using the :option:`--without-mimalloc` configure option, but this option
767+
cannot be combined with :option:`--disable-gil`.
739768
740769
tracemalloc C API
741770
=================

Doc/using/cmdline.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,13 @@ conflict.
10851085
* ``pymalloc_debug``: same as ``pymalloc`` but also install debug hooks.
10861086
* ``mimalloc_debug``: same as ``mimalloc`` but also install debug hooks.
10871087

1088+
.. note::
1089+
1090+
In the :term:`free-threaded <free threading>` build, the ``malloc``,
1091+
``malloc_debug``, ``pymalloc``, and ``pymalloc_debug`` values are not
1092+
supported. Only ``default``, ``debug``, ``mimalloc``, and
1093+
``mimalloc_debug`` are accepted.
1094+
10881095
.. versionadded:: 3.6
10891096

10901097
.. versionchanged:: 3.7
@@ -1094,12 +1101,13 @@ conflict.
10941101
.. envvar:: PYTHONMALLOCSTATS
10951102

10961103
If set to a non-empty string, Python will print statistics of the
1097-
:ref:`pymalloc memory allocator <pymalloc>` every time a new pymalloc object
1098-
arena is created, and on shutdown.
1104+
:ref:`pymalloc memory allocator <pymalloc>` or the
1105+
:ref:`mimalloc memory allocator <mimalloc>` (whichever is in use)
1106+
every time a new object arena is created, and on shutdown.
10991107

11001108
This variable is ignored if the :envvar:`PYTHONMALLOC` environment variable
11011109
is used to force the :c:func:`malloc` allocator of the C library, or if
1102-
Python is configured without ``pymalloc`` support.
1110+
Python is configured without both ``pymalloc`` and ``mimalloc`` support.
11031111

11041112
.. versionchanged:: 3.6
11051113
This variable can now also be used on Python compiled in release mode.

Doc/using/configure.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ also be used to improve performance.
774774
Disable the fast :ref:`mimalloc <mimalloc>` allocator
775775
(enabled by default).
776776

777+
This option cannot be used together with :option:`--disable-gil`
778+
because the :term:`free-threaded <free threading>` build requires mimalloc.
779+
777780
See also :envvar:`PYTHONMALLOC` environment variable.
778781

779782
.. option:: --without-pymalloc

0 commit comments

Comments
 (0)