2 ms·
Instead of devirtualization, a simpler optimization, which would additionally also help in the dynamic case, is simple loop hoisting of the method pointer fetch
by tomp 13y ago
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++.
- deleted 13y ago[deleted]
- eliben 13y agoThat 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.
- jheriko 13y agothis is not about C... C++ certainly. C has no such thing as a virtual function or dynamic dispatch (unless you implement it yourself).