Skip to content

Commit

Permalink
pythongh-90815: Add mimalloc memory allocator (python#109914)
Browse files Browse the repository at this point in the history
* Add mimalloc v2.12

Modified src/alloc.c to remove include of alloc-override.c and not
compile new handler.

Did not include the following files:

 - include/mimalloc-new-delete.h
 - include/mimalloc-override.h
 - src/alloc-override-osx.c
 - src/alloc-override.c
 - src/static.c
 - src/region.c

mimalloc is thread safe and shares a single heap across all runtimes,
therefore finalization and getting global allocated blocks across all
runtimes is different.

* mimalloc: minimal changes for use in Python:

 - remove debug spam for freeing large allocations
 - use same bytes (0xDD) for freed allocations in CPython and mimalloc
   This is important for the test_capi debug memory tests

* Don't export mimalloc symbol in libpython.
* Enable mimalloc as Python allocator option.
* Add mimalloc MIT license.
* Log mimalloc in Lib/test/pythoninfo.py.
* Document new mimalloc support.
* Use macro defs for exports as done in:
  python#31164

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Christian Heimes <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
  • Loading branch information
4 people authored and aisk committed Feb 11, 2024
1 parent 5cfe748 commit 5f7475d
Show file tree
Hide file tree
Showing 61 changed files with 16,789 additions and 158 deletions.
10 changes: 10 additions & 0 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,21 @@ PyPreConfig
* ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
memory allocator <pymalloc>` with :ref:`debug hooks
<pymem-debug-hooks>`.
* ``PYMEM_ALLOCATOR_MIMALLOC`` (``6``): use ``mimalloc``, a fast
malloc replacement.
* ``PYMEM_ALLOCATOR_MIMALLOC_DEBUG`` (``7``): use ``mimalloc``, a fast
malloc replacement with :ref:`debug hooks <pymem-debug-hooks>`.
``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` are
not supported if Python is :option:`configured using --without-pymalloc
<--without-pymalloc>`.
``PYMEM_ALLOCATOR_MIMALLOC`` and ``PYMEM_ALLOCATOR_MIMALLOC_DEBUG`` are
not supported if Python is :option:`configured using --without-mimalloc
<--without-mimalloc>` or if the underlying atomic support isn't
available.
See :ref:`Memory Management <memory>`.
Default: ``PYMEM_ALLOCATOR_NOT_SET``.
Expand Down
12 changes: 12 additions & 0 deletions Doc/c-api/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ Legend:
* ``malloc``: system allocators from the standard C library, C functions:
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`.
* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`.
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`. The pymalloc
allocator will be used if mimalloc support isn't available.
* "+ debug": with :ref:`debug hooks on the Python memory allocators
<pymem-debug-hooks>`.
* "Debug build": :ref:`Python build in debug mode <debug-build>`.
Expand Down Expand Up @@ -672,6 +674,16 @@ Customize pymalloc Arena Allocator
Set the arena allocator.
.. _mimalloc:
The mimalloc allocator
======================
.. versionadded:: 3.13
Python supports the mimalloc allocator when the underlying platform support is available.
mimalloc "is a general purpose allocator with excellent performance characteristics.
Initially developed by Daan Leijen for the runtime systems of the Koka and Lean languages."
tracemalloc C API
=================
Expand Down
26 changes: 26 additions & 0 deletions Doc/license.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1040,3 +1040,29 @@ https://www.w3.org/TR/xml-c14n2-testcases/ and is distributed under the
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


mimalloc
--------

MIT License

Copyright (c) 2018-2021 Microsoft Corporation, Daan Leijen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -921,13 +921,17 @@ conflict.
* ``pymalloc``: use the :ref:`pymalloc allocator <pymalloc>` for
:c:macro:`PYMEM_DOMAIN_MEM` and :c:macro:`PYMEM_DOMAIN_OBJ` domains and use
the :c:func:`malloc` function for the :c:macro:`PYMEM_DOMAIN_RAW` domain.
* ``mimalloc``: use the :ref:`mimalloc allocator <mimalloc>` for
:c:macro:`PYMEM_DOMAIN_MEM` and :c:macro:`PYMEM_DOMAIN_OBJ` domains and use
the :c:func:`malloc` function for the :c:macro:`PYMEM_DOMAIN_RAW` domain.

Install :ref:`debug hooks <pymem-debug-hooks>`:

* ``debug``: install debug hooks on top of the :ref:`default memory
allocators <default-memory-allocators>`.
* ``malloc_debug``: same as ``malloc`` but also install debug hooks.
* ``pymalloc_debug``: same as ``pymalloc`` but also install debug hooks.
* ``mimalloc_debug``: same as ``mimalloc`` but also install debug hooks.

.. versionchanged:: 3.7
Added the ``"default"`` allocator.
Expand Down
7 changes: 7 additions & 0 deletions Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,13 @@ also be used to improve performance.
Enable computed gotos in evaluation loop (enabled by default on supported
compilers).

.. option:: --without-mimalloc

Disable the fast mimalloc allocator :ref:`mimalloc <mimalloc>`
(enabled by default).

See also :envvar:`PYTHONMALLOC` environment variable.

.. option:: --without-pymalloc

Disable the specialized Python memory allocator :ref:`pymalloc <pymalloc>`
Expand Down
4 changes: 4 additions & 0 deletions Include/cpython/pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ typedef enum {
PYMEM_ALLOCATOR_PYMALLOC = 5,
PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6,
#endif
#ifdef WITH_MIMALLOC
PYMEM_ALLOCATOR_MIMALLOC = 7,
PYMEM_ALLOCATOR_MIMALLOC_DEBUG = 8,
#endif
} PyMemAllocatorName;


Expand Down
Loading

0 comments on commit 5f7475d

Please sign in to comment.