Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between dynamic and virtual methods?

Tags:

delphi

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.

like image 297
EProgrammerNotFound Avatar asked Mar 14 '13 17:03

EProgrammerNotFound


People also ask

What is a virtual dynamic?

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 ...

What does it mean for a method to be virtual?

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.

What is a non virtual 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.

What is non virtual function in c++?

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).


1 Answers

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:

  • http://hallvards.blogspot.co.uk/2007/03/hack15-overriding-message-and-dynamic.html
  • http://hallvards.blogspot.co.uk/2006/04/dynamic-methods-and-inherited.html?m=1
like image 117
David Heffernan Avatar answered Oct 04 '22 19:10

David Heffernan