#Correct way to use arena allocators?

1 messages · Page 1 of 1 (latest)

lethal whale
#

Hi, 0.5x dev here. Just wanted to hear some human feedback on the "zig" way of handling arenas and allocators. So far I have just been defining a bunch of arenas, each one corresponding to lifetimes of data so that I can reset them granularly. Also, if I pass an arena allocator into an ArrayList, do I ever need to call ArrayList.deinit()? Is this correct? ```zig pub var respArena: std.heap.ArenaAllocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub const respAllocator: std.mem.Allocator = respArena.allocator();

pub var persistentArena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub const persistentAllocator = persistentArena.allocator();

pub var browserArena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub const browserAllocator = browserArena.allocator();

pastel basin
#

Yes you are thinking correctly. Arenas are really good at pinning a lifetime of complex nested datastructures for example.

#

I wouldnt back the arenas with page allocator though unless you preheat them

lethal whale
pastel basin
#

but if it's only memory, once the arena is gone all the memory it held is gone

pastel basin
#

arenas will eventually defragment themselves if you use reset, but in general it's better back them up with general purpose allocator instead of page