Topic: virtual versus dynamic


Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/02/02
Raw View
In article <917861033.740809@saturn.riv.be>, cminnoy@starlab.net
says...
> While I was experimenting with Delphi I noticed a rather remarkable feature
> that C++ doesn't support, namely dynamic methods. Dynamic methods are, just
> like virtual methods, late binded. The difference is size and speed.

[ ... ]

> Maybe it's worthwhile to explore the possibility to include such a
> feature also in C++. C++ always claims to be efficient with it's
> resources, and this feature seams to increase the resource handling
> capability (speed versus size).

This doesn't require any addition to the language.  You can handle
such considerations of size vs. speed without any new keywords, etc.

Possibilities include the compiler simply looking at the number of
functions involved, and the number that are overridden, and making a
decision based upon that.  A compiler might also do some profiling and
make decisions about how to handle things based upon how often
particular functions are used.

Note that these techniques are already well-known in present
compilers.  I'm not sure whether they use it to decide upon a format
for the vtable (I kind of doubt it) but the first is _commonly_ used
in generating code for case statements, and the second is used (e.g.
by HP's compiler, IIRC) for certain forms of optimization in general.

Right now, the language includes both ``register'' and ``inline'',
both of which are nearly obsolete for most compilers.  ``inline''
means a few other things that compilers can't ignore, but the general
idea remains: compiler technology is generally fairly quick overtake
things we add to languages in an attempt to help them out.

I also did a quick bit of looking through programs I have on hand.  On
average, the vtable seems to consume less than 1% of the programs.
Even if addition of the keyword could completely eliminate that (which
is clearly impossible) the best we could hope for from it would be a
1% reduction in program size and memory usage.  It might be that other
programs have much larger vtables than mine, but they'd clearly need
to be a LOT larger before this could possibly make a meaningful
difference.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/02/02
Raw View
Chris Minnoy wrote in message <917861033.740809@saturn.riv.be>...
[comments on dynamic methods snipped]

A couple of months ago, I developed a scheme for dispatching Windows
messages to C++ methods. The mechanism uses dynamic methods in a way that's
almost totally transparent to the user of the mechanism.
It has the following features:
- portable
- entirely type-safe (you don't have to make cast from WPARAM and LPARAM to
message-specific types)
- does not use switch at all
- it's reasonably efficient - exactly as Chris writes about dynamic methods
- it doesn't employ virtual functions, thus saving vtable space
- it uses only one macro
- it's easy to maintain - no tables and/or extra structures to keep track of
at the client side

If there's any interest in publishing that (lengthy) code in this newsgroup,
I'd be glad to.

Andrei
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/02/02
Raw View
Chris Minnoy wrote:
>
> While I was experimenting with Delphi I noticed a rather remarkable feature
> that C++ doesn't support, namely dynamic methods. Dynamic methods are, just
> like virtual methods, late binded. The difference is size and speed.
> In the Delphi helpfile I found:
>
> A method is made dynamic by including a dynamic directive in its declaration.
> Dynamic methods are semantically identical to virtual methods.

This sentence settles the issue. The standard only tells you
the semantics, not the implementation of virtual functions.
Compilers are allowed to implement virtual functions with
the Delphi dynamic method mechanism as well as with the usual
vtbl mechanism. It is even allowed to decide the used scheme
on a case-by-case analysis, or to make it dependant on compiler
options, pragmas, or even the method name (i.e. a compiler
which used dynamic invocation for all methods starting with
dyn would be conforming - not that I would like it).
Also, the compiler doesn't need to do any of those - it could
do a type id, and use a big switch statement internally.
Or do an if for the most common case, and use a vtbl or
"dtbl" otherwise.

[...]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Chris Minnoy" <cminnoy@starlab.net>
Date: 1999/02/01
Raw View
While I was experimenting with Delphi I noticed a rather remarkable feature
that C++ doesn't support, namely dynamic methods. Dynamic methods are, just
like virtual methods, late binded. The difference is size and speed.
In the Delphi helpfile I found:

A method is made dynamic by including a dynamic directive in its declaration.
Dynamic methods are semantically identical to virtual methods.
Virtual and dynamic methods differ only in the implementation of method call
dispatching at runtime; for all other purposes, the two types of methods can
be considered equivalent. In the implementation of virtual methods, the
compiler favors speed of call dispatching over code size. The
implementation of dynamic methods on the other hand favors code size
over speed of call dispatching.

In general, virtual methods are the most efficient way to implement
polymorphic behavior. Dynamic methods are useful only in situations
where a base class declares a large number of virtual methods, and an
application declares a large number of descendant classes with few
overrides of the inherited virtual methods.

Dynamic methods are virtual methods with a slightly different dispatch
mechanism.  Because dynamic methods don't have entries in the object's
virtual method table, they can reduce the amount of memory that objects
consume. However, dispatching dynamic methods is somewhat slower than
dispatching regular virtual methods.  If a method is called frequently,
or if its execution is time-critical, you should probably declare it as
virtual rather than dynamic.  Objects must store the addresses of their
dynamic methods. But instead of receiving entries in the virtual method
table, dynamic methods are listed separately. The dynamic method list
contains entries only for methods introduced or overridden by a
particular class. (The virtual method table, in contrast, includes all
of the object's virtual methods, both inherited and introduced.)
Inherited dynamic methods are dispatched by searching each ancestor's
dynamic method list, working backwards through the inheritance tree.


Maybe it's worthwhile to explore the possibility to include such a
feature also in C++. C++ always claims to be efficient with it's
resources, and this feature seams to increase the resource handling
capability (speed versus size).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]