170 points · by DyslexicAtheist · 5y ago · 40 comments
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.
You can build a jemalloc in about 10 lines roughly.
It's quite simple and extensively commented.
[0] https://web.archive.org/web/20190126213344/https://www.drdob...
[1] https://github.com/dlang/dmd/blob/025caed79/src/dmd/root/rme...
This is one of the reasons why people write custom allocators - to suit a specific purpose where the allocation pattern is known well in advance.
Debugging it is great fun as well.
Toying with allocators is one of my favorite programming pastimes for some reason.