Stack + Heap allocation store

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 28 01:46:28 UTC 2020


On 7/27/20 7:50 PM, Per Nordlöw wrote:
> Walter's talk on DConf 2019 talks about a favorite design pattern of his 
> - a hybrid approach to memory allocation.
> 
> A store first allocates on the stack and then when it grows too large it 
> moves the data to a heap allocation (array).
> 
> I know such a struct is not hard to write but I ask anyway to collect 
> advice on writing it.
> 
> Does Phobos have anything like that?
> 
> What about code.dlang.org?

Doesn't std.experimental.allocator provide such capabilities?

I think you can combine a Region allocator with a Fallback heap allocator.

In that case, all you need is something that will realloc when it needs 
more data, and the allocator will handle the details.

But depending on how movable the item needs to be, you may want to 
allocate it directly in the struct itself, and have a flag which 
indicates to use a pointer instead (like the small string optimization).

-Steve


More information about the Digitalmars-d mailing list