ArenaAllocators don't play nicely with ArrayLists
21 points by ibobev 6 days ago | 3 comments

dnautics 13 minutes ago
> I know this is obvious, but I never actually thought about it. I'm probably not the only one.

It's pretty obvious, and I have thought about it, but I bet this is the sort of thing that I would reach for, forget about and lose some amount of time chasing down, so it's nice to have periodic reminders.

As an aside: sometimes, a linked list is the right datastructure.

reply
wasmperson 2 hours ago
All of my "arenas" have an additional fixed-length list of function pointers that they call in sequence before resetting/de-allocating the memory. That way they can manage any form of memory (or non-memory resource) you want:

  char *dat = malloc(42);
  arena_push_dtor(ar, dat, free);
  // use dat
Neatly solves the problem of stuff that's too awkward to put in linear memory while still letting you be lazy about cleanup.

Also: if you don't need the contiguity you can simply break up your dynamic array into linked buckets the same way the arena internally does with its own memory. Iteration and random access will still be fast.

reply
tynorf 4 hours ago
FWIW, if you aren’t interleaving other allocations (which includes on other threads), the ArenaAllocator in Zig doesn’t have this problem. If you attempt to resize the most recent allocation, it will do so in place if possible.

https://ziglang.org/documentation/0.16.0/std/#std.heap.Arena...

reply