[fpc-pascal] Feature announcement: Dynamic array extensions
Mark Morgan Lloyd
markMLl.fpc-pascal at telemetry.co.uk
Thu Jun 7 11:45:51 CEST 2018
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.
--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk
[Opinions above are the author's, not those of his employers or colleagues]
More information about the fpc-pascal
mailing list