[fpc-pascal] Performance issue with dynamic arrays

Volker Zipfel maillist at fotoalbum2000.de
Wed Jul 9 16:44:29 CEST 2008


Hi all!

While analyzing my project with valgrind, I discovered that almost a quarter 
of the cpu cycle where eaten up by the function fpc_finalize_array. It seams 
that fpc_finalize_array calls for every element of the array fpc_finalize. To 
illustrate the problem i have written to small test programs:


program DynArray;
{$mode objfpc}{$H+}
var
 i: Integer;
 aArray: Array of Integer;
begin
 SetLength(aArray, 1000000);
 for i := 0 to 999999 do
   aArray[i] := i;
 aArray := nil;
end.      
program StaticArray;
{$mode objfpc}{$H+}
var
 i: Integer;
 aArray: PIntegerArray;
begin
 GetMem(aArray, 1000000);
 for i := 0 to 999999 do
   aArray^[i] := i;
 FreeMem(aArray);
end.
According to valgrind:

Total CPU cycles:
 DynArray: 32 243 842
 StaticArray: 2 107 419

And in the last line (aArray := nil; / FreeMem(aArray);):
 DynArray: 22 001 122
 StaticArray: 0

In order to avoid the unnecessary calls to fpc_finalize i added the following 
line to  fpc_finalize_array:

begin
 if PByte(typeinfo)^ in [tkAstring, tkWstring, tkArray, tkObject, tkRecord, 
tkInterface, tkDynArray, tkVariant] then   ...

After that i go:

Total CPU cycles - DynArray: 8 205 710.

As far as  I can see this change doesn't cause memory leaks.


Regards Volker

P.S.: I'm using rc_2_2_2.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.diff
Type: text/x-diff
Size: 505 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20080709/0ca13400/attachment.diff>


More information about the fpc-pascal mailing list