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.
Index: rtl/inc/rtti.inc
===================================================================
--- rtl/inc/rtti.inc	(Revision 11340)
+++ rtl/inc/rtti.inc	(Arbeitskopie)
@@ -339,7 +339,8 @@
   var
      i : longint;
   begin
-     for i:=0 to count-1 do
-       int_finalize(data+size*i,typeinfo);
+     if PByte(typeinfo)^ in [tkAstring, tkWstring, tkArray, tkObject, tkRecord, tkInterface, tkDynArray, tkVariant] then 
+       for i:=0 to count-1 do
+         int_finalize(data+size*i,typeinfo);
   end;
 

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to