[fpc-devel] Faster InitObject

Sven Barth pascaldragon at googlemail.com
Thu Feb 23 09:27:33 CET 2012


Am 23.02.2012 08:47 schrieb "Amir" <amir at aavani.net>:
>
> Hi,
>
>   I have a code, developed in object pascal, with many classes. The
> project is working fine.
>   Today, I used callgrind (valgrind --tool=calgrind) to see which
> function consumes the most execution time and I noticed that the most of
> time in my project is consumed by fillchar function.
>
> Incl.   Self.   Called     Function
> 29.78   29.72    26M        SYSTEM_FILLCHAR$formal$INT64$BYTE
> 19.07   19.07    123M       SYSTEM_MOVE$formal$formal$INT64
>  3.63    3.63    70M       SYSTEM_SYSGETMEM_FIXED$QWORD$$POINTER
> ....
>
> The main caller of FillChar function is
> system_TObject_$__NewInstance$$TObject which called Fillchar 26 Million
> times.
>
> NewInstance method in TObject calls InitInstance which is an inline
> function and defined as follows:
>
>       class function TObject.InitInstance(instance : pointer) :
> tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
>
>         begin
>            { the size is saved at offset 0 }
>            fillchar(instance^, InstanceSize, 0);
>            { insert VMT pointer into the new created memory area }
>            { (in class methods self contains the VMT!)           }
>            ppointer(instance)^:=pointer(self);
>            if PVmt(self)^.vIntfTable <> @emptyintf then
>              InitInterfacePointers(self,instance);
>            InitInstance:=TObject(Instance);
>         end;
>
> My question is do we need to call fillchar in InitInstance? Or Is there
> any way to avoid calling InitInstance?

Yes, FillChar is needed, because a class instance is guaranteed to have
Bennett initialized with zero before the first constructor is called (this
way the fields are basically initialized by 0, Nil or '' depending on the
field type).

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20120223/242fc8a3/attachment.html>


More information about the fpc-devel mailing list