4 ms·
The jit can optimize away virtual function calls. ParentClass g = ....might be a subclass... for (int i=0;i<100000;i++) { g.func(i); } The
by yummyfajitas 10y ago
The jit can optimize away virtual function calls.
ParentClass g = ....might be a subclass...
for (int i=0;i<100000;i++) {
g.func(i);
}
The JIT can optimize away the vtable lookups to find func, and sometimes inline the code. Ok, maybe you could do this in C++ too.
ParentClass[] g = ....might be a subclass...
for (int i=0;i<100000;i++) {
g[i].func(i);
}
Suppose 99% of g's are the same class. The JIT can optimize away most of the virtual function lookups (particularly if this is one of your hotspots). I.e. the code becomes:
ParentClass[] g = ....might be a subclass...
for (int i=0;i<100000;i++) {
if (g.class == COMMONCLASS) {
inlined_func(i);
else {
g[i].func(i);
}
}
In the common case, a simple pointer comparison + inlined code is a lot faster than a function dereferene.
- richardwhiuk 10y agoYou could, I guess, annotate C++ child classes as likely, and then perform this optimization. That'd be an extension to the standard of course.
- vitalyd 10y agoDevirtualization is mostly an issue for Java since everything is virtual by default and the language doesn't have support for compile time monomorphization. While C++ code does use virtuals, it's nowhere near the amount as Java - there are language constructs to avoid that and move the dispatch selection to compile time.
- ahh 10y agoGCC in fact performs this optimization.
- yummyfajitas 10y agoGCC can get runtime information at compile time? That's a truly advanced compiler.
- ahh 10y agoIt can in fact make predictions in a number of circumstances. The most common case is FDO (https://gcc.gnu.org/wiki/AutoFDO https://gcc.gnu.org/wiki/AutoFDO) - with FDO coverage GCC can easily demonstrate a particular vcall is ~always a particular type and emit exactly the code you describe. (To be clear, this isn't just in theory, but is actually happening in a wide variety of binaries I use.) This is perhaps not fully "ahead of time", granted, but it's extremely easy to deploy and highly effective, and entirely accessible to C++.
- srean 10y agoSnark not warranted, profile guidance is a thing. But more likely many of those virtual functions will not be a virtual function in idiomatic C++ for performance oriented design. I have seen that I can get away with compile time polymorphism many a time. For the rest there is compiler devirtualization and profiles.