r/C_Programming • u/Ok_Outlandishness906 • 14d ago
Question A question about arenas.
I worked in C for years . i have used arenas a lot in the past. Today i have to modify an old c program , nothing difficult but i found something that made me think. Who developed the code ( very well written and documented ) used an arena for allocating variable.
What 's seems to me strange is that he did not used an heap memory area ( malloc ) but it used a char [] buffer and used it for its arena . At that point he could obviously allocate directly variable in stack instead he grabs all its variable from that buffer . Why a choice like that ? i thought for the CPU cache L1/L2 and so on ( all the "arena" will likely be in cache of the cpu ) but it is only an ipotesys and i was asking to you , in your opinion , the reason of a such choice .
Just curiosity .
Thanks
PP
5
u/ffd9k 14d ago
Storing lots of data on the stack is a very bad idea, because the stack is very limited (just a few megabytes by default).
Storing lots of data in a big buffer declared as a static/global variable is fine. It will typically be stored in the .bss section which can use much of the huge address space and all available RAM/swap, so on a modern computer you can store gigabytes like this without a problem.
19
u/treemcgee42 14d ago
Was it a global array? If the objects are in a global array, they can be used non-locally, e.g. across different functions. This is nice for libraries to completely avoid heap allocation, in case the user has hard requirements or wants to manage all dynamic allocations themselves.
7
u/mjmvideos 14d ago
Oh!!! hypothesis!!
18
u/runningOverA 14d ago
Among other things, using char[large] instead of malloc() reduces one level of indirection at run time.
The compiler knows the address of the char[] buffer during compile time, but not the malloc()-ed space.
ie. char buffer is faster.
10
u/tstanisl 14d ago
On some embedded platforms the stack may be very limited (like a few kiBs). Even though RAM is a lot larger. Where the
char[]buffer is allocated? Is it a global variable?5
u/ReallyEvilRob 14d ago
No need for malloc if using a global or static array.
1
u/gremolata 14d ago
So that he could pass arena to other functions being called?
Or substitute it later for a version that's not stack-allocated?
2
u/UnrealHallucinator 14d ago
Stack allocated memory is faster and the array would be contiguous in memory, so better cache locality as well. Also like another comment said, the compiler knows where the buffer is.
2
u/LumpySpecial5351 14d ago
I always thought that it was the granularity, the overhead for each allocation that drove the use of arenas. I believe that an malloc of one byte costs more than one byte, whereas an arena can be tailored per application.
I strive ever towards fully static memory allocation on my embedded ESP32 projects and avoid malloc and have never used an arena.
Burtrum
2
u/August_72_West 14d ago
What is an arena?
1
u/Scootztheprgrmmer 14d ago
Saving memory or saving time that seems to he the reasoning here but yea you'd think malloc would be used but if it could save a bit of time the other way may be better definitely a unique way to do it
1
u/DawnOnTheEdge 13d ago
One reason to use arenas is so different threads aren’t constantly blocked, waiting for their turn with the global heap.
Another is to avoid freeing each allocation individually. In many algorithms, a task needs some scratch space that can all be freed at once, but must be dynamically allocated of unknown size, or might need to be returned from helper functions, so local variable won’t suffice. Linux does provide an
alloca()function to dynamically allocate bytes on the stack, and C99 introduced a now-deprecated variable-length array feature.1
u/Dangerous_Region1682 13d ago
Since most processors these days cache virtual addresses as well as physical addresses assuming things are going to be retained in the L1 cache as long as you think is unlikely as context switching will likely displace more than you would desire.
The virtual address defines the cache slot if you like and the physical address defines the process effectively. Yes I know it’s more complex than this as there’s a TLB at play here.
The reality is you might expect an arena to be largely cache by access activity, but the reality is once you get more than a few context switches away the L1 cache is largely flushed of your data.
Yes I known this isn’t a 100% accurate view of how things exactly work, but the reality is that an L1 cache is rarely as useful as you would think on systems with context switches and many switches to kernel space and back. The same cache is used by the things you don’t see when looking at one process actually running on a time sharing system with interrupt handling, system call occurrences and context switches going on. They are great things to have for performance improvement within your time slice but entry persistence is not as good as one would think.
1
u/PlacentaOnOnionGravy 13d ago
Sounds like deterministic memory management
1
u/FLMKane 10d ago
The answer to all C programming arenas is Quake 3 Arena.
/s
1
u/AntMan5421 8d ago
I like to do this too if i know I won't exceed the stack size! i think stack size >> L1 cache so in the end it's more about how you lay out your variables in the buffer than anything else. but I might be in the wrong here :)
0
u/rubidus-api 14d ago
An arena inside a char array, whether it's on the stack or static, has a serious problem in principle. Under C's effective-type rules, an object with a declared type always has that type, so a declared char array stays char, and accessing parts of it as a struct isn't allowed. Copying the data in with memcpy doesn't fix it either: memcpy passes the effective type on only to memory that has no declared type, such as memory from malloc. The aliasing rule lets you access any object as char, not a char array as anything else. In practice this almost never causes trouble, and code like this is everywhere, but I think a C programmer should at least know about this contradiction. (If it worries you, put the arena in memory from malloc, or build with -fno-strict-aliasing.)
While you're at it, make sure the buffer is aligned for what you put in it (for example alignas(max_align_t) in C11); C doesn't promise that for a char array.
Apart from that, and from where it lives, an arena on the stack isn't very different from one on the heap. Allocating on the stack is usually faster than a heap allocation, but when you take one large block once, that hardly matters. The cache doesn't care whether memory is on the stack or the heap either; what counts is what you touched recently. Unless other hot variables sit right next to it, I don't think a stack arena is better or worse for cache hits than a heap one. And a 64 KB buffer is already larger than the L1 data cache of most x86 cores (32–48 KB), so the whole arena can't stay in L1 anyway.
Where malloc isn't allowed, you can build the arena in a static char array. You get the same benefits as with a heap arena: you take the memory all at once when you choose, and release it (or reuse it for something else) when you choose.
So what's the difference from just using local variables? I still think it's the same thing: you decide when a group of objects comes into being and when its space is released or reused, even across functions. Local variables live exactly as long as the block they're declared in, so you can't control their lifetimes that freely.
Arenas also combine well with pools. Say you create and destroy structs of one type very often. You can keep an array of pointers to that type as a stack: when an object is no longer needed, push its address, and the next allocation pops it and reuses the space. That's what's usually called a pool with a free list.
And since the code only talks to an arena, the same code can run on top of a static buffer, a stack buffer or malloc. If it's shared with environments where malloc is available, that alone is a good reason to write it this way.
It isn't completely free, though. Objects carved out of an arena usually have to live in memory, so the compiler can't keep them in registers the way it often does with locals, and tools like AddressSanitizer can't see the boundaries between objects inside one buffer.
Finally, this is more about having an allocator layer than about arenas as such, but once all allocation and release go through one place, you can add debugging or monitoring there in one go.