ArenaAllocators don't play nicely with ArrayLists

9 points by lvig


pervognsen

While this is true in the first-order sense, it is partly a symptom of a major pet peeve of mine, which is that people overuse and thereby misuse ArrayLists, Vecs, whatever you want to call them.

In most use-cases what you need is a relatively small pool of recycled ArrayLists that are used during construction of a data structure when the size of some lists are yet unknown; high-watermark capacity growth and length = 0 to reset makes them well-suited for that, and you can initialize lists in the pool with an unusually high capacity to skip most of the growth phase. When you know the final size of a list you allocate and copy the final perfectly sized array into its final resting place, using a separate allocator which almost always has a completely different lifetime from the builder. E.g. the lifetime of the builder context (I'm using that term very generally) is often tied to a worker thread whereas the final product is tied to some arbitrary external lifetime.

Obviously this doesn't cover all use-cases where people use ArrayLists but it covers a surprising number of them. In the remaining cases I would often consider alternative strategies altogether, for the allocation scheme, the data structure design, etc. It's hard to generalize but I tend to look pretty skeptically at non-transient uses of ArrayLists until I've analyzed the problem in detail, just because of how overused they tend to be.