Skip to content

Commit 717ebd7

Browse files
[3.14] gh-142518: Improve mimalloc allocator docs (GH-145224) (#145823)
(cherry picked from commit 7a1da45) Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
1 parent f688a4b commit 717ebd7

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
@@ -208,8 +208,11 @@ The following function sets, modeled after the ANSI C standard, but specifying
208208
behavior when requesting zero bytes, are available for allocating and releasing
209209
memory from the Python heap.
210210
211-
The :ref:`default memory allocator <default-memory-allocators>` uses the
212-
:ref:`pymalloc memory allocator <pymalloc>`.
211+
In the GIL-enabled build (default build) the
212+
:ref:`default memory allocator <default-memory-allocators>` uses the
213+
:ref:`pymalloc memory allocator <pymalloc>`, whereas in the
214+
:term:`free-threaded build`, the default is the
215+
:ref:`mimalloc memory allocator <mimalloc>` instead.
213216
214217
.. warning::
215218
@@ -219,6 +222,11 @@ The :ref:`default memory allocator <default-memory-allocators>` uses the
219222
220223
The default allocator is now pymalloc instead of system :c:func:`malloc`.
221224
225+
.. versionchanged:: 3.13
226+
227+
In the :term:`free-threaded <free threading>` build, the default allocator
228+
is now :ref:`mimalloc <mimalloc>`.
229+
222230
.. c:function:: void* PyMem_Malloc(size_t n)
223231
224232
Allocates *n* bytes and returns a pointer of type :c:expr:`void*` to the
@@ -344,7 +352,9 @@ memory from the Python heap.
344352
the :ref:`Customize Memory Allocators <customize-memory-allocators>` section.
345353
346354
The :ref:`default object allocator <default-memory-allocators>` uses the
347-
:ref:`pymalloc memory allocator <pymalloc>`.
355+
:ref:`pymalloc memory allocator <pymalloc>`. In the
356+
:term:`free-threaded <free threading>` build, the default is the
357+
:ref:`mimalloc memory allocator <mimalloc>` instead.
348358
349359
.. warning::
350360
@@ -424,23 +434,24 @@ Default Memory Allocators
424434
425435
Default memory allocators:
426436
427-
=============================== ==================== ================== ===================== ====================
428-
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
429-
=============================== ==================== ================== ===================== ====================
430-
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
431-
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
432-
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
433-
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
434-
=============================== ==================== ================== ===================== ====================
437+
=================================== ======================= ==================== ====================== ======================
438+
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
439+
=================================== ======================= ==================== ====================== ======================
440+
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
441+
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
442+
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
443+
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
444+
Free-threaded build ``"mimalloc"`` ``mimalloc`` ``mimalloc`` ``mimalloc``
445+
Free-threaded debug build ``"mimalloc_debug"`` ``mimalloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
446+
=================================== ======================= ==================== ====================== ======================
435447
436448
Legend:
437449
438450
* Name: value for :envvar:`PYTHONMALLOC` environment variable.
439451
* ``malloc``: system allocators from the standard C library, C functions:
440452
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`.
441453
* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`.
442-
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`. The pymalloc
443-
allocator will be used if mimalloc support isn't available.
454+
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`.
444455
* "+ debug": with :ref:`debug hooks on the Python memory allocators
445456
<pymem-debug-hooks>`.
446457
* "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
@@ -1045,6 +1045,13 @@ conflict.
10451045
* ``pymalloc_debug``: same as ``pymalloc`` but also install debug hooks.
10461046
* ``mimalloc_debug``: same as ``mimalloc`` but also install debug hooks.
10471047

1048+
.. note::
1049+
1050+
In the :term:`free-threaded <free threading>` build, the ``malloc``,
1051+
``malloc_debug``, ``pymalloc``, and ``pymalloc_debug`` values are not
1052+
supported. Only ``default``, ``debug``, ``mimalloc``, and
1053+
``mimalloc_debug`` are accepted.
1054+
10481055
.. versionadded:: 3.6
10491056

10501057
.. versionchanged:: 3.7
@@ -1054,12 +1061,13 @@ conflict.
10541061
.. envvar:: PYTHONMALLOCSTATS
10551062

10561063
If set to a non-empty string, Python will print statistics of the
1057-
:ref:`pymalloc memory allocator <pymalloc>` every time a new pymalloc object
1058-
arena is created, and on shutdown.
1064+
:ref:`pymalloc memory allocator <pymalloc>` or the
1065+
:ref:`mimalloc memory allocator <mimalloc>` (whichever is in use)
1066+
every time a new object arena is created, and on shutdown.
10591067

10601068
This variable is ignored if the :envvar:`PYTHONMALLOC` environment variable
10611069
is used to force the :c:func:`malloc` allocator of the C library, or if
1062-
Python is configured without ``pymalloc`` support.
1070+
Python is configured without both ``pymalloc`` and ``mimalloc`` support.
10631071

10641072
.. versionchanged:: 3.6
10651073
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
@@ -747,6 +747,9 @@ also be used to improve performance.
747747
Disable the fast :ref:`mimalloc <mimalloc>` allocator
748748
(enabled by default).
749749

750+
This option cannot be used together with :option:`--disable-gil`
751+
because the :term:`free-threaded <free threading>` build requires mimalloc.
752+
750753
See also :envvar:`PYTHONMALLOC` environment variable.
751754

752755
.. option:: --without-pymalloc

0 commit comments

Comments
 (0)