#would it be a good idea to use one arena for every variable I have?

1 messages · Page 1 of 1 (latest)

graceful basalt
#

and how time-efficient would that be?

#

and if ive deinited a arena can I then use its allocator again to allocate new stuff?

graceful basalt
#

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)

wide torrent
#

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

graceful basalt
#

what is a ton? I think i would use 6 of them.

wide torrent
#

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

wise swift
#

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.

graceful basalt
#

ive seen it, afaik there is nothing about multiple arenas..

graceful basalt
wise swift
#

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.

wide torrent
# graceful basalt the problem is that i just very complex data-structures (Map(i32, Set(i32))) so ...

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