News for hackers. Stuff that matters.
Re:The cost of dynamic vs. static dispatch in C++ by tomp on Thursday December 05, 2013 @11:24AM (#6855184)
Instead of devirtualization, a simpler optimization, which would additionally also help in the dynamic case, is simple loop hoisting of the method pointer fetch. Instead of doing

    while(...) {
      (obj->vtable[0])(...)
    }
we could have

    void(*fn)(...) = obj->vtable[0]
    while(...) {
      fn(...)
    }
which would avoid two redirections per inner loop! Actually, I'm almost sure that is what LuaJIT would do, and many other high-level programming languages could perform this optimization as well. However, maybe C is too low-level to be able to do that, and I don't know about C++.
Re:The cost of dynamic vs. static dispatch in C++ by eliben on Thursday December 05, 2013 @11:44AM (#6855319)
That would save the indirection, but I hope the article shows that by far the biggest cost comes from the lack of inlining. The latter would not be solved by your function pointer.
Re:The cost of dynamic vs. static dispatch in C++ by jheriko on Thursday December 05, 2013 @11:47AM (#6855363)
this is not about C... C++ certainly. C has no such thing as a virtual function or dynamic dispatch (unless you implement it yourself).