On 2010-05-20 15:47:22 -0400, bearophile <[email protected]> said:
Michel Fortin:
Devirtualization is only possible in certain cases: when the function
knows exactly which type it'll get.<
You are wrong, in most cases there are ways to de-virtualize, even when
the runtime type isn't exactly known, but sometimes to do it you have
to work too much. This is probably why C# dotnet doesn't perform this
optimization.
It's a complex topic, I suggest you to read about it, I can't explain
here, see polymorpic call points, megamorphic ones, dispatch trees, and
so on.
I know I simplified a bit, and I'll admit you may know more than me
about dynamic dispatch optimizations. But if I'm not mistaken other
devirtualizing solutions are creating multiple instances of the code
path based on either static info or a runtime switch. All this isn't
very different from calling a templated function, but it's more complex
and less reliable (because those optimizations might not be in the
compiler, or might not be applicable to all scenarios).
My point all along was that it's better to stick with templates, where
you're "guarantied" to get the "optimal" code path. The downside is
that you lose the dynamic dispatch capabilities. But I believe those
are rarely needed in most programs. And if you really need it it's
quite easy and efficient to layer dynamic dispatch over static calls
(much more than the reverse).
--
Michel Fortin
[email protected]
http://michelf.com/