Why Strings as Classes?
JAnderson
ask at me.com
Tue Aug 26 08:32:38 PDT 2008
Jb wrote:
> "Walter Bright" <newshound1 at digitalmars.com> wrote in message
> news:g90iia$2jc4$3 at digitalmars.com...
>> Yigal Chripun wrote:
>>> a) people here said that a virtual call will make it slow. How much
>>> slow? how much of an overhead is it on modern hardware considering also
>>> that this is a place where hardware manufacturers spend time on
>>> optimizations?
>> Virtual function calls have been a problem for hardware optimization.
>> Direct function calls can be speculatively executed, but not virtual ones,
>> because the hardware cannot predict where it will go. This means virtual
>> calls can be much slower than direct function calls.
>
> Modern x86 branch prediction treats indirect calls the same as conditional
> branches. They get a slot in the branch target buffer, so they do get
> speculatively executed. And if correctly predicted it's only a couple of
> cycles more costly direct calls.
>
> See the thread "Feature Request: nontrivial functions and vtable
> optimizations" about 2 weeks ago.
>
> I cited the technical docs and a few doubters ran benchmarks, which proved
> that virtual methods are not as evil as many people think. In fact they are
> no more evil than a conditional branch.
>
>
>
>
>
That's x86 hardware. Try something like the ps3. That systems has
little or no cache. They have to jump to the vtable which is in a
totally different location from the class. Note I'm not in the camp
that things they should never be used on these ystems however I think
you should use them smartly and profile profile profile.
One technique I've used in C++ to help improve things a little is to
switch the vtable with one that's in the same location or close to the
class. The wrapper function looked something like this:
class A {...}
A a = new LocalVirualTable<A>(); //ie LocalVirualTable is a bolt-in template
However, performance only improved in cases where I could flush the
cache. It many cases it was slightly worse on a x86 so you had to try
it, profile and see it it had a positive or negative effect in each
case. I imagine when you've got hundreds of these classes its simply
more memory to process, so on a high cache system it can be inversely
beneficial. I never tried it on a ps3 so it might be more effective there.
-Joel
More information about the Digitalmars-d
mailing list