> I was writing some C++, and in the middle of a tight loop, had> something like:>> Base *foo = ...;>> for (...) {> foo->virtual_method(...);> }>> Are compilers smart enough to lift the virtual lookup out of the loop?> Is this commonly done?

Most compilers that are any good should be hoisting this out of the loop. I
would certainly consider it a bug if a compiler doesn't. Such a bug could
come about because the implementor had dedicated an intermediate language
opcode to the operation (rather than spelling out the operation as a load
and call-register-indirect) but would more likely mean that the compiler
did a very poor job of hoisting generally.

A related but much more interesting problem is this:

Given a loop with several virtual function calls:

while (1) {
x->Func1();
x->Func2();
y->SomeOtherFuncMaybe();
etc...
}

When is it worthwhile hoisting the dispatching out of the loop and inlining
the loop? That is, you turn the loop into a virtual function so that the
virtual calls inside the loop can be statically bound and then pulled inline.

You need one for every type that can appear, so you probably want a fall back
function which just does the regular dispatching and there is some interesting
book-keeping to take care of as a type's virtual table is now dynamic rather
than static. Also, you probably want to drive this from a profiler which can
give statistics on what types were present at any point in the code rather than
just what code was executed.

Clearly, a lot of work for one optimization, but the speed-ups could be
spectacular.

(I threw in y so you can work on dynamic typing algorithms while
pretending to do optimization - if you only want to support single
dispatching you need to prove y is an x or else not pull that inline)

I have not seen any discussion of this in the literature. Indeed, I suspect
optimization of C++ is still a largely untapped field.
--