From: Douglas Gregor (dgregor_at_[hidden])
Date: 2005-01-28 01:58:00


On Monday 24 January 2005 07:20 pm, Jonathan Turkanis wrote:
> I'm writing documentation for my interface library comparing the
> performance of interfaces vs. base classes with virtual functions. I was
> going cite a reduction in code bloat caused by virtual functions, with a
> link to the Boost.Function docs. But when I read the function docs again I
> was surprised: I had thought the bloat was caused by duplicate vtables
> being placed in different translation units;

That could contribute some, but...

> I see instead that the bloat
> cited is attributes to auxiliary type-classification functions.

That's what we found. The use of virtual functions makes a class polymorphic,
which requires (among other things) a virtual destructor, support for
dynamic_cast, and support for dynamic typeid: these things together were
causing a lot of code bloat.

> In the interface library I construct artificial vtables containing pointers
> to free functions, but each table has a slot reserved for a function which
> allows the type of the bound object to be partially recovered. Could this
> extra function mean that any improvement in code size achieved by
> eliminating virtual functions has been canceled out?

You'll probably still get some benefit. Function tries to save on space by
smashing about 7 different functions into 2 functions, so that less code is
generated. You might want to measure the effect in some typical use cases
first: using virtual functions is a heck of a lot nicer than dealing with a
library coded like Boost.Function, so you _really_ want to make sure it's
worth the effort (it can always be optimized later).

 Doug