Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-06-25 20:51:21

On Tuesday 25 June 2002 05:55 pm, Andrei Alexandrescu wrote:
> > 2) Why use the vtable idiom when the variant is completely bounded? The
> > vtables are quite a bit larger than the single byte needed to specify
> which
> > type of the variant is selected, and I don't see any benefit to the
> > vtable approach.
> That's true, but the benefit is much smaller than you might think.
> Alignment would have the one-byte index occupy four bytes anyway, even if
> you put it after the aligned storage. Very few classes have an size that's
> not a multiple of 4. Besides, I believe (without having actually tested)
> that speed might be affected as well.

I misread the source and assumed that the vtable was dynamically allocated
(it's ~40 bytes). So size savings is a moot point.

I haven't done any speed tests yet (but will at some point), although I do
know that it is a common compiler optimization in OO languages to replace
virtual function calls with switch-on-type constructs when the set of
possible types is bounded at compile time. I can't possibly fathom how it
would be less efficient to branch to a known location based on an integer
comparison than to call an arbitrary memory location.

> On a related note, looking at the implementation reinforces my impression
> that programming with mpl is more trouble than it's worth. After including
> 13 (!) mpl headers, the net benefit is that more code must be written with
> mpl than without. For example, computing the maximum sized type in a
> typelist takes 26 non-blank lines, while the vanilla MaxSize takes 16 lines
> to do the same task. I didn't run a full analysis, but my impression after
> a couple of reads is that the difference in source file sizes (783 vs. 573
> lines) is partly due to using mpl.

Programming with function objects and STL algorithms often requires more lines
of code than the equivalent explicitly coded for-loop, but I don't see any
objections to that. MPL does the same thing at the metaprogramming level,
where metafunctions are factored out and supplied to generic algorithms.
What's the difference? [This is rapidly becoming off-topic. We should
probably take it into a separate MPL thread.]


Boost list run by bdawes at, gregod at, cpdaniel at, john at