News for hackers. Stuff that matters.
Re:The Performance Impact of C++'s `final` Keyword by mgaunard on Monday April 22, 2024 @02:01PM (#40116928)
What final enables is devirtualization in certain cases. The main advantage of devirtualization is that it is necessary for inlining.

Inlining has other requirements as well -- LTO pretty much covers it.

The article doesn't have sufficient data to tell whether the testcase is built in such a way that any of these optimizations can happen or is beneficial.

Re:The Performance Impact of C++'s `final` Keyword by Negitivefrags on Monday April 22, 2024 @02:55PM (#40117555)
See this is why I find this odd.

Is there a theory as to how devirtualisation could hurt performance?

Re:The Performance Impact of C++'s `final` Keyword by phire on Monday April 22, 2024 @04:10PM (#40119416)
Jumps/calls are actually be pretty cheap with modern branch predictors. Even indirect calls through vtables, which is the opposite of most programmers intuition.

And if the devirtualisation leads to inlining, that results in code bloat which can lower performance though more instruction cache misses, which are not cheap.

Inlining is actually pretty evil. It almost always speeds things up for microbenchmarks, as such benchmarks easily fit in icache. So programmers and modern compilers often go out of their way to do more inlining. But when you apply too much inlining to a whole program, things start to slow down.

But it's not like inlining is universally bad in larger program, inlining can enable further optimisations, mostly because it allows constant propagation to travel across function boundaries.

Basically, compilers need better heuristics about when they should be inlining. If it's just saving the overhead of a lightweight call, then they shouldn't be inlining.

Re:The Performance Impact of C++'s `final` Keyword by qsdf38100 on Monday April 22, 2024 @04:54PM (#40120402)
"Inlining is actually pretty evil".

No it's not. Except if you __force_inline__ everything, of course.

Inlining reduces the number of instructions in a lot of cases. Especially when things are abstracted and factored with lot of indirections into small functions that calls other small functions and so on. Consider a 'isEmpty' function, which dissolves to 1 cpu instruction once inlined, compared with a call/save reg/compare/return. Highly dynamic code (with most functions being virtual) tend to result in a fest of chained calls, jumping into functions doing very little work. Yes the stack is usually hot and fast, but spending 80% of the instructions doing stack management is still a big waste.

Compilers already have good heuristics about when they should be inlining, chances are they are a lot better at it than you. They don't always inline, and that's not possible anyway.

My experience is that compiler do marvels with inlining decisions when there are lots of small functions they _can_ inline if they want to. It gives the compiler a lot of freedom. Lambdas are great for that as well.

Make sure you make the most possible compile-time information available to the compiler, factor your code, don't have huge functions, and let the compiler do its magic. As a plus, you can have high level abstractions, deep hierarchies, and still get excellent performances.

Re:The Performance Impact of C++'s `final` Keyword by grdbjydcv on Monday April 22, 2024 @06:41PM (#40123388)
The “evilness” is just that sometimes if you inline aggressively in a microbenchmark things get faster but in real programs things get slower.

As you say: “chances are they are a lot better at it than you”. Infrequently they are not.

Re:The Performance Impact of C++'s `final` Keyword by EasyMark on Monday April 22, 2024 @11:16PM (#40128074)
doesn't the compiler usually do well enough that you really only need to worry about time critical sections of code? Even then you could go in and look at the assembler and see if it's being inlined, no?
Re:The Performance Impact of C++'s `final` Keyword by usefulcat on Tuesday April 23, 2024 @12:05AM (#40128378)
I find that gcc and clang are so aggressive about inlining that it's usually more effective to tell them what not to inline.

In a moderately-sized codebase I regularly work on, I use __attribute__((noinline)) nearly ten times as often as __attribute__((always_inline)). And I use __attribute__((cold)) even more than noinline.

So yeah, I can kind of see why someone would say inlining is 'evil', though I think it's more accurate to say that it's just not possible for compilers to figure out these kinds of details without copious hints (like PGO).

Re:The Performance Impact of C++'s `final` Keyword by jandrewrogers on Tuesday April 23, 2024 @12:54AM (#40128647)
+1 on the __attribute__((cold)). Compilers so aggressively optimize based on their heuristics that you spend more time telling them that an apparent optimization opportunity is not actually an optimization.

When writing ultra-robust code that has to survive every vaguely plausible contingency in a graceful way, the code is littered with code paths that only exist for astronomically improbable situations. The branch predictor can figure this out but the compiler frequently cannot without explicit instructions to not pollute the i-cache.

