Skip to content

Add mimalloc memory allocator #90815

Description

@tiran
BPO 46657
Nosy @nascheme, @tiran, @corona10, @erlend-aasland, @h-vetinari
PRs
  • gh-90815: Add mimalloc memory allocator #31164
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2022-02-06.14:49:12.884>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = 'Add mimalloc memory allocator'
    updated_at = <Date 2022-03-24.02:10:23.628>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2022-03-24.02:10:23.628>
    actor = 'h-vetinari'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2022-02-06.14:49:12.884>
    creator = 'christian.heimes'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46657
    keywords = ['patch']
    message_count = 12.0
    messages = ['412639', '412641', '412645', '412654', '412669', '412679', '412741', '412749', '412796', '412835', '412867', '412986']
    nosy_count = 5.0
    nosy_names = ['nascheme', 'christian.heimes', 'corona10', 'erlendaasland', 'h-vetinari']
    pr_nums = ['31164']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46657'
    versions = ['Python 3.11']

    Linked PRs

    Activity

    1. tiran commented on Feb 6, 2022

      @tiran
      MemberAuthor

      From https://github.com/microsoft/mimalloc

      mimalloc (pronounced "me-malloc") is a general purpose allocator with excellent performance characteristics. Initially developed by Daan Leijen for the run-time systems of the Koka and Lean languages.

      mimalloc has several interesting properties that make it useful for CPython. Amongst other it is fast, thread-safe, and NUMA-aware. It has built-in free lists with multi-sharding and allocation heaps. While Python's obmalloc requires the GIL to protect its data structures, mimalloc uses mostly thread-local and atomic instructions (compare-and-swap) for efficiency. Sam Gross' nogil relies on mimalloc's thread safety and uses first-class heaps for heap walking GC.

      mimalloc works on majority of platforms and CPU architectures. However it requires a compiler with C11 atomics support. CentOS 7's default GCC is slightly too old, more recent GCC from Developer Toolset is required.

      For 3.11 I plan to integrate mimalloc as an optional drop-in replacement for obmalloc. Users will be able to compile CPython without mimalloc or disable mimalloc with PYTHONMALLOC env var. Since mimalloc will be optional in 3.11, Python won't depend or expose on any of the advanced features yet. The approach enables the community to test and give feedback with minimal risk of breakage.

      mimalloc sources will vendored without any option to use system libraries. Python's mimalloc requires several non-standard compile-time flags. In the future Python may extend or modify mimalloc for heap walking and nogil, too.

      (This is a tracking bug until I find time to finish a PEP.)

    2. added
      3.11only security fixes
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-featureA feature request or enhancement
      on Feb 6, 2022
    3. corona10 commented on Feb 6, 2022

      @corona10
      Member

      I add Neil to the nosy list since he is one of the kick-off members with this amazing works :)

    4. tiran commented on Feb 6, 2022

      @tiran
      MemberAuthor

      New features:

      • vendored mimalloc 2.0.3 + two patches from mimalloc dev branch. Mimalloc is embedded in obmalloc.o. Symbols are either hidden or names are mangled to have a _Py_ prefix.
      • ./configure --with[out]-mimalloc (default: yes), fails if atomics are not available.
      • PYTHONMALLOC=mimalloc, PYTHONMALLOC=mimalloc-debug env var settings
      • PYMEM_ALLOCATOR_MIMALLOC, PYMEM_ALLOCATOR_MIMALLOC_DEBUG
      • sys.debugmallocstats() and _PyObject_DebugMallocStats() prints mimalloc stats
      • sys._malloc_info struct, contains information about available and current allocator
    5. tiran commented on Feb 6, 2022

      @tiran
      MemberAuthor

      Buildbots "PPC64 Fedora PR" and all RHEL 7 build bots provided by David Edelsohn are failing because compiler is missing support for stdatomic.h.

    6. nascheme commented on Feb 6, 2022

      @nascheme
      Member

      Thanks, I'm indeed interested. Most credit goes to Christian for advancing this.

      For the missing stdatomic.h, would it be appropriate to have an autoconfig check for it? Can just disable mimalloc if it doesn't exist.

    7. tiran commented on Feb 6, 2022

      @tiran
      MemberAuthor

      We have an autoconf check for stdatomic.h. The test even verifies that a program with atomic_load_explicit() compiles and links.

      How do we want to use mimalloc in the future? Is it going to stay optional in 3.12? Then the default setting for --with-mimalloc should depend on presence of stdatomic.h. Do we want to make it mandatory for GC heap walking and nogil? Then --with-mimalloc should default to "yes" and configure should abort when stdatomic.h is missing.

      I'm leaning towards --with-mimalloc=yes. It will make users aware that they need a compiler with atomics:

      configure: error: --with-mimalloc requires stdatomic.h. Update your compiler or rebuild with --without-mimalloc. Python 3.12 will require stdatomic.

    8. tiran commented on Feb 7, 2022

      @tiran
      MemberAuthor

      ICC might be a problem. Apparently some version have an incomplete stdatomic.h, see bpo-37415.

    9. nascheme commented on Feb 7, 2022

      @nascheme
      Member

      My preference would be for --with-mimalloc=yes in an upcoming release. For platforms without the required stdatomic.h stuff, they can manually specify --with-mimalloc=no. That will make them aware that a future release of Python might no longer build (if mimalloc is no longer optional).

      A soft-landing for merging nogil is not a good enough reason to merge mimalloc, IMHO. nogil may never be merged. There should be some concrete and immediate advantage to switch to mimalloc. The idea of using the "heap walking" to improve is cyclic GC is not concrete enough. It's just an idea at this point.

      I think the (small) performance win could be enough of a reason to merge. This seems to be the most recent benchmark:

      https://github.com/proxy/gist.github.com/pablogsal/8027937b71cd30f17aaaa5ef7c885d3e

      There is also the long-term maintenance issue. So far, mimalloc upstream has been responsive. The mimalloc code is not so huge or complicated that we couldn't maintain it (if for some reason it gets abandoned upstream). However, I think we would prefer to maintain obmalloc rather than mimalloc, all else being equal. Abandonment by the upstream seems fairly unlikely. So, I'm not too concerned about maintenance.

    10. 51 remaining items

    11. added a commit that references this issue on Nov 3, 2023
    12. nascheme commented on Dec 23, 2023

      @nascheme
      Member

      I think this issue could be closed now that mimalloc went into HEAD already.

    13. erlend-aasland commented on Jan 4, 2024

      @erlend-aasland
      Contributor

      [...]
      There is also the long-term maintenance issue. So far, mimalloc upstream has been responsive. The mimalloc code is not so huge or complicated that we couldn't maintain it (if for some reason it gets abandoned upstream). However, I think we would prefer to maintain obmalloc rather than mimalloc, all else being equal. Abandonment by the upstream seems fairly unlikely. So, I'm not too concerned about maintenance.

      I made a similar point wrt the bundling at #109914 (comment).

      See follow-up issue:

    14. added 5 commits that reference this issue on Feb 11, 2024
    15. added a commit that references this issue on Apr 18, 2024
    16. added 5 commits that reference this issue on Sep 2, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions