Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 27, 2020 Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
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? |
July 27, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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
|
July 28, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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. |
July 28, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mathias LANG | 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?
|
July 28, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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.
|
July 28, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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 |
July 28, 2020 Re: Stack + Heap allocation store | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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 |
Copyright © 1999-2021 by the D Language Foundation