Re:The Performance Impact of C++'s `final` Keyword by somenameforme on Tuesday April 23, 2024 @10:05AM (#40132147)
I find the Unreal Engine source to be a reasonable reference for C++ discussions, because it runs just unbelievably well for what it does, and on a huge array of hardware (and software). And it's explicit with inlining, other hints, and even a million things that could be easily called micro-optimizations, to a somewhat absurd degree. So I'd take away two conclusions from this.

The first is that when building a code base you don't necessarily know what it's being compiled with. And so even if there were a super-amazing compiler, there's no guarantee that's what will be compiling your code. Making it explicit, so long as you have a reasonably good idea of what you're doing, is generally just a good idea. It also conveys intent to some degree, especially things like final.

The second is that I think the saying 'premature optimization is the root of all evil' is the root of all evil. Because that mindset has gradually transitioned to being against optimization in general outside of the most primitive things like not running critical sections in O(N^2) when they could be O(N). And I think it's this mindset that has gradually brought us to where we are today where need what what would have been a literal supercomputer not that long ago, to run a word processor. It's like death by a thousand cuts, and quite ridiculous.

Re:The Performance Impact of C++'s `final` Keyword by moring on Wednesday April 24, 2024 @03:51AM (#40141770)
> The second is that I think the saying 'premature optimization is the root of all evil' is the root of all evil.

The greater evil is putting a one-sentence quote out of context:

""" There is no doubt that the grail of efficiency leads to abuse. Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified. It is often a mistake to make a priori judgments about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been that their intuitive guesses fail. After working with such tools for seven years, I've become convinced that all compilers written from now on should be designed to provide all programmers with feedback indicating what parts of their programs are costing the most; indeed, this feedback should be supplied automatically unless it has been specifically turned off. """

Re:The Performance Impact of C++'s `final` Keyword by somenameforme on Wednesday April 24, 2024 @05:20AM (#40142241)
Indeed, but I think even that advice, with context, is pretty debatable. Obviously one should prioritize critical sections, but completely ignoring those "small efficiencies" is certainly a big part of how we got to where we are today in software performance. A 10% jump in performance is huge; whether that comes from a single 10% jump, or a hundred 0.1% jumps - it's exactly the same!

So referencing something in particular from Unreal Engine, they actually created a caching system for converting between a quaternion and a rotator (euler rotation)! Obviously that sort of conversion isn't going to, in a million years, be even close to a bottleneck. That conversion is quite cheap on modern hardware, and so that caching system probably only gives the engine one of those 0.1% boosts in performance. But there are literally thousands of these "small efficiencies" spread all throughout the code. And it yields a final product that runs dramatically better than comparable engines.

Re:The Performance Impact of C++'s `final` Keyword by mgaunard on Thursday April 25, 2024 @04:03AM (#40154757)
That's not how compounding percentages works.
Re:The Performance Impact of C++'s `final` Keyword by a_e_k on Tuesday April 23, 2024 @12:16AM (#40128448)
Another for the pro side: inlining can allow for better branch prediction if the different call sites would tend to drive different code paths in the function.
Re:The Performance Impact of C++'s `final` Keyword by phire on Tuesday April 23, 2024 @01:23AM (#40128773)
This was true 15 years ago, but not so much today.

The branch predictors actually hash the history of the last few branches taken into the branch prediction query. So the exact same branch within a child function will map different branch predictors entries depending on which parent function it was called from, and there is no benifit to inlining.

It also means that branch predictor can also learn correlations between branches within a function. Like when a branches at the top and bottom of functions share conditions, or have inverted conditions.

Re:The Performance Impact of C++'s `final` Keyword by hansvm on Monday April 22, 2024 @03:17PM (#40117756)
There's a cost to loading more instructions, especially if you have more types of instructions.

The main advantages to inlining are (1) avoiding a jump and other function call overhead, (2) the ability to push down optimizations.

If you execute the "same" code (same instructions, different location) in many places that can cause cache evictions and other slowdowns. It's worse if some minor optimizations were applied by the inlining, so you have more types of instructions to unpack.

The question, roughly, is whether the gains exceed the costs. This can be a bit hard to determine because it can depend on the size of the whole program and other non-local parameters, leading to performance cliffs at various stages of complexity. Microbenchmarks will tend to suggest inlining is better in more cases that it actually is.

