Live data from Hacker News

Writing a Memory Allocator (2019)

dmitrysoshnikov.com

21–30 of 44 posts

Re: Writing a Memory Allocator (2019)

#21

Every time I hear someone talking about writing a memory allocator, it always reminds me of the brilliant CppCon 2015 talk by Andrei Alexandrescu about how he wrote an modular allocator library that can be built up in pieces to make whatever you like. https://www.youtube.com/watch?v=LIb3L4vKZ7U

I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.

Re: Writing a Memory Allocator (2019)

#22

Every time I hear someone talking about writing a memory allocator, it always reminds me of the brilliant CppCon 2015 talk by Andrei Alexandrescu about how he wrote an modular allocator library that can be built up in pieces to make whatever you like. https://www.youtube.com/watch?v=LIb3L4vKZ7U

I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.

The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays.

And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread.

Most of what you can find about writing allocators is written by people who don't actually know how; and actually useful allocators are made by people who don't write tutorials about it.

Re: Writing a Memory Allocator (2019)

#23

Implementing a memory allocator seems to require system calls. What happens when there's no OS? For example on an MCU like Arduino, is the memory allocator packaged along with the program data? If so and in the case of an Arduino, is the code for it available anywhere? I would be curious to see how this is implemented on an embedded system.

You could fairly easily implement a bitmapped memory allocator which hands out chunks of reserved memory. I think you could build a more complex allocator on top of that if needed. For a lot of embedded applications, you really want to avoid dynamic memory allocation though. Use mempools if you need dynamic memory, which will at least limit fragmentation.

Re: Writing a Memory Allocator (2019)

#24
post #22

Earlier quoted context omitted.

I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.

The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays. And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread. Most of what you can find about writing allocators is written by people who don't actually kn…

Thread-based allocators are a bit overrated. In apps it’s quite common for objects to migrate across threads, especially from workers to the main thread.

Re: Writing a Memory Allocator (2019)

#25
post #22

Earlier quoted context omitted.

I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.

The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays. And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread. Most of what you can find about writing allocators is written by people who don't actually kn…

What is he wrong about? He's upfront about limiting the scope to single-threaded arena allocators, but still mentions mmap and threads in passing. For talks that are mostly sales pitches, I think it's a pretty informative introduction to how to select and compare allocators.

Re: Writing a Memory Allocator (2019)

#26
post #22

Earlier quoted context omitted.

I also really like John Lakos' talks on allocators. He does a great job of systematically decomposing the problem space.

The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays. And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread. Most of what you can find about writing allocators is written by people who don't actually kn…

Can you explain what you mean by mmap is how memory is obtained nowadays?

Re: Writing a Memory Allocator (2019)

#27
post #22

Earlier quoted context omitted.

The only problem is that he is wrong on almost every detail. He has apparently never heard of mmap, which is awkward because mmap is how memory is obtained, nowadays. And if you want, you know, performance, it needs to know something about threads, pre-allocating chunks to assign to threads, and batching free ops per thread. Most of what you can find about writing allocators is written by people who don't actually kn…

Thread-based allocators are a bit overrated. In apps it’s quite common for objects to migrate across threads, especially from workers to the main thread.

That's not the point at all. Having a per-thread pool means synchronization-free alloc.

What happens with the object afterwards is not relevant.

Re: Writing a Memory Allocator (2019)

#28
I remember I did a memory allocator incompletely, and it did increase the speed of my application, but then debugging memory problem become less ideal because valgrind couldn't tell about memory boundaries any more. Make sure you put proper guard in debugging to trigger segfault :)

Re: Writing a Memory Allocator (2019)

#29
post #27

Earlier quoted context omitted.

Thread-based allocators are a bit overrated. In apps it’s quite common for objects to migrate across threads, especially from workers to the main thread.

That's not the point at all. Having a per-thread pool means synchronization-free alloc. What happens with the object afterwards is not relevant.

But objects are passed around between threads and another thread might want to free it?

Re: Writing a Memory Allocator (2019)

#30
post #29
post #27

Earlier quoted context omitted.

That's not the point at all. Having a per-thread pool means synchronization-free alloc. What happens with the object afterwards is not relevant.

But objects are passed around between threads and another thread might want to free it?

Then it goes to the freeing thread memory pool. Thread-aware mallocs know this and have clever balancing mechanisms for situations like producer-consumer, where one thread allocs a lot and another frees a lot.

Edit: wrong, I re-checked. The solution to this is that marking an object as free is lock-free. The freed objects do not immediately change thread pools.

Post reply on HN