How Dynamic methods improve code-size??
What code-size means??? It means the program.exe file size??
What code-size means??? It means the program.exe file size??
According the Manual:
In general, virtual methods are the most efficient way to implement polymorphic behavior. Dynamic methods are useful when a base class declares many overridable methods that are inherited by many descendent classes in an application, but only occasionally overridden.
What i gain if i use dynamic instead of virtual when only one of the inherited classes override the method, since the manual also says:
virtual methods optimize for speed, while dynamic methods optimize for code size.
Virtual methods are implemented with a virtual method table (VMT). There is one VMT for each class. The VMT contains one entry for each virtual method in the class. And that entry is the address of the method.
This allows for very efficient calling. You simply get the address of the VMT which is located at a fixed offset from Self. Then you look up the method pointer by index and call the method.
What this does mean is that if you have a class with a lot of virtual methods, and you derive a sub-class, you will make a brand new VMT with all the virtual methods. And if you have not overridden many of them, then you'll find that the VMTs have a lot of overlap.
This used to matter in the days of 16 bit. The VMTs could take up a lot of space in the executable image (that's what is meant by code size) and you could run out of space for the VMTs. So dynamic methods were introduced. The analogue to the VMT is the dynamic method table, DMT. This is implemented differently to avoid the repetition when methods are not overridden. The downside is that calling dynamic methods is more expensive.
In modern times, since 32 bit, and especially with the very fat executables that Delphi produces, these size issues don't matter. And so all sound advice is to use virtual methods exclusively.
Virtual method table implementations are well understood and there are many references can be found to understand them. That's less so for dynamic methods which are rather quaint.
The best sources of information I have found are from Hallvard Vassbotn's blog:
No comments:
Post a Comment