News for hackers. Stuff that matters.
Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @02:40PM (#40117389)
I don't do much C++, but I have definitely found that engineers will just assert that something is "faster" without any evidence to back that up.

Quick example, I got in an argument with someone a few years ago that claimed in C# that a `switch` was better than an `if(x==1) elseif(x==2)...` because switch was "faster" and rejected my PR. I mentioned that that doesn't appear to be true, we went back and forth until I did a compile-then-decompile of a minimal test with equality-based-ifs, and showed that the compiler actually converts equality-based-ifs to `switch` behind the scenes. The guy accepted my PR after that.

But there's tons of this stuff like this in CS, and I kind of blame professors for a lot of it [1]. A large part of becoming a decent engineer [2] for me was learning to stop trusting what professors taught me in college. Most of what they said was fine, but you can't assume that; what they tell you could be out of date, or simply never correct to begin with, and as far as I can tell you have to always test these things.

It doesn't help that a lot of these "it's faster" arguments are often reductive because they only are faster in extremely minimal tests. Sometimes a microbenchmark will show that something is faster, and there's value in that, but I think it's important that that can also be a small percentage of the total program; compilers are obscenely good at optimizing nowadays, it can be difficult to determine when something will be optimized, and your assertion that something is "faster" might not actually be true in a non-trivial program.

This is why I don't really like doing any kind of major optimizations before the program actually works. I try to keep the program in a reasonable Big-O and I try and minimize network calls cuz of latency, but I don't bother with any kind of micro-optimizations in the first draft. I don't mess with bitwise, I don't concern myself on which version of a particular data structure is a millisecond faster, I don't focus too much on whether I can get away with a smaller sized float, etc. Once I know that the program is correct, then I benchmark to see if any kind of micro-optimizations will actually matter, and often they really don't.

[1] That includes me up to about a year ago.

[2] At least I like to pretend I am.

Re:The Performance Impact of C++'s `final` Keyword by jandrewrogers on Monday April 22, 2024 @03:38PM (#40118212)
A significant part of it is that what engineers believe was effectively true at one time. They simply haven't revisited those beliefs or verified their relevance in a long time. It isn't a terrible heuristic for life in general to assume that what worked ten years ago will work today. The rate at which the equilibriums shift due to changes in hardware and software environments when designing for system performance is so rapid that you need to make a continuous habit of checking that your understanding of how the world works maps to reality.

I've solved a lot of arguments with godbolt and simple performance tests. Some topics are recurring themes among software engineers e.g.:

- compilers are almost always better at micro-optimizations than you are

- disk I/O is almost never a bottleneck in competent designs

- brute-force sequential scans are often optimal algorithms

- memory is best treated as a block device

- vectorization can offer large performance gains

- etc...

No one is immune to this. I am sometimes surprised at the extent to which assumptions are no longer true when I revisit optimization work I did 10+ years ago.

Most performance these days is architectural, so getting the initial design right often has a bigger impact than micro-optimizations and localized Big-O tweaks. You can always go back and tweak algorithms or codegen later but architecture is permanent.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @04:10PM (#40119418)
Yep, completely agree with you on this. Intuition is often wrong, or at least outdated.

When I'm building stuff I try my best to focus on "correctness", and try to come up with an algorithm/design that will encompass all realistic use cases. If I focus on that, it's relatively easy to go back and convert my `decimal` type to a float64, or even convert an if statement into a switch if it's actually faster.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @03:52PM (#40118774)
.NET is a particularly bad case for this because it was a decade of few performance improvements, which caused a certain intuition to develop within the industry, then 6-8 years of significant changes each year (with most wins compressed to the last 4 years or so). Companies moving from .NET Framework 4.6/7/8 to .NET 8 experience a 10x average performance improvement, which naturally comes with rendering a lot of performance know-how obsolete overnight.

(the techniques that used to work were similar to earlier Java versions and overall very dynamic languages with some exceptions, the techniques that still work and now are required today are the same as in C++ or Rust)

Re:The Performance Impact of C++'s `final` Keyword by throwaway2037 on Monday April 22, 2024 @09:23PM (#40127463)
.NET 4.6 to .NET 8 is a 10x "average" performance improvement. I find this hard to believe. In what scenarios? I tried to Google for it and found very little hard evidence.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @09:59PM (#40127673)
In general purpose scenarios, particularly in codebases which have high amount of abstractions, use ASP.NET Core and EF Core, parse and de/serialize text with the use of JSON, Regex and other options, have network and file IO, and are deployed on many-core hosts/container images.

There are a few articles on msft devblogs that cover from-netframework migration to older versions (Core 3.1, 5/6/7):

- https://devblogs.microsoft.com/dotnet/bing-ads-campaign-plat...

- https://devblogs.microsoft.com/dotnet/microsoft-graph-dotnet...

- https://devblogs.microsoft.com/dotnet/the-azure-cosmos-db-jo...

- https://devblogs.microsoft.com/dotnet/one-service-journey-to...

- https://devblogs.microsoft.com/dotnet/microsoft-commerce-dot...

The tl;dr is depending on codebase the latency reduction was anywhere from 2x to 6x, varying per percentile, or the RPS was maintained with CPU usage dropping by ~2-6x.

Now, these are codebases of likely above average quality.

If you consider that moving 6 -> 8 yields another up to 15-30% on average through improved and enabled by default DynamicPGO, and if you also consider that the average codebase is of worse quality than whatever msft has, meaning that DPGO-reliant optimizations scale way better, it is not difficult to see the 10x number.

Keep in mind that while particular regular piece of enterprise code could have improved within bounds of "poor netfx codegen" -> "not far from LLVM with FLTO and PGO", the bottlenecks have changed significantly where previously they could have been in lock contention (within GC or user code), object allocation, object memory copying, e.g. for financial domains - anything including possibly complex Regex queries on imported payment reports (these alone have now difference anywhere between 2 and >1000[0]), and for pretty much every code base also in interface/virtual dispatch for layers upon layers of "clean architecture" solutions.

The vast majority of performance improvements (both compiler+gc and CoreLib+frameworks), which is difficult to think about, given it was 8 years, address the above first and foremost. At my previous employer the migration from NETFX 4.6 to .NET Core 3.1, while also deploying to much more constrained container images compared to beefy Windows Server hosts, reduced latency of most requests by the same factor of >5x (certain request type went from 2s to 350ms). It was my first wow moment when I decided to stay with .NET rather than move over to Go back then (was never a fan of syntax though, and other issues, which subsequently got fixed in .NET, that Go still has, are not tolerable for me).

[0] Cumulative of

https://devblogs.microsoft.com/dotnet/regex-performance-impr...

https://devblogs.microsoft.com/dotnet/regular-expression-imp...

https://devblogs.microsoft.com/dotnet/performance-improvemen...

Re:The Performance Impact of C++'s `final` Keyword by rerdavies on Monday April 22, 2024 @11:57PM (#40128327)
Cheating.

All of the 6x performance improvement cases seem to be related to using the .net based Kestrel web server instead of IIS web server, which requires marshalling and interprocess communication. Several of the 2x gains appear to be related to using a different database backend. Claims that regex performance has improved a thousand-fold.... seem more troubling than cause for celebration. Were you not precompiling your regex's in the older code? That would be a bug.

Somewhere in there, there might be 30% improvements in .net codegen (it's hard to tell). Profile Guided Optimization (PGO) seems to provide a 35% performance improvement over older versions of .net with PGO disabled. But that's dishonest. PGO was around long before .net Core. And claiming that PGO will provide 10x performance because our code is worse than Microsoft's code insults both our code and our intelligence.

Re:The Performance Impact of C++'s `final` Keyword by ygra on Tuesday April 23, 2024 @01:08AM (#40128705)
Not sure about the 10×, either, and if true it would involve more than just the JIT changes. But changing ASP.NET to ASP.NET Core at the same time and the web server as well as other libraries may make it plausible. For certain applications moving from .NET Framework to .NET isn't so simple when they have dependencies and those have changed their API significantly. And in that case most of the newer stuff seems to be built with performance in mind. So you gain 30 % from the JIT, 2× from Kestrel, and so on. Perhaps.

With a Roslyn-based compiler at work I saw 20 % perf improvement just by switching from .NET Core 3.1 to .NET 6. No idea how slow .NET Framework was, though. I probably can't target the code to that anymore.

But for regex even with precompilation, the compiler got a lot better at transforming the regex into an equivalent regex that performs better (automatic atomic grouping to reduce unnecessary backtracking when it's statically known that backtracking won't create more matches for example) and it also benefits a lot from the various vectorized implementations of Index of, etc. Typically with each improvement of one of those core methods for searching stuff in memory there's a corresponding change that uses them in regex.

So where in .NET Framework a regex might walk through a whole string character by character multiple times with backtracking it might be replaced with effectively an EndsWith and LastIndexOfAny call in newer versions.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @02:32AM (#40129123)
Roslyn didn't have much of changes in terms of optimizations - it compiles C# to IL so does very little of that, save for switches and certain new or otherwise features like collection literals. You are probably talking about RyuJIT, also called just JIT nowadays :D

(the distinction becomes important for targets serviced by Mono, so to outline the difference Mono is usually specified, while CoreCLR and RyuJIT may not be, it also doesn't help that JIT, that is, the IL to machine code compiler, also services NativeAOT, so it gets more annoying to be accurate in a conversation without saying the generic ".net compiler", some people refer to it as JIT/ILC)

Re:The Performance Impact of C++'s `final` Keyword by ygra on Tuesday April 23, 2024 @03:49AM (#40129570)
No, I meant that we've written a compiler, based on Roslyn, whose runtime for compiling the code has improved by 20 % when switching to .NET 6.

And indeed, on the C# -> IL side there's little that's being actually optimized. Besides collection literals there's also switch statements/expressions over strings, along with certain pattern matching constructs that get improved on that side.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @03:52AM (#40129585)
Interesting! (I was way off the mark, not reading carefully, ha)

Is it a public project?

Re:The Performance Impact of C++'s `final` Keyword by ygra on Tuesday April 23, 2024 @06:57AM (#40130443)
Nope, completely internal and part of how we offer essentially the same product on multiple platforms with minimal integration work. And existing C# → anything compilers are typically too focused on compiling a whole application instead of offering a library with a stable and usable API on the other end, so we had to roll our own.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @01:35AM (#40128834)
No. DynamicPGO was first introduced in .NET 6 but was not mature and needed two releases worth of work to become enabled by default. It needs no user input and is similar to what OpenJDK Hotspot has been doing for some time and then a little more. It also is required for major features that were strictly not available previously: guarded devirtualization of virtual and interface calls and delegate inlining.

Also, IIS hosting through Http.sys is still an option that sees separate set of improvements, but that's not relevant in most situations given the move to .NET 8 from Framework usually also involves replacing Windows Server host with a Linux container (though it works perfectly fine on Windows as well).

On Regex, compiled and now source generated automata has seen a lot of work in all recent releases, it is night and day to what it was before - just read the articles. Previously linear scans against heavy internal data structures (matching by hashset) and heavy transient allocations got replaced with bloom-filter style SIMD search and other state of the art text search algorithms[0], on a completely opposite end of a performance spectrum.

So when you have compiler improvements multiplied by changes to CoreLib internals multiplied by changes to frameworks built on top - it's achievable with relative ease. .NET Framework, while performing adequately, was still that slow compared to what we got today.

[0] https://github.com/dotnet/runtime/tree/main/src/libraries/Sy...

Re:The Performance Impact of C++'s `final` Keyword by rerdavies on Tuesday April 23, 2024 @03:16PM (#40136001)
Sure. But static PGO was introduced in .Net Framework 4.7.0. And we're talking about apps in production, so there's no excuse NOT to use static PGO on the .net framework 4.7.0 version.

And you have misrepresented the contents of the blogs. The projects discussed in the blogs are typically claiming ~30% improvements (perhaps because they weren't using static PGO in their 4.7.0 incarnation), with two dramatic outliers that seem to be related to migrating from IIS to Kestrel.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @05:26PM (#40137502)
It’s a moot point. Almost no one used static PGO and its feature set was way more limited - it did not have devirtualization which provides the biggest wins. Though you are welcome to disagree it won’t change the reality of the impact .NET 8 release had on real world code.

It’s also convenient to ignore the rest of the content at the links but it seems you’re more interested in proving your argument so the data I provided doesn’t matter.

Re:The Performance Impact of C++'s `final` Keyword by andyayers on Tuesday April 23, 2024 @12:16PM (#40133671)
Something closer to a "pure codegen/runtime" example perhaps: I have data showing Roslyn (the C# compiler, itself written in C#) speeds up between ~2x and ~3x running on .NET 8 vs .NET 4.7.1. Roslyn is built so that it can run either against full framework or core, so it's largely the same application IL.
Re:The Performance Impact of C++'s `final` Keyword by throwaway2037 on Saturday April 27, 2024 @03:43AM (#40178114)

    > Were you not precompiling your regex's in the older code? That would be a bug.
I never heard of this before. Perl has legendary fast regexen and I never heard of this feature. Does Java do it? I don't think so, and the regexes are fast enough in my experience. Can you name a language when regexen are precompiled?
Re:The Performance Impact of C++'s `final` Keyword by wvenable on Monday April 22, 2024 @02:56PM (#40117566)
In my opinion, the only things that really matter are algorithmic complexity and readability. And even algorithmic complexity is usually only an issue a certain scales. Whether or not an 'if' is faster than a 'switch' is the micro of micro optimizations -- you better have a good reason to care. The question I would have for you is was your bunch of ifs more readable than a switch would be.
Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:19PM (#40117773)
Yeah, and it's not like I didn't know how to do the stuff I was doing with a switch, I just don't like switches because I've forgotten to add break statements and had code that appeared correct but actually a month down the line. I've also seen other people make the same mistakes. ifs, in my opinion at least, are a bit harder to screw up, so I will always prefer them.

But I agree, algorithmic complexity is generally the only thing I focus on, and even then it's almost always a case of "will that actually matter?" If I know that `n` is never going to be more than like `10`, I might not bother trying to optimize an O(n^2) operation.

What I feel often gets ignored in these conversations is latency; people obsess over some "optimization" they learned in college a decade ago, and ignore the 200 HTTP or Redis calls being made ten lines below, despite the fact that the latter will have a substantially higher impact on performance.

Re:The Performance Impact of C++'s `final` Keyword by dllthomas on Tuesday April 23, 2024 @12:19PM (#40133706)
> in my opinion at least, are a bit harder to screw up, so I will always prefer them

My experience is the opposite - a sizeable chain of ifs has more that can go wrong precisely because it is more flexible. If I'm looking at a switch, I immediately know, for instance, that none of the tests modifies anything.

Meanwhile, while a missing break can be a brutal error in a language that allows it, it's usually trivial to set up linting to require either an explicit break or a comment indicating fallthrough.

Re:The Performance Impact of C++'s `final` Keyword by doctor_phil on Monday April 22, 2024 @03:01PM (#40117620)
But a switch and an if-else *is* a matter of algorithmic complexity. (Well, at least could be for a naive compiler). A switch could be converted to a constant time jump, but the if-else would be trying each case linearly.
Re:The Performance Impact of C++'s `final` Keyword by bregma on Monday April 22, 2024 @04:13PM (#40119451)
But what if, and stick with me here, a compiler is capable of reading and processing your code and through simple scalar evolution of the conditionals and phi-reduction, it can't tell the difference between a switch statement and a sequence of if statements by the time it finishes its single static analysis phase?

It turns out the algorithmic complexity of a switch statement and the equivalent series of if-statements is identical. The bijective mapping between them is close to the identity function. Does a naive compiler exist that doesn't emit the same instructions for both, at least outside of toy hobby project compilers written by amateurs with no experience?

Re:The Performance Impact of C++'s `final` Keyword by smaudet on Tuesday April 23, 2024 @09:12AM (#40131585)
The issue with if statements (for compiled languages) is not one of "speed" but of correctness.

If statements are unbounded, unconstrained logic constructs, whereas switch statements are type-checkable. The concern about missing break statements here is irrelevant, where your linter/compiler can warn about missing switch cases they can easily warn about non-terminated (non-explicitly marked as fall-through) cases.

For non-compiled languages (so branch prediction is not possible because the code is not even loaded), switch statements also provide a speed-up, i.e. the parser can immediately evaluate the branch to execute vs being forced to evaluate intermediate steps (and the conditions to each if statement can produce side-effects e.g. if(checkAndDo()) { ... } else if (checkAndDoB()) { ... } else if (checkAndDoC()) { ... }

Which, of course, is a potential use of if statements that switches cannot use (although side-effects are usually bad, if you listened to your CS profs)... And again a sort of "static analysis" guarantee that switches can provide that if statements cannot.

Re:The Performance Impact of C++'s `final` Keyword by saurik on Monday April 22, 2024 @03:28PM (#40117855)
While I personally find the if statements harder to immediately mentally parse/grok--as I have to prove to myself that they are all using the same variable and are all chained correctly in a way that is visually obvious for the switch statement--I don't find "but what if we use a naive compiler" at all a useful argument to make as, well, we aren't using a naive compiler, and, if we were, there are a ton of other things we are going to be sad about the performance of leading us down a path of re-implementing a number of other optimizations. The goal of the compiler is to shift computational complexity from runtime to compile time, and figuring out whether the switch table or the comparisons are the right approach seems like a legitimate use case (which maybe we have to sometimes disable, but probably only very rarely).
Re:The Performance Impact of C++'s `final` Keyword by smaudet on Tuesday April 23, 2024 @09:17AM (#40131644)
Per my sibling comment, I think the argument is not about speed, but simplicity.

Awkward switch syntax aside, the switch is simpler to reason about. Fundamentally we should strive to keep our code simple to understand and verify, not worry about compiler optimizations (on the first pass).

Re:The Performance Impact of C++'s `final` Keyword by saurik on Tuesday April 23, 2024 @01:25PM (#40134555)
Right, and there I would say we even agree, per my first sentence; however, I wanted to reply not to you, but to doctor_phil, who was explicitly disagreeing about speed.
Re:The Performance Impact of C++'s `final` Keyword by cogman10 on Monday April 22, 2024 @03:17PM (#40117749)
Yup.

That said, the linear test is often faster due to CPU caches, which is why JITs will often convert switches to if/elses.

IMO, switch is clearer in general and potentially faster (at very least the same speed) so it should be preferred when dealing with 3+ if/elseif statements.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:21PM (#40117786)
Hard disagree that it's "clearer". I have had to deal with a ton of bugs with people trying to be clever with the `break` logic, or forgetting to put `break` in there at all.

if statements are dumber, and maybe arguably uglier, but I feel like they're also more clear, and people don't try and be clever with them.

Re:The Performance Impact of C++'s `final` Keyword by cogman10 on Monday April 22, 2024 @03:31PM (#40117989)
Updates to languages (don't know where C# is on this) have different types of switch statements that eliminate the `break` problem.

For example, with java there's enhanced switch that looks like this

    var val = switch(foo) {
     case 1, 2, 3 -> bar;
     case 4 -> baz;
     default -> {
       yield bat();
     }
    }
The C style switch break stuff is definitely a language mistake.
Re:The Performance Impact of C++'s `final` Keyword by wvenable on Monday April 22, 2024 @03:33PM (#40118072)
C# has both switch expressions like this and also break statements are not optional in traditional switch statements so it actually solves both problems. You can't get too clever with switch statements in C#.

However most languages have pretty permissive switch statements just like C.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:47PM (#40118468)
Yeah, fair, it's been awhile since I've done any C#, so my memory is a bit hazy with the details. I've been burned C with switch statements so I have a pretty strong distaste for them.
Re:The Performance Impact of C++'s `final` Keyword by smaudet on Tuesday April 23, 2024 @09:22AM (#40131685)
I think using C as your language with which to judge language constructs is hardly fair - one of its main strengths has been as a fairly stable, unchanging code-to-compiler contract, i.e. little to none syntax change or improvements.

So no offense, but I would revisit the wider world of language constructs before claiming that switch statements are "all bad". There are plenty of bad languages or languages with poor implementations of syntax, that do not make the fundamental language construct bad.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @03:39PM (#40118218)
C# has switch statements which are C/C++ style switches and switch expressions which are like Rust's match except no control flow statements inside:

    var len = slice switch
    {
        null => 0,
        "Hello" or "World" => 1,
        ['@', ..var tags] => tags.Length,
        ['{', ..var body, '}'] => body.Length,
        _ => slice.Length,
    };
(it supports a lot more patterns but that wouldn't fit)
Re:The Performance Impact of C++'s `final` Keyword by gloryjulio on Monday April 22, 2024 @06:40PM (#40123378)
This is just forcing return value. You either have to break or return at the branches. To me they all look equivalent
Re:The Performance Impact of C++'s `final` Keyword by SAI_Peregrinus on Tuesday April 23, 2024 @12:41PM (#40133941)
I always set -Werror=implicit-fallthrough, among others. That prevents fallthrough unless explicitly annotated. Sadly these will forever remain optional warnings requiring specific compiler flags, since requiring them could break compiling broken legacy code.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @03:44PM (#40118274)
Any sufficiently advanced compiler will rewrite those arbitrarily depending on its heuristics. What authors usually forget is that there is defined behavior and specification which the compiler abides by, but it is otherwise free to produce any codegen that preserves the defined program order. Branch reordering, generating jump tables, optimizing away or coalescing checks into branchless forms are all very common. When someone says "oh I write C because it lets you tell CPU how exactly to execute the code" is simply a sign that a person never actually looked at disassembly and has little to no idea how the tool they use works.
Re:The Performance Impact of C++'s `final` Keyword by cogman10 on Monday April 22, 2024 @03:53PM (#40118787)
A complier will definitely try this, but it's important to note that if/else blocks tell the compiler that "you will run these evaluations in order". Now, if the compiler can detect that the evaluations have no side effects (which, in this simple example with just integer checks, is fairly likely) then yeah I can see a jump table getting shoved in as an optimization.

However, the moment you add a side effect or something more complicated like a method call, it becomes really hard for the complier to know if that sort of optimization is safe to do.

The benefit of the switch statement is that it's already well positioned for the compiler to optimize as it does not have the "you must run these evaluations in order" requirement. It forces you to write code that is fairly compiler friendly.

All that said, probably a waste of time debating :D. Ideally you have profiled your code and the profiler has told you "this is the slow block" before you get to the point of worrying about how to make it faster.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @04:12PM (#40119437)
I agree with what you said but in this particular case, it actually was a direct integer equality check, there was zero risk of hitting side effects and that was plainly obvious to me, the checker, and compiler.
Re:The Performance Impact of C++'s `final` Keyword by cogman10 on Monday April 22, 2024 @04:20PM (#40119664)
And to your original comment, I think the reviewer was wrong to reject the PR over that. Performance has to be measured before you can use it to reject (or create...) a PR. If someone hasn't done that then unless it's something obvious like "You are making a ton of tiny heap allocations in a tight loop" then I think nitpicking these sorts of things is just wrong.
Re:The Performance Impact of C++'s `final` Keyword by adrianN on Tuesday April 23, 2024 @12:03AM (#40128361)
Both the switch and the if have O(1) instructions, so both are the same from an algorithmic complexity perspective.
Re:The Performance Impact of C++'s `final` Keyword by yau8edq12i on Tuesday April 23, 2024 @01:37AM (#40128847)
Unless the number of "else if" statements somehow grows e.g. linearly with the size of your input, which isn't plausible, the "else if" statements also execute in O(1) time.
Re:The Performance Impact of C++'s `final` Keyword by Gazoche on Tuesday April 23, 2024 @04:08AM (#40129650)
It's linear with respect to the number of cases, not the size of inputs. It's still O(1) in the sense of algorithmic complexity.
Re:The Performance Impact of C++'s `final` Keyword by jpc0 on Tuesday April 23, 2024 @04:58AM (#40129931)
... really matter are algorithmic complexity ...

This is not entirely true either... Measure. There are many cases where the optimiser will vectorise a certian algorithm but not another... In many cases On^2 vectorised may be significantly faster than On or Onlogn even for very large datasets depending on your data...

Make your algorithms generic and it won't matter which one you use, if you find that one is slower swap it for the quicker one. Depending on CPU arch and compiler optimisations the fastest algorithm may actually change multiple times in a codebases lifetime even if the usage pattern doesn't change at all.

Re:The Performance Impact of C++'s `final` Keyword by bluGill on Tuesday April 23, 2024 @12:27PM (#40133802)
While you are not wrong, if you have a decent language you will discover all the useful algorithms are already in your standard library and so it isn't a worry. Your code should mostly look like apply this existing algorithm to some new data structure.
Re:The Performance Impact of C++'s `final` Keyword by jpc0 on Tuesday April 23, 2024 @01:37PM (#40134712)
I don't disagree with you at all on this. However you may need to combine several to get to an end result. And if that happens a few times in a codebase, well makes sense to factor that into a library.
Re:The Performance Impact of C++'s `final` Keyword by saghm on Monday April 22, 2024 @02:58PM (#40117587)
> But there's tons of this stuff like this in CS

Reminds me of the classic https://stackoverflow.com/questions/24848359/which-is-faster...

Re:The Performance Impact of C++'s `final` Keyword by sgerenser on Monday April 22, 2024 @03:25PM (#40117823)
Never saw that before, that is indeed a classic.
Re:The Performance Impact of C++'s `final` Keyword by trueismywork on Monday April 22, 2024 @04:45PM (#40120062)
There's not yet a culture of writing reproducible benchmarks to gage these effects.
Re:The Performance Impact of C++'s `final` Keyword by klyrs on Monday April 22, 2024 @04:20PM (#40119643)
> A large part of becoming a decent engineer [2] for me was learning to stop trusting what professors taught me in college

When I was taught about performance, it was all about benchmarking and profiling. I never needed to trust what my professors taught, because they taught me to dig in and find the truth for myself. This was taught alongside the big-O stuff, with several examples where "fast" algorithms are slower on small inputs.

Re:The Performance Impact of C++'s `final` Keyword by TylerE on Monday April 22, 2024 @04:24PM (#40119718)
How do you even get meaningful profiling out of most modern langs? It seems the vast majority of time and calls gets spent inside tiny anonymous functions, GC allocations, and stuff like that.
Re:The Performance Impact of C++'s `final` Keyword by klyrs on Monday April 22, 2024 @05:15PM (#40120880)
I don't use most modern langs! And especially if I'm doing work where performance is critical, I won't kneecap myself by using a language that I can't reasonably profile.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @06:47PM (#40123459)
This is easy in most modern programming languages.

JVM ecosystem has IntelliJ Idea profiler and similar advanced tools (AFAIK).

.NET has VS/Rider/dotnet-trace profilers (they are very detailed) to produce flamegraphs.

Then there are native profilers which can work with any AOT compiled language that produces canonically symbolicated binaries: Rust, C#/F#(AOT mode), Go, Swift, C++, etc.

For example, you can do `samply record ./some_binary`[0] and then explore multi-threaded flamegraph once completed (I use it to profile C#, it's more convenient than dotTrace for preliminary perf work and is usually more than sufficient).

[0] https://github.com/mstange/samply

Re:The Performance Impact of C++'s `final` Keyword by TylerE on Tuesday April 23, 2024 @03:21PM (#40136062)
I mean sure, but I've never seen much in a flamegraph besides noise.
Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Tuesday April 23, 2024 @05:29PM (#40137528)
My experience is complete opposite. You just need to construct a realistic load test for the code and the bottlenecks will stand out (more often than not).

Also there is learning curve to grouping and aggregating data.

Re:The Performance Impact of C++'s `final` Keyword by leetcrew on Monday April 22, 2024 @02:56PM (#40117564)
agreed, especially in cases like this. final is primarily a way to prohibit overriding methods and extending classes, and it indicates to the reader that they should not be doing this. use it when it makes conceptual sense.

that said, c++ is usually a language you use when you care about performance, at least to an extent. it's worth understanding features like nrvo and rewriting functions to allow the compiler to pick the optimization if it doesn't hurt readability too much.

Re:The Performance Impact of C++'s `final` Keyword by jollyllama on Monday April 22, 2024 @03:05PM (#40117660)
I've encountered similar situations before. It's insane to me when people hold up PRs over that kind of thing.
Re:The Performance Impact of C++'s `final` Keyword by BurningFrog on Monday April 22, 2024 @02:56PM (#40117562)
Even if one of these constructs is faster it doesn't matter 99% of the time.

Writing well structured readable code is typically far more important than making it twice as fast. And those times can rarely be predicted beforehand, so you should mostly not worry about it until you see real performance problems.

Re:The Performance Impact of C++'s `final` Keyword by apantel on Monday April 22, 2024 @03:14PM (#40117722)
The counter-argument to this is if you are building something that is in the critical path of an application (for example, parsing HTTP in a web server), you need to be performance-minded from the beginning because design decisions lead to design decisions. If you are building something in the critical path of the application, the best thing to do is build it from the ground up measuring the performance of what you have as you go. This way, each time you add something you will see the performance impact and usually there’s a more performant way of doing something that isn’t more obscure. If you do this as you build, early choices become constraints, but because you chose the most performant thing at every stage, the whole process takes you in the direction of a highly-performant implementation.

Why should you care about performance?

I can give you my personal experience: I’ve been working on a Java web/application server for the past 15 years and a typical request (only reading, not writing to the db) would take maybe 4-5 ms to execute. That includes HTTP request parsing, JSON parsing, session validation, method execution, JSON serialization, and HTTP response dispatch. Over the past 9 months I have refactored the entire application for performance and a typical request now takes about 0.25 ms or 250 microseconds. The computer is doing so much less work to accomplish the same tasks, it’s almost silly how much work it was doing before. And the result is the machine can handle 20x more requests in the same amount of time. If it could handle 200 requests per second per core before, now it can handle 4000. That means the need to scale is felt 20x less intensely, which means less complexity around scaling.

High performance means reduced scaling requirements.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:27PM (#40117844)
But even that sort of depends right? Hardware is often pretty cheap in comparison to dev-time. I really depends on the project, what kind of servers you're using, the nature of the application etc, but I think a lot of the time it might be cheaper to just pay for 20x the servers than it would be to pay a human to go find a critical path.

I'm not saying you completely throw caution to the wind, I'm just saying that there's a finite amount of human resources and it can really vary how you want to allocate them. Sometimes the better path is to just throw money at the problem.

It really depends.

Re:The Performance Impact of C++'s `final` Keyword by apantel on Monday April 22, 2024 @03:57PM (#40118961)
I think it depends on what you’re building and who’s building it. We’re all benefitting from the fact that the designers of NGINX made performance a priority. We like using things that were designed to be performant. We like high-FPS games. We like fast internet.

I personally don’t like the idea of throwing compute at a slow solution. I like when the extra effort has been put into something. The good feeling I get from interacting with something that is optimal or excellent is an end in itself and one of the things I live for.

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @04:05PM (#40119287)
Sure, though I've mentioned a few times in this thread now that the thing that bothers me more than CPU optimizations is not taking into account latency, particularly when hitting the network, and I think focusing on that will generally pay higher dividends than trying to optimize for processing.

CPUs are ridiculously fast now, and compilers are really really good now too. I'm not going to say that processing speed is a "solved" problem, but I am going to say that in a lot of performance-related cases the CPU processing is probably not your problem. I will admit that this kind of pokes holes in my previous response, because introducing more machines into the mix will almost certainly increase latency, but I think it more or less holds depending on context.

But I think it really is a matter of nuance, which you hinted at. If I'm making an admin screen that's going to have like a dozen users max, then a slow, crappy solution is probably fine; the requests will be served fast enough to where no one will notice anyway, and you can probably even get away with the cheapest machine/VM. If I'm making an FPS game that has 100,000 concurrent users, then it almost certainly will be beneficial to squeeze out as much performance out of the machine as possible, both CPU and latency-wise.

But as I keep repeating everywhere, you have to measure. You cannot assume that your intuition is going to be right, particularly at-scale.

Re:The Performance Impact of C++'s `final` Keyword by apantel on Monday April 22, 2024 @04:16PM (#40119491)
I absolutely agree that latency is the real thing to optimize for. In my case, I only leave the application to access the db, and my applications tend not to be write-heavy. So in my case latency-per-request == how much work the computer has to do, which is constrained to one core because the overhead of parallelizing any part of the pipeline is greater than the work required. See, in that sense, we’re already close to the performance ceiling for per-request processing because clock speeds aren’t going up. You can’t make the processing of a given request faster by throwing more hardware at it. You can only make it faster by creating less work for the hardware to do.

(Ironically, HN is buckling under load right now, or some other issue.)

Re:The Performance Impact of C++'s `final` Keyword by oivey on Monday April 22, 2024 @05:06PM (#40120781)
It almost certainly would require more than 20x servers because setting up horizontal scaling will have some sort of overhead. Not only that, there is the significant engineering effort to develop and maintain the code to scale.

If your problem can fit on one server, it can massively reduce engineering and infrastructure costs.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @03:48PM (#40118567)
Please accept a high five from a fellow "it does so little work it must have sub-millisecond request latency" aficionado (though I must admit I'm guilty of abusing memory caches to achieve this).
Re:The Performance Impact of C++'s `final` Keyword by apantel on Monday April 22, 2024 @04:31PM (#40119889)
Caches, precomputed values, lookup tables — it’s all good as long as it’s well-organized and maintainable.
Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:09PM (#40117692)
I mostly focus on "using stuff that won't break", and yeah "if it actually matters".

For example, much to the annoyance of a lot of people, I don't typically use floating point numbers when I start out. I will use the "decimal" or "money" types of the language, or GMP if I'm using C. When I do that, I can be sure that I won't have to worry about any kind of funky overflow issues or bizarre rounding problems. There might be a performance overhead associated with it, but then I have to ask myself "how often is this actually called?"

If the answer is "a billion times" or "once in every iteration of the event loop" or something, then I will probably eventually go back and figure out if I can use a float or convert it to an integer-based thing, but in a lot of cases the answer is "like ten or twenty times", and at that point I'm not even 100% sure it would be even measurable to change to the "faster" implementations.

What annoys me is that people will act like they really care about speed, do all these annoying micro-optimizations, and then forget that pretty much all of them get wiped out immediately upon hitting the network, since the latency associated with that is obscene.

Re:The Performance Impact of C++'s `final` Keyword by neonsunset on Monday April 22, 2024 @03:32PM (#40118001)
This attitude is part of the problem. Another part of the problem is having no idea which things actually end up costing performance and how much.

It is why many language ecosystems suffered from performance issues for a really long time even if completely unwarranted.

Is changing ifs to switch or vice versa, as outlined in the post above, a waste of time? Yes, unless you are writing some encoding algorithm or a parser, it will not matter. The compiler will lower trivial statements to the same codegen and it will not impact the resulting performance anyway even if there was difference given a problem the code was solving.

However, there are things that do cost like interface spam, abusing lambdas writing needlessly complex wokflow-style patterns (which are also less readable and worse in 8 out of 10 instances), not caching objects that always have the same value, etc.

These kinds of issues, for example, plagued .NET ecosystem until more recent culture shift where it started to be cool once again to focus on performance. It wasn't being helped by the notion of "well-structured code" being just idiotic "clean architecture" and "GoF patterns" style dogma applied to smallest applications and simplest of business domains.

(it is also the reason why picking slow languages in general is a really bad idea - everything costs more and you have way less leeway for no productivity win - Ruby and Python, and JS with Node.js are less productive to write in than C#/F#, Kotlin/Java or Go(under some conditions))

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:57PM (#40118962)
I mean, that's kind of why I tried to emphasize measuring things yourself instead of depending on tribal knowledge.

There are plenty of cases where even the "slow" implementation is more than fast enough, and there are also plenty of cases where the "correct" solution (from a big-O or intuition perspective) is actually slower than the dumb case. Intuition helps, you have to measure and/or look at the compiled results if you want to ensure correct numbers.

An example that really annoys me is how every whiteboard interview ends up being "interesting ways to use a hashmap", which isn't inherently an issue, but they will usually be so small-scoped that an iterative "array of pairs" might actually be cheaper than paying the up-front cost of hashing and potentially dealing with collisions. Interviews almost always ignore constant factors, and that's fair enough, but in reality constant factors can matter, and we're training future employees to ignore that.

I'll say it again: as far as I can tell, you have to measure if you want to know if your result is "faster". "Measuring" might involve memory profilers, or dumb timers, or a mixture of both. Gut instincts are often wrong.

Re:The Performance Impact of C++'s `final` Keyword by dosshell on Monday April 22, 2024 @03:19PM (#40117770)
> I can get away with a smaller sized float

When talking about not assuming optimizations...

32bit float is slower than 64bit float on reasonable modern x86-64.

The reason is that 32bit float is emulated by using 64bit.

Of course if you have several floats you need to optimize against cache.

Re:The Performance Impact of C++'s `final` Keyword by jcranmer on Monday April 22, 2024 @04:33PM (#40119928)
Um... no. This is 100% completely and totally wrong.

x86-64 requires the hardware to support SSE2, which has native single-precision and double-precision instructions for floating-point (e.g., scalar multiply is MULSS and MULSD, respectively). Both the single precision and the double precision instructions will take the same time, except for DIVSS/DIVSD, where the 32-bit float version is slightly faster (about 2 cycles latency faster, and reciprocal throughput of 3 versus 5 per Agner's tables).

You might be thinking of x87 floating-point units, where all arithmetic is done internally using 80-bit floating-point types. But all x86 chips in like the last 20 years have had SSE units--which are faster anyways. Even in the days when it was the major floating-point units, it wasn't any slower, since all floating-point operations took the same time independent of format. It might be slower if you insisted that code compilation strictly follow IEEE 754 rules, but the solution everybody did was to not do that and that's why things like Java's strictfp or C's FLT_EVAL_METHOD were born. Even in that case, however, 32-bit floats would likely be faster than 64-bit for the simple fact that 32-bit floats can safely be emulated in 80-bit without fear of double rounding but 64-bit floats cannot.

Re:The Performance Impact of C++'s `final` Keyword by dosshell on Monday April 22, 2024 @05:03PM (#40120639)
I agree with you. It should take the same time when thinking more about it. I remember learning this in ~2016 and I did performance test on Skylake which confirmed (Windows VS2015). I think I remember that i only tested with addsd/addss. Definitely not x87. But as always, if the result can not be reproduced... I stand corrected until then.
Re:The Performance Impact of C++'s `final` Keyword by dosshell on Tuesday April 23, 2024 @05:26AM (#40130105)
I tried to reproduce it on Ivybridge (Windows VS20122) and failed (mulss and muldd) [0]. single and double precision takes the same time. I also found a behavior where the first batch of iterations takes more time regardless of precision. It is possible that this tricked me last time.

[0] https://gist.github.com/dosshell/495680f0f768ae84a106eb054f2...

Sorry for the confusion and spreading false information.

Re:The Performance Impact of C++'s `final` Keyword by sgerenser on Monday April 22, 2024 @03:27PM (#40117840)
I think this is only true if using x87 floating point, which anything computationally intensive is generally avoiding these days in favor of SSE/AVX floats. In the latter case, for a given vector width, the cpu can process twice as many 32 bit floats as 64 bit floats per clock cycle.
Re:The Performance Impact of C++'s `final` Keyword by dosshell on Monday April 22, 2024 @03:35PM (#40118179)
Yes, as I wrote, it is only true for one float value.

SIMD/MIMD will benefit of working on smaller width. This is not only true because they do more work per clock but because memory is slow. Super slow compared to the cpu. Optimization is alot about cache misses optimization.

(But remember that the cache line is 64 bytes, so reading a single value smaller than that will take the same time. So it does not matter in theory when comparing one f32 against one f64)

Re:The Performance Impact of C++'s `final` Keyword by tombert on Monday April 22, 2024 @03:22PM (#40117796)
Sure, I clarified this in a sibling comment, but I kind of meant that I will use the slower "money" or "decimal" types by default. Usually those are more accurate and less error-prone, and then if it actually matters I might go back to a floating point or integer-based solution.
Re:The Performance Impact of C++'s `final` Keyword by zmj on Monday April 22, 2024 @05:27PM (#40121250)
.NET is a little smarter about switch code generation these days: https://github.com/dotnet/roslyn/pull/66081
Re:The Performance Impact of C++'s `final` Keyword by KerrAvon on Monday April 22, 2024 @07:56PM (#40126968)
> `if(x==1) elseif(x==2)...` because switch was "faster" and rejected my PR

Yeah, that's never been true. Old compilers would often compile a switch to __slower__ code because they'd tend to always go to a jump table implementation.

A better reason to use the switch is because it's better style in C-like languages. Using an if statement for that sort of thing looks like Python; it makes the code harder to maintain.

Re:The Performance Impact of C++'s `final` Keyword by wzdd on Tuesday April 23, 2024 @07:12AM (#40130520)
And it's better style because it better conveys intent. An if-else chain in C/C++ implies there's something important about the ordering of cases. Though I'd say that for a very small number of cases it's fine.

(Also, Python has a switch-like construct now.)

Re:The Performance Impact of C++'s `final` Keyword by mynameisnoone on Monday April 22, 2024 @10:18PM (#40127794)
Yep. "Profiling or it didn't happen." The issue is that it's essentially impossible for even the most neckbeard of us to predict with a high degree of accuracy and precision the performance on modern systems impact of change A vs. change B due to the unpredictable nature of the many variables that are difficult to control including compiler optimization passes, architecture gotchas (caches, branch misses), and interplay of quirks on various platforms. Therefore, irreducible and necessary work to profile the differences become the primary viable path to resolving engineering decision points. Hopefully, LLMs now and in the future will be able to help build out boilerplate roughly in the direct of creating such profiling benchmarks and fixtures.

PS: I'm presently revisiting C++14 because it's the most universal statically-compiled language to quickly answer interview problems. It would be unfair to impose Rust, Go, Elixir, or Haskell on an interviewer software engineer.

Re:The Performance Impact of C++'s `final` Keyword by pjmlp on Tuesday April 23, 2024 @03:08AM (#40129351)
I would say it would be safer to go up to C++17, and there are some goodies there, specially for better compile time stuff.
Re:The Performance Impact of C++'s `final` Keyword by ot1138 on Tuesday April 23, 2024 @09:55AM (#40132005)
>I don't do much C++, but I have definitely found that engineers will just assert that something is "faster" without any evidence to back that up.

Very true, though there is one case where one can be highly confident that this is the case: code elimination.

You can't get any faster than not doing something in the first place.

Re:The Performance Impact of C++'s `final` Keyword by konstantinua00 on Tuesday April 23, 2024 @10:02AM (#40132099)
inb4 instruction (cache) alignment screws everythin up