We know about malloc() and free() but they can be pretty slow. What if I told you there's a faster technique used for high-performance C programming?
Let's talk about Memory Arenas.
(Thread)
What IS a memory arena?
A memory arena is a large, contiguous block of memory that you allocate once at the beginning of a task.
It's helpful because instead of using malloc() for each element, we can just work with our allocated memory.
First advantage: incredibly fast allocation
Allocating from a simple arena can be as fast as a single pointer addition. You just bump a pointer forward to reserve a new chunk of memory.
Here we have a conceptual code. This is orders of magnitude faster than malloc(), which has to search for free blocks and manage metadata.
Second advantage: instant deallocation
So, you don't free individual objects in an arena.
When you're done with all the objects, you free the entire arena at once.
In its simplest form, this can just be resetting a pointer:
The trade-off
So, if it's that good, what's the issue?
In a simple arena, you cannot free individual objects. You can only free the entire collection of objects at once.
This is why arenas are a specialized tool.
When to use arenas
Arenas are actually great for tasks where you allocate many objects that all have a similar, well-defined lifetime.
- Batch processing: Processing a single web request, compiling a single file, or rendering one frame of a game (for videogames, believe me, it's useful).
- Data structures: Building a complex data structure like a tree, where you know you'll destroy the entire tree at the same time.
Memory arenas are a class C pattern that trades the flexibility of freeing individual objects for massive gains in allocation and deallocation speed and cache performance.
If you found this useful, follow for more C / low level daily threads!
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.