Over time you get a feel for which functions should be inlined. E.g., very often you'll have guard clauses or whatnot around a trivial amount of work when the caller is expected to be able to prove the guarded information at compile-time. A function call takes space in the generated assembly too, and if you're only guarding a few instructions it's usually worth forcing an inline (even in places where the compiler's heuristics would choose not to because the guard clauses take up too much space), regardless of the potential cache costs.

Re:The Performance Impact of C++'s `final` Keyword by cogman10 on Monday April 22, 2024 @03:25PM (#40117822)
Through inlining.

If you have something like a `while` loop and that while loop's instructions fit neatly on the cache line, then executing that loop can be quiet fast even if you have to jump to different code locations to do the internals. However, if you pump in more instructions in that loop you can exceed the length of the cache line which causes you to need more memory loads to do the same work.

It can also create more code. A method that took a `foo(NotFinal& bar)` could be duplicated by the compiler for the specialized cases which would be bad if there's a lot of implementations of `NotFinal` that end up being marshalled into foo. You could end up loading multiple implementations of the same function which may be slower than just keeping the virtual dispatch tables warm.

Re:The Performance Impact of C++'s `final` Keyword by masklinn on Monday April 22, 2024 @03:21PM (#40117789)
Code bloat causing icache evictions?
Re:The Performance Impact of C++'s `final` Keyword by samus on Monday April 22, 2024 @03:12PM (#40117711)
Devirtualization maybe not necessarily, but inlining might make code fail to fit into instruction caches.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @06:53PM (#40123832)
Practically - it never does. It is always cheaper to perform a direct, possibly inlined, call (devirtualization != inlining) than a virtual one.

Guarded devirtualization is also cheaper than virtual calls, even when it has to do

    if (instance is SpecificType st) { st.Call() }
    else { instance.Call() } 
or even chain multiple checks at once (with either regular ifs or emitting a jump table)

This technique is heavily used in various forms by .NET, JVM and JavaScript JIT implementations (other platforms also do that, but these are the major ones)

The first two devirtualize virtual and interface calls (important in Java because all calls default to virtual, important in C# because people like to abuse interfaces and occasionally inheritance, C# delegates are also devirtualized/inlined now). The JS JIT (like V8) performs "inline caching" which is similar where for known object shapes property access is shape type identifier comparison and direct property read instead of keyed lookup which is way more expensive.

Re:The Performance Impact of C++'s `final` Keyword by ynik on Tuesday April 23, 2024 @03:13AM (#40129373)
Caution! If you compare across languages like that, not all virtual calls are implemented equally. A C++ virtual call is just a load from a fixed offset in the vtbl followed by an indirect call. This is fairly cheap, on modern CPUs pretty much the same as a non-virtual non-inlined call. A Java/C# interface call involves a lot more stuff, because there's no single fixed vtbl offset that's valid for all classes implementing the interface.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @03:32AM (#40129486)
Yes, it is true that there is difference. I'm not sure about JVM implementation details but the reason the comment says "virtual and interface" calls is to outline it. Virtual calls in .NET are sufficiently close[0] to virtual calls in C++. Interface calls, however, are coded differently[1].

Also you are correct - virtual calls are not terribly expensive, but they encroach on ever limited* CPU resources like indirect jump and load predictors and, as noted in parent comments, block inlining, which is highly undesirable.

[0] https://github.com/dotnet/runtime/blob/5111fdc0dc464f01647d6...

[1] https://github.com/dotnet/runtime/blob/main/docs/design/core... (mind you, the text was initially written 18 years ago, wow)

* through great effort of our industry to take back whatever performance wins each generation brings with even more abstractions that fail to improve our productivity

Re:The Performance Impact of C++'s `final` Keyword by variadix on Monday April 22, 2024 @07:42PM (#40126867)
It basically never should unless the inliner made a terrible judgement. Devirtualizing in C++ can remove 3 levels of pointer chasing, all of which could be cache misses. Many optimizations in modern compilers require the context of the function to be inlined to make major optimizations, which requires devirtualization. The only downside is I$ pressure, but this is generally not a problem because hot loops are usually tight.
Re:The Performance Impact of C++'s `final` Keyword by bandrami on Tuesday April 23, 2024 @12:51AM (#40128634)
If it's done badly, the same code that runs N times also gets cached N times because it's in N different locations in memory rather than one location that gets jumped to. Modern compilers and schedulers will eliminate a lot of that (but probably not for anything much smaller than a page), but in general there's always a tradeoff.
Re:The Performance Impact of C++'s `final` Keyword by i80and on Monday April 22, 2024 @02:24PM (#40117201)
If you already have LTO, can't the compiler determine this information for devirtualization purposes on its own?
Re:The Performance Impact of C++'s `final` Keyword by ot on Monday April 22, 2024 @02:55PM (#40117547)
In general the compiler/linker cannot assume that derived classes won't arrive later through a shared object.

You can tell it "I won't do that" though with additional flags, like Clang's -fwhole-program-vtables, and even then it's not that simple. There was an effort in Clang to better support whole program devirtualization, but I haven't been following what kind of progress has been made: https://groups.google.com/g/llvm-dev/c/6LfIiAo9g68?pli=1

Re:The Performance Impact of C++'s `final` Keyword by Slix on Tuesday April 23, 2024 @04:44AM (#40129839)
This optimization option isn't on by default? That sounds like a lot of missed optimization. Most programs aren't going to be loading from shared libraries.

Maybe I can set this option at work. Though it's scary because I'd have to be certain.

Re:The Performance Impact of C++'s `final` Keyword by Thiez on Tuesday April 23, 2024 @08:17AM (#40131052)
The JVM can actually perform this optimization optimistically and can undo it if the assumption is violated at runtime. So Java's 'everything is virtual by default' approach doesn't hurt. Of course relying an a sufficiently smart JIT comes with its own trade-offs.
Re:The Performance Impact of C++'s `final` Keyword by soooosn on Tuesday April 23, 2024 @06:56AM (#40130440)
I think you have answered your own question: If turning on the setting is scary for you in a very localized project at your company, imagine how scary it would be to turn on by default for everybody :-P
Re:The Performance Impact of C++'s `final` Keyword by JonChesterfield on Tuesday April 23, 2024 @07:17PM (#40138495)
Optimization means "make it faster without changing behaviour in ways I don't like". Clang can't generally default that one to on because it doesn't know whether you're going to splice in more code it can't see at runtime.

Lots of code gets slower if it might need to be called from something not currently in the compiler's scope. That's essentially what ABI overhead is. If there isn't already, there should be a compiler flag that says "this is the whole program, have at it" which implies the vtables option.

Re:The Performance Impact of C++'s `final` Keyword by wiml on Monday April 22, 2024 @02:32PM (#40117297)
If your runtime environment has dynamic linking, then the LTO pass can't always be sure that a subclass won't be introduced later that overrides the method.
Re:The Performance Impact of C++'s `final` Keyword by gpderetta on Monday April 22, 2024 @03:23PM (#40117807)
You can tell the compiler it is indeed compiling the whole program.
Re:The Performance Impact of C++'s `final` Keyword by i80and on Monday April 22, 2024 @02:47PM (#40117460)
Aha! That makes sense. I wasn't thinking of that case. Thanks!
Re:The Performance Impact of C++'s `final` Keyword by adzm on Monday April 22, 2024 @02:40PM (#40117401)
MSVC with LTO and PGO will inline virtual calls in some situations along with a check for the expected vtable, bypassing the inlined code and calling the virtual function normally if it is an unexpected value.
Re:The Performance Impact of C++'s `final` Keyword by samus on Monday April 22, 2024 @03:15PM (#40117736)
This is one of the cases where JIT compiling can shine. You can use a bazillion interfaces to decouple application code, and the JIT will optimize the calls after it found out which implementation is used. This works as long as there is only one or two of them actually active at runtime.
Re:The Performance Impact of C++'s `final` Keyword by account42 on Tuesday April 23, 2024 @09:11AM (#40131575)
You don't need a JIT to do whole program optimization.
Re:The Performance Impact of C++'s `final` Keyword by samus on Tuesday April 23, 2024 @01:15PM (#40134421)
AOT whole program optimization has two limits:

* It is possible with `dlopen()` to load code objects that violate the assumptions made during compilation.

* The presence of runtime configuration mechanisms and application input can make it impossible to anticipate things like the choice of implementations of an interface.

One can always strive to reduce such situations, but it might simply not be necessary if a JIT is present.

Re:The Performance Impact of C++'s `final` Keyword by nickwanninger on Monday April 22, 2024 @02:28PM (#40117242)
At the level that LLVM's LTO operates, no information about classes or objects is left, so LLVM itself can't really devirtualize C++ methods in most cases
Re:The Performance Impact of C++'s `final` Keyword by nwallin on Monday April 22, 2024 @05:32PM (#40121330)
You appear to be correct. Clang does not devirtualize in LTO, but GCC does. Personally I consider this very strange.

     $ cat animal.h cat.cpp main.cpp
    // animal.h
    
    #pragma once
    
    class animal {
     public:
      virtual ~animal() {}
      virtual void speak() = 0;
    };
    
    animal& get_mystery_animal();
    // cat.cpp
    
    #include "animal.h"
    #include <cstdio>
    
    class cat final : public animal {
    public:
      ~cat() override{}
      void speak() override{
        puts("meow");
      }
    };
    
    static cat garfield{};
    
    animal& get_mystery_animal() {
      return garfield;
    }
    // main.cpp
    
    #include "animal.h"
    
    int main() {
      animal& a = get_mystery_animal();
      a.speak();
    }
     $ make clean && CXX=clang++ make -j && objdump --disassemble=main -C lto_test
    rm -f *.o lto_test
    clang++ -c -flto -O3 -g cat.cpp -o cat.o
    clang++ -c -flto -O3 -g main.cpp -o main.o
    clang++ -flto -O3 -g cat.o main.o -o lto_test
    
    lto_test:     file format elf64-x86-64
    
    
    Disassembly of section .init:
    
    Disassembly of section .plt:
    
    Disassembly of section .plt.got:
    
    Disassembly of section .text:
    
    00000000000011b0 <main>:
        11b0: 50                    push   %rax
        11b1: 48 8b 05 58 2e 00 00  mov    0x2e58(%rip),%rax        # 4010 <garfield>
        11b8: 48 8d 3d 51 2e 00 00  lea    0x2e51(%rip),%rdi        # 4010 <garfield>
        11bf: ff 50 10              call   *0x10(%rax)
        11c2: 31 c0                 xor    %eax,%eax
        11c4: 59                    pop    %rcx
        11c5: c3                    ret
    
    Disassembly of section .fini:
     $ make clean && CXX=g++ make -j && objdump --disassemble=main -C lto_test|sed -e 's,^,    ,'
    rm -f *.o lto_test
    g++ -c -flto -O3 -g cat.cpp -o cat.o
    g++ -c -flto -O3 -g main.cpp -o main.o
    g++ -flto -O3 -g cat.o main.o -o lto_test
    
    lto_test:     file format elf64-x86-64
    
    
    Disassembly of section .init:
    
    Disassembly of section .plt:
    
    Disassembly of section .plt.got:
    
    Disassembly of section .text:
    
    0000000000001090 <main>:
        1090: 48 83 ec 08           sub    $0x8,%rsp
        1094: 48 8d 3d 75 2f 00 00  lea    0x2f75(%rip),%rdi        # 4010 <garfield>
        109b: e8 50 01 00 00        call   11f0 <cat::speak()>
        10a0: 31 c0                 xor    %eax,%eax
        10a2: 48 83 c4 08           add    $0x8,%rsp
        10a6: c3                    ret
    
    Disassembly of section .fini:
Re:The Performance Impact of C++'s `final` Keyword by ranger_danger on Monday April 22, 2024 @09:22PM (#40127454)
What if you add -fwhole-program-vtables on clang?
Re:The Performance Impact of C++'s `final` Keyword by JonChesterfield on Tuesday April 23, 2024 @07:20PM (#40138520)
I think this is a bug. There's dedicated metadata that's supposed to end up on the indirect call to list the possible targets and when that list of possible targets is this short it should be turning into a switch over concrete targets. Don't have time to dig into the IR now but it might be worth posting to the github llvm issues.
Re:The Performance Impact of C++'s `final` Keyword by bluGill on Monday April 22, 2024 @02:42PM (#40117423)
not if there is a shared libray or other plugin. Then you coannot determine until runtime if there is an override.
Re:The Performance Impact of C++'s `final` Keyword by chipdart on Tuesday April 23, 2024 @01:57AM (#40128945)
> What final enables is devirtualization in certain cases. The main advantage of devirtualization is that it is necessary for inlining.

I think that enabling inlining is just one of the indirect consequences of devirtualization, and perhaps one that is largely irrelevant for performance improvements.

The whole point of devirtualization is eliminating the need to resort to pointer dereferencing when calling virtual members. The main trait of a virtual class is it's use of a vtable that requires dereferencing virtual members to access each and every one of them.

In classes with larger inheritance chains, you can easily have more than one pointer dereferencing taking place before you call a virtual members function.

Once a class is final, none of that is required anymore. When a member is referred, no dereferencing takes place.

Devirtualization helps performance because you are able to benefit from inheritance and not have to pay a performance penalty for that. Without the final keyword, a performance oriented project would need to be architected to not use inheritance at all, or in the very least in code in the hot path, because that sneaks gratuitous pointer dereferences all over the place, which require running extra operations and has a negative impact on caching.

The whole purpose of the final keyword is that compilers can easily eliminate all pointer dereferencing used by virtual members. What stops them from applying this optimization is that they have no information on whether that class will be inherited and one of its members will either override any of its members or invoke any member function implemented by one of its parent classes.

With the introduction of the final keyword, you are now able to tell the compiler "from thereon, this is exactly what you get" and the compiler can trim out anything loose.

Re:The Performance Impact of C++'s `final` Keyword by simonask on Tuesday April 23, 2024 @02:21AM (#40129061)
An extra indirection (indirect call versus direct call) is practically nothing on modern hardware. Branch predictors are insanely good, and this isn't something you generally have to worry about.

Inlining is by far the most impactful optimization here, because it can eliminate the call altogether, and thus specialize the called function to the callsite, lifting constants, hoisting loop variables, etc.

Re:The Performance Impact of C++'s `final` Keyword by silvestrov on Tuesday April 23, 2024 @04:48AM (#40129864)
"is practically nothing on modern hardware" if the data is already present in the L2 cache. Random RAM access that stalls execution is expensive.

My guess is this is why he didn't see any speedup: all the code could fit inside the L2 cache, so he did not have to pay for RAM access for the deference.

The number of different classes is important, not the number of objects as they have the same small number of vtable pointers.

It might be different for large codebases like Chrome and Firefox.

Re:The Performance Impact of C++'s `final` Keyword by dblohm7 on Tuesday April 23, 2024 @01:25PM (#40134545)
Firefox has done a lot of work on devirtualization over the years. There is a cost.
Re:The Performance Impact of C++'s `final` Keyword by simonask on Wednesday April 24, 2024 @03:31AM (#40141665)
Both the number of objects (dcache) and the number of classes (icache) are significant, as well as the size of both, but yeah. It's pretty rare to have extremely wide class hierarchies, though. You really have to go out of your way to run into significant icache misses.
Re:The Performance Impact of C++'s `final` Keyword by ot1138 on Tuesday April 23, 2024 @09:51AM (#40131957)
I had a section of code which incurred ~20 clock cycles to make a function call to a virtual function in a critical loop. That's over and above potential delays resulting from cache misses and the need to place multiple parameters on the stack.

I was going to eliminate polymorphism altogether for this object but later figured out how to refactor so that this particular call could be called once a millisecond. Then if more work was needed, it would dispatch a task to a dedicated CPU.

This was an incredibly performant improvement which made a significant difference to my P&L.

Re:The Performance Impact of C++'s `final` Keyword by mgaunard on Tuesday April 23, 2024 @04:50PM (#40137095)
Could just be inefficient spilling caused by ABI requirements due to the inability to inline.

In general if you're manipulating values that fit into registers and work on a platform with a shitty ABI,you need to be very careful of what your function call boundaries look like.

The most obvious example is SIMD programming on Windows x86 32-bit.

Re:The Performance Impact of C++'s `final` Keyword by pixelpoet on Tuesday April 23, 2024 @12:45PM (#40133999)
Vfuncs are only fast when they can be predicted: https://forwardscattering.org/post/28
Re:The Performance Impact of C++'s `final` Keyword by mgaunard on Tuesday April 23, 2024 @04:52PM (#40137113)
Same as any other branch. They're fast if predicted correctly and slow if not.

If they cannot be predicted, write your code accordingly.

Re:The Performance Impact of C++'s `final` Keyword by rurban on Tuesday April 23, 2024 @11:18PM (#40140126)
C++ vtables need 2 levels of indirection. See the asm or decompile it with ghidra. First the vtable field, and then the method field.

Of course you have to worry about pointer chasing, when you can easily avoid it. Either via a switch to a single indirection (by passing method pointers around) or inlining with final. Or other compile-time specialization.

Re:The Performance Impact of C++'s `final` Keyword by simonask on Wednesday April 24, 2024 @03:20AM (#40141592)
Show me the receipts. :-)

In general it takes a significant amount of nondeterministic pointer chasing to fool modern branch predictors. Decades of research have been put into optimizing the hardware for languages like C++ and Java, both of which exhibit a lot of pointer chasing.

Re:The Performance Impact of C++'s `final` Keyword by phire on Wednesday April 24, 2024 @03:59PM (#40148864)
Though the branch predictor can chew though both layers of indirection. It can actually start fetching code from the function (and even executing it) before it even reads the function pointer from the vtable.

Though, that assumes a correct prediction. But modern branch predictors are really good, they can track and correctly predict hundreds (if not thousands) of indirect calls, taking into account the history of the last few branches (so it can even get an idea of what class is currently being executed, and make branch predictions based on that). Modern branch predictors do a really good job at chewing up indirect branches in hot sequences of code.

Virtual functions are probably the most harmful for warm code. We are talking about code that's executed too often to be considered cold code, but not often enough to stick around in the branch predictors' cache, executed only a few hundred times a second. It's a death by a thousand cuts type thing. And that's where devirtualisation will help the most...

As long as you don't go too far with the inlining and start causeing icache misses with code bloat. In an ideal would the compiler would inline enough to devirtualise the class, but not necessarily inline the actual function (unless they are small, or only called from one place)

Re:The Performance Impact of C++'s `final` Keyword by account42 on Tuesday April 23, 2024 @07:23AM (#40130592)
> Devirtualization helps performance because you are able to benefit from inheritance and not have to pay a performance penalty for that. Without the final keyword, a performance oriented project would need to be architected to not use inheritance at all, or in the very least in code in the hot path, because that sneaks gratuitous pointer dereferences all over the place, which require running extra operations and has a negative impact on caching.

virtual inheritance. Regular old inheritance does not need or benefit from devirtualization. This is why the CRTP exists.

Re:The Performance Impact of C++'s `final` Keyword by chipdart on Tuesday April 23, 2024 @07:50AM (#40130822)
> This is why the CRTP exists.

CRTP does not exist for that. CRTP was one of the many happy accidents in template metaprogramming that happened to be discovered when doing recursive templates.

Also, you've missed the whole point. CRTP is a way to rearchitect your code to avoid dereferencing pointers to virtual members in inheritance. The whole point is that with final you do not need to pull tricks: just tell the compiler that you don't want the class to be inherited, and the compiler picks up from there and does everything for you.

Re:The Performance Impact of C++'s `final` Keyword by account42 on Tuesday April 23, 2024 @08:35AM (#40131221)
If that's your point then it is simply wrong. Final does not allow the compiler to devirtualize calls through a base pointer, it only eliminates the virtualness for calls through pointers to the (final) derived type. The compiler can devirtualize calls through base pointers in others ways (by deducing the possible derived types via whole program optimization or PGO) but final does not help with that.
Re:The Performance Impact of C++'s `final` Keyword by chipdart on Tuesday April 23, 2024 @10:41AM (#40132530)
> If that's your point then it is simply wrong. Final does not allow the compiler to devirtualize calls through a base pointer, it only eliminates the virtualness for calls through pointers to the (final) derived type.

Please read my post. That's not my claim. I think I was very clear.

Re:The Performance Impact of C++'s `final` Keyword by scaredginger on Tuesday April 23, 2024 @03:11PM (#40135944)
Maybe a nitpick, but virtual inheritance is a term used for something else entirely.

What you're talking about is dynamic dispatch

Re:The Performance Impact of C++'s `final` Keyword by oasisaimlessly on Tuesday April 23, 2024 @03:34PM (#40136221)
> In classes with larger inheritance chains, you can easily have more than one pointer dereferencing taking place before you call a virtual members function.

This is not a thing in C++; vtables are flat, not nested. Function pointers are always 1 dereference away.

Re:The Performance Impact of C++'s `final` Keyword by bdjsiqoocwk on Tuesday April 23, 2024 @05:22AM (#40130080)
Whats devirtualization in C++?

Funny how things work. From working with Julia I've built a good intuition for guessing when functions would be inlined. And yet, I've never heard the word devirtualization until now.

Re:The Performance Impact of C++'s `final` Keyword by saagarjha on Tuesday April 23, 2024 @06:03AM (#40130238)
In C++ virtual functions are polymorphic and indirected, with the target not known to the compiler. Devirtualization gives the compiler this information (in this case a final method cannot be overridden and branch to something else).