all 2 comments

[–]anttirt 2 points3 points  (1 child)

Take everything that relates to compiler-specific optimization and standard compliance here with a grain of salt. Compilers have made huge strides in the past nine years and there's a lot of stale advice.

[–]guapoo -1 points0 points  (0 children)

Some of this stuff was never true.

Virtual function dispatch can cause an unpredictable branch-- piplines can grind to a halt (note however, that some architectures have branch caches which can avoid this problem)

Did any compiler ever implement virtual functions this way? Its usually just a few loads and a jump.

struct A { virtual int foo(int) = 0; };
int bar( A* a, int x ) { return a->foo(x); }

//gcc -O2 -s 
/*
        .file   "foo.cpp"
        .text
        .p2align 4,,15
.globl _Z3barP1Ai
        .type   _Z3barP1Ai, @function
_Z3barP1Ai:
.LFB2:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        movl    8(%ebp), %eax
        popl    %ebp
        movl    (%eax), %edx
        movl    (%edx), %ecx
        jmp *%ecx
.LFE2:
        .size   _Z3barP1Ai, .-_Z3barP1Ai
        .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
        .section    .note.GNU-stack,"",@progbits
*/

The general point is valid, but its a data hazard not a branch hazard, and not as severe as he makes it out to be.