3 ms·
Good for didactic purposes but in the real world you may want to try an allocator like tcmalloc or jemalloc.
by partycoder 8y ago
Good for didactic purposes but in the real world you may want to try an allocator like tcmalloc or jemalloc.
- lasagnaphil 8y agoI’ve heard many gamedevs make specialized allocators for some of their code (the most being arena allocators)...
- 01100011 8y agoIt's fairly common to avoid naked malloc()/free() in systems with real-time requirements. Memory pools are a great way to go if you want deterministic behavior and better reliability.
- archi42 8y agoThis. If you're going to malloc/free equally sized objects often but randomly, a pool allocator can be a great improvement. With real time requirements you often care about your response time or worst case execution time. In some areas of embedded, safety critical systems you're usually prohibited from using heap at all (instead, stuff is put in global variables or on the stack - so you're only growing in one direction).
- Thaxll 8y agoAll major game engines have custom memory allocator.
- saagarjha 8y agoOr even the standard memory allocator provided by your system. I'm pretty sure this article was meant as a way to understand how malloc works and not as a high-performance replacement for the one you're currently using.
- apankrat 8y agoNot sure why you are downvoted, but this is a toy allocator.
- typon 8y agoIt's downvoted because it's missing the entire point of this article?
- wongarsu 8y agoIn the past I've sometimes achieved 20% or better speedups by writing custom allocators (speedup on overall performance, not just malloc performance). tcmalloc or jemalloc are great for the general case, but sometimes you know invariants about object sizes, alloc patterns and free patterns that allow much more performant allocation. The simplest case is if you know that you will free everything at once, or nothing at all. This allows you to eliminate most bookkeeping and allows a completely lock-free architecture. But there are also more complex cases where you can still get big benefits from exploiting known invariants.
- flurrything 8y ago> but sometimes you know invariants about object sizes, alloc patterns and free patterns that allow much more performant allocation. jemalloc allows you to query these invariants if you don't know them, and to use the information to re-configure the allocator to match them :/ I'm pretty sure most modern allocators allow you to do this as well. > The simplest case is if you know that you will free everything at once, or nothing at all. That's pretty much a one liner with jemalloc.