Discussion about this post

User's avatar
Trần Thành Long's avatar

A couple of questions:

1. People usually make a scratch arena locals to a thread using something like __declspec(thread). What if you're on a platform or project that can't use thread-local storage? I usually see people handle this by passing around a "thread context" Care to comment on that?

2. The push and pop operator on the current arena implementation aren't atomic, which mean they aren't thread-safe. How do you go about dealing with memory management for multithreading?

3. Do you think the idea of ArenaTemp can be made more abstract and applied to other kinds of allocators? For example, I may have a generic heap allocator that keeps a free list and allows for out-of-order alloc and free allocations. I can still create an ArenaTemp for this allocator by storing the free list in the ArenaTempBegin and restoring it in the ArenaTempEnd function. The same also goes for a pool allocator, for example. I can still store the current free list and reset it later.

Henry's avatar

Could anyone elaborate on common strategies for handling collections of dynamic lists?

A pattern I run into constantly is a list of lists indexed by resource ID, where each entry accumulates a dynamic list of data. A typical example would be buffering per-model draw requests in a renderer.

The approaches I'm familiar with all seem to have fairly unpleasant tradeoffs:

- Page-based allocation (mmap / VirtualAlloc): allocating a page per collection can produce a lot of TLB pressure when the number of collections grows.

- Chunked lists: avoid large reallocations but destroy contiguity.

- Quake-style freelists / gap reuse: keep things contiguous but make memory usage difficult to reason about, since fragmentation depends heavily on allocation and release order.

Are there common patterns I'm missing for this scenario? Or are these just the fundamental tradeoffs and the real problem is choosing which failure mode you want?

I'm also curious if people have better mental models for reasoning about the fragmentation behavior of freelist-style solutions in this scenario.

47 more comments...

No posts

Ready for more?