[fpc-pascal] Feature announcement: Dynamic array extensions

Sven Barth pascaldragon at googlemail.com
Thu Jun 7 14:18:12 CEST 2018


Mark Morgan Lloyd <markMLl.fpc-pascal at telemetry.co.uk> schrieb am Do., 7.
Juni 2018, 11:46:

> On 04/06/18 12:00, Nitorami wrote:
> >>> It would be reasonable to assume that the predefined + might be>>
> substantially more efficient than a programmer-defined one could be.
> >> Yes, that's one of the reasons I vote for keeping the new feature>and
> allow to overload the operator.
> > I don't think that argument holds water. Concatenation of dynamic
> structuresis a slow function in the first place. I am not a core developer,
> but Iconsider that regardless how you do it, it will require a call to
> setlength,which in turn will call the operating system to allocate memory
> for the nowlarger structure. That takes much more time than a few assembly
> instructionsfor a normal pascal procedure call.
>
> The obvious workaround is to preallocate a dynamic array to a nominal
> size and then to trim it using SetLength(0), I'd assumed that it would
> retain ownership of the memory but I've just checked.
>
> What actually happens is that the memory is released back to the heap
> (but *not* to the OS, at least on Linux), with the result that
> concatenating elements will introduce a substantial hit particularly if
> space for a new element allocated from the heap isn't contiguous.
>

Of course SetLength(0) releases the memory as the value of a dynamic array
with length 0 is Nil, so nothing can be saved there.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180607/1c01d6fb/attachment.html>


More information about the fpc-pascal mailing list