Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2006-12-15 17:54:02

Sebastian Redl wrote:
> Joel de Guzman wrote:
>> Gennadiy Rozental wrote:
>>> It's possible. Still there code size will be significantly bigger in
>>> comparison with offline implementation, right?
>> Fusion vector, for
>> example is as fast as a struct.
> But Gennadiy is talking about code size, not execution speed. The very
> fact that makes Fusion so fast, its ability to compile down to code that
> looks like it was specifically written for the case at hand, means that
> every usage will have almost exactly the same code, but slightly
> different - as if it was written specifically for each case by hand.

Ok, let's see...

     for_each(x, f)




     for_each(y, f)

which gives:


so where is the overhead? It's as minimal as it can be. It can't
be any better than that. Duplication of code? Of course! There's
no way around that given the polymorphic nature of 'f'. Even
variant would generate these code.


     std::for_each(x, f) // x is a variant
     std::for_each(y, f) // y is a variant

in addition to what fusion does (zero overhead), the variant code
will also have to emit some switch code to dispatch to the correct
'f' (visitor) overload.


Really, this is all very wasteful of time. When in doubt, profile,
test! See the assembler. If you think variant can be as optimal as
fusion, be my guest, profile the code and examine the assembler
results (in terms of code size *and* speed).


Joel de Guzman

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