Often repeated array allocations

monarch_dodra monarchdodra at gmail.com
Mon Jul 22 01:35:23 PDT 2013


On Sunday, 21 July 2013 at 21:35:15 UTC, Namespace wrote:
> On Sunday, 21 July 2013 at 21:31:08 UTC, bearophile wrote:
>> Namespace:
>>
>>> I have a float[1024] buffer which is used, as long as the 
>>> requested size is less than 1024. If it's greater, I will 
>>> temporary allocate the whole array with new float[Size];
>>
>> That's one of the ways I solve similar problems.
>>
>>
>>> Any improvements? Or is 1024 to small / big?
>>
>> An idea is to add some debug{} code that computes statistics 
>> for the array sizes...
>>
>> Bye,
>> bearophile
>
> Too much effort. But if some of you have this experiences 
> already, I would gladly profit from them. ;)

It would really help to know what your function is doing exactly.

Personally, I think you are over thinking it. Just do this:

//----
void foo ()
{
     static float[] scratchPadBuffer;
     // use scratchPadBuffer here
}
//----

And simply make buffer grow as it needs, and never shorten it. 
You could consider this to be "controlled leak" I guess. But at 
least, with this approach, you can't "guess wrong", and you'll 
only very rarely allocate.


More information about the Digitalmars-d-learn mailing list