[fpc-devel] Faster InitObject
Alexander Shishkin
alexvins at mail.ru
Thu Feb 23 10:20:40 CET 2012
23.02.2012 11:47, Amir пишет:
> 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?
1) You can override TObject.NewInstance, w|o calling inherited. (Init
instance is called from NewInstance) and do whatever you want
2) You can use objects instead of classes if Delphi class model overhead
is significant for you.
--
AVS
More information about the fpc-devel
mailing list