[fpc-devel] Feature request/discussion - SetLengthNoInit

Michael Van Canneyt michael at freepascal.org
Mon Sep 14 20:41:18 CEST 2020



On Mon, 14 Sep 2020, J. Gareth Moreton via fpc-devel wrote:

> Hi everyone,
>
> This is something that came up in a discussion with a client. One thing 
> he noticed when using SetLength with dynamic arrays is that it can cause 
> a CPU bottleneck because it initialises the entire block to zero, which 
> is often undesirable if the array is going to be overwritten with its 
> own values (in his own example, he's passing the array to a GPU shader 
> as an output buffer).  I mentioned that the best ways around this is to 
> either pre-initialise the array and just reuse it where and when you 
> need it (object pooling) or to use GetMem and FreeMem, which increases 
> the danger of memory leaks, dangling pointers and removes the benefits 
> of a managed type.
>
> With all this in mind, would there be support for an intrinsic such as 
> "SetLengthNoInit" or "SetLengthNoClear" or some similar name that has 
> all the benefits of SetLength (especially the reference counting) but 
> doesn't initialise new elements to zero? If I'm learning one thing, Free 
> Pascal is finding a niche in some scientific applications, and speed can 
> be essential sometimes.

Better add an additional (optional) parameter SkipInit : Boolean = False;

Michael.


More information about the fpc-devel mailing list