How Dynamic methods improve code-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.
vDSO (virtual dynamic shared object) is a kernel mechanism for exporting a carefully selected set of kernel space routines to user space applications so that applications can call these kernel space routines in-process, without incurring the performance penalty of a mode switch from user mode to kernel mode that is ...
A virtual method is one that is declared as virtual in the base class. A method is declared as virtual by specifying the keyword "virtual" in the method signature. A virtual method may or may not have a return type. Virtual methods allow subclasses of the type to override the method.
Methods that don't have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual. When a virtual method is invoked on an object, the run-time type of the object is used to determine which implementation of the method to use.
Non- virtual member functions are resolved statically. That is, the member function is selected statically (at compile-time) based on the type of the pointer (or reference) to the object. In contrast, virtual member functions are resolved dynamically (at run-time).
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With