#would it be a good idea to use one arena for every variable I have?
1 messages · Page 1 of 1 (latest)
and if ive deinited a arena can I then use its allocator again to allocate new stuff?
use reset instead if you want to continue using the arena
ah
is this a good way of doing memory management, or is there another best practice?
and is this a fast way or is it really slow
(new to memory management)
there's no silver bullet, arenas are fast because they keep very little state and have to ask the OS for memory infrequently as they grow, so you lose some of the benefit if you have a ton of separate arenas
with an arena, you're making a trade off between having fast allocation but also having to keep around those allocations until you can free them all at once, so using one arena per 'group' of related allocations is a decent strategy
what is a ton? I think i would use 6 of them.
i was thinking hundreds/thousands, 6 seems reasonable enough, but there's no 'correct' way to go, you'll need to benchmark to get an idea of what actually matters
if you haven't seen it, there's a bit about choosing allocators in the language reference: https://ziglang.org/documentation/master/#Choosing-an-Allocator
An arena is meant to be used when you have lots of allocations that you want to free at once. You usually use only one arena allocator with which you allocate a lot of data(many variables). So it's not ok to use one arena per variable. What you have to keep in mind is that when you deinit an arena, all variables allocated with it become invalid. If you want to deallocate variables individually, just use the GeneralPurposeAllocator.
ive seen it, afaik there is nothing about multiple arenas..
the problem is that i just very complex data-structures (Map(i32, Set(i32))) so i need to loop over the values also to deinit the sets, it would be nice to clear them all in once.
If you think that you will have a lot (and I mean a lot) of sets, then it might be worth it. But I suggest you do some benchmarks so as to see if there is any performance benefit from using arenas.
imo it's better to wrap those complex types in a struct and write a deinit method:
pub const Foo = struct {
map: Map(i32, Set(i32)),
pub fn deinit(self: Foo) void {
// iterate map, deinit each set
// deinit map
}
}
for me, the goal for data structures i write is to be able to use them with any allocator, so they should behave properly when using GeneralPurposeAllocator as well as ArenaAllocator, etc