www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Stack + Heap allocation store

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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?
Jul 27 2020
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jul 27 2020
prev sibling next sibling parent reply Mathias LANG <geod24 gmail.com> writes:
On Monday, 27 July 2020 at 23:50:13 UTC, 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?
There's https://github.com/dlang/phobos/blob/master/std/internal/scopebuffer.d However, beware, here be dragons.
Jul 27 2020
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 28 July 2020 at 04:16:51 UTC, Mathias LANG wrote:
 There's 
 https://github.com/dlang/phobos/blob/master/std/internal/scopebuffer.d
 However, beware, here be dragons.
Which are its issues?
Jul 28 2020
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 28 July 2020 at 14:02:55 UTC, Per Nordlöw wrote:
 Which are its issues?
I looked the code. I see what you mean now. Seems like a template param for initial size is preferred over the unsafe buffer pointer to the ctor.
Jul 28 2020
parent Ben Jones <fake fake.fake> writes:
On Tuesday, 28 July 2020 at 14:19:25 UTC, Per Nordlöw wrote:
 On Tuesday, 28 July 2020 at 14:02:55 UTC, Per Nordlöw wrote:
 Which are its issues?
I looked the code. I see what you mean now. Seems like a template param for initial size is preferred over the unsafe buffer pointer to the ctor.
Not sure it's bulletproof, but I have something like that here: https://github.com/benjones/dtriangulate/blob/master/source/dtriangulate/ssoVector.d
Jul 28 2020
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 27 July 2020 at 23:50:13 UTC, 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?
http://dpldocs.info/experimental-docs/std.experimental.allocator.showcase.StackFront.html
Jul 28 2020