Unofficial Hacker News companion — get notified on comment replies, and more.

Doug Lea's Malloc (opens external site)

52 points by grep_it 6 days ago | gee.cs.oswego.edu | 32 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (16)

  1. It was a good single-threaded allocator back in the day but in the multicore era the design is now thoroughly obsolete. I suggest reading e.g. the jemalloc[1] and mimalloc[2] papers if you want to understand how modern allocators are implemented.

    [1] https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemal...

    [2] https://www.microsoft.com/en-us/research/wp-content/uploads/...

    Replies (6)
    1. Considering that global allocators are usually an anti-pattern and that most applications should have component-specific allocators, dlmalloc isn't all that bad when used through its mspace API. Of course, if one doesn't actually need a general allocator, they should simply use an arena allocator.
      Replies (3)
      1. Musl still supports single threaded slab allocations. In Musl, the difference comes down to one lock, some atomics, and a #define. My library extracts out its algorithm and let's you use scoped single and multi threaded Musl allocators freely mixed with each other.
        Replies (1)
        1. > the difference comes down to one lock, some atomics, and a #define

          Hopefully not on the alloc and free paths

          Replies (1)
          1. Musl does lock on alloc and free paths, yes. It is not a lockless allocator. It doesn't claim to be the very fastest allocator, but it is a reasonably fast modern (slab) allocator with relatively aggressive security mitigations. I assume the lock makes it easier to guarantee these mitigations.
            Replies (2)
            1. But it shouldn't take locks in your private arena?

              Continue this thread (1 replies) →

            2. jemalloc and tcmalloc are not lockless allocators, but they take care not to lock in the fast path of malloc and free.

              BTW mimalloc (a lockless allocator) has a secure mode, not sure how it compares to musl's mitigations.

      2. Unfortunately, ancient Unix tradition makes everything gravitate towards a global heap. You can do something different, but you're fighting the system when you do. There should be a Windows-style multi-heap allocator API, for example, but there isn't. Instead, making a new heap is something every random heap implementation has its own API. Common practice is for one component to free() memory allocated by another too, sadly.
      3. Global allocators are quite good actually. Most applications do not have component-specific allocators and are better off for it.
    2. Has anyone tried jev as an allocator?
      Replies (1)
      1. Brilliant. "End-to-end response time is 70ms-500ms for TypeSafe." https://typesafe.ai/blog/introducing-system-one-models-and-j... That could work for some use cases.
    3. Cosmopolitan uses dlmalloc and just wraps it with an rseq allocator for multicore performance. See also https://jart.dev/rseq/
    4. Still useful as an instructional aid though. Everyone should implement a simple malloc at least once just to demystify the heap.
    5. Would this logic apply to domain specific allocators? Memcached for example uses a pretty simple slab style allocator[0] to manage a preallocated pool of memory, and afaict, they have a mutex over any threads interaction with it.

      0: https://github.com/memcached/memcached/blob/master/slabs.c

    6. I recently contributed to jemalloc (a change related to heap profiling) and got curious how popular is jemalloc in general. https://github.com/jemalloc/jemalloc/wiki/Background the list is a little dated but still.

      > Major uses of jemalloc include:

      FreeBSD NetBSD Mozilla Firefox Varnish Cassandra Redis hhvm MariaDB Aerospike Android

      It was the default global memory allocator implementation for rust when it started (until 8 years ago https://www.reddit.com/r/rust/s/g6nwaGAdGA). Firefox adopted jemalloc on windows and later diverged (https://searchfox.org/firefox-main/source/memory/build/mozje...).