News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by ianlevesque on Friday August 18, 2023 @03:20PM (#37180739)
Thanks for giving an actual example of such optimizations. In my personal experience my C++ (and Rust) code was often outperformed by the JVM's optimizations so I've found it hard to relate to the tradeoffs C++ developers assume are obvious to the rest of us.
Re:C and C++ Prioritize Performance over Correctness by grumpyprole on Friday August 18, 2023 @03:52PM (#37181184)
+1. Part of the problem is that x86 assembly is hardly programming the metal anymore. The performance characteristics of processors has changed over the years also.
Re:C and C++ Prioritize Performance over Correctness by overgard on Friday August 18, 2023 @04:39PM (#37181792)
x86 assembly doesn't represent the actual opcodes the CPU executes anymore, but it's still the low level "API" we have to the CPU. Even if assembly isn't programming to the metal, it's definitely more to the metal than C, and C is more to the metal than Java, etc. Metalness is a gradient
Re:C and C++ Prioritize Performance over Correctness by grumpyprole on Friday August 18, 2023 @04:59PM (#37182024)
Lol, I like the word "metalness".
Re:C and C++ Prioritize Performance over Correctness by SubjectToChange on Friday August 18, 2023 @06:45PM (#37183105)
Metalness is a gradient

It would be better to say that "Metalness" is a sort of "Feature Set". IMO, most programmers would tend to agree that C++ is far closer to Java than C is, yet C++ is every bit as low level as C is. Indeed, even a managed language like C# supports raw pointers and even inline assembly if one is willing to get their hands dirty.

Re:C and C++ Prioritize Performance over Correctness by kaba0 on Saturday August 19, 2023 @02:18AM (#37186080)
There is expressivity and abstraction-ability which are language features, and there is “levelness” or metalness as is it being referred to.

Low and high level languages have two definitions I have seen, one is more objective, but utterly useless: only assemblies are low-level, everything else is high. The other one is more about what can be controlled in a given language’s idiomatic usage, I believe that’s what most people actually refer to intuitively - e.g. C#/Java have ways to manipulate pointers, but you would generally not say that.

In that regard, C, C++, Rust are quite close to the metal with the latter two actually being even closer as they have native control over SIMD datatypes as well, while C doesn’t have that. Note, this is only a partial ordering, as e.g. C#/Java also have control over vector instructions, yet I wouldn’t go around claiming them closer to the metal as C.

Going back to expressivity, C++ is very expressive, while C is very inexpressive, but that is a completely orthogonal axis.

Re:C and C++ Prioritize Performance over Correctness by lbrandy on Friday August 18, 2023 @04:04PM (#37181348)
I struggle to resonate with what you are saying, as my experience is the opposite. I'm curious where this discrepancy is rooted. Reckless hypothesis: are you working on majority latency or majority throughput sensitive systems?

I have seen so, so, so many examples of systems where latencies, including and especially tail latencies, end up mattering substantially and where java becomes a major liability.

In my experience, actually, carefully controlling things like p99 latency is actually the most important reason C++ is preferred rather than the more vaguely specified "performance".

Re:C and C++ Prioritize Performance over Correctness by ianlevesque on Friday August 18, 2023 @04:32PM (#37181703)
The specific example that comes to mind was translating a Java application doing similarity search on a large dataset into fairly naive Rust doing the same. Throughput I guess. It may be possible to optimize Rust to get there but it’s also possible to (and in this case did) end up with less understandable code that runs at 30% the speed.

Edit: And probably for that specific example it’d be best to go all the way to some optimized library like FAISS, so maybe C++ still wins?

Re:C and C++ Prioritize Performance over Correctness by grumpyprole on Friday August 18, 2023 @04:33PM (#37181711)
I've seen C++ systems that are considerably slower than equivalent Java systems, despite the lack of stack allocation and boxing in Java. It's mostly throughput, malloc is slow, the C++ smart pointers cause memory barriers and the heap fragments. Memory management for complex applications is hard and the GC often gives better results.
Re:C and C++ Prioritize Performance over Correctness by vvanders on Friday August 18, 2023 @05:00PM (#37182034)
I've seen so may flat profiles due to shared_ptr. Rust has done a lot of things right but one thing it really did well was putting a decent amount of friction into std::sync::Arc<T>(and offering std::rc::Rc<T> when you don't want atomics!) vs &mut T or even Box<T>. Everyone reaches for shared_ptr when 99% of the time unique_ptr is the correct option.
Re:C and C++ Prioritize Performance over Correctness by memefrog on Friday August 18, 2023 @08:06PM (#37183876)
I see comments like yours more than I see anyone suggesting actually using shared_ptr widely. In my experience, most people (that use smart pointers - many do not) prefer to use unique_ptr where they can.
Re:C and C++ Prioritize Performance over Correctness by vvanders on Friday August 18, 2023 @11:37PM (#37185251)
I don't think anyone is suggesting shared_ptr explicitly, more that if you're coming from Python/Java/etc it's a similar to a GC memory model and a bit more familiar if that's where your experience is. I've observed in a number of codebases unless you're setting a standard of unique_ptr by default it's fairly easy for shared_ptr to become widely used.

FWIW I consider shared_ptr a bit of a code-smell that you don't have your ownership model well understood, there are cases to use it but 90% of the time you can eliminate it with a more explicit design(and avoid chances of leaks + penalties from atomics).

Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @12:07AM (#37185403)
Bear in mind that my ultimate perspective is that you shouldn't use smart pointers (or C++) at all.

But even if you think they have some value, it isn't a flaw in a language if it's not immediately obvious how to write code in it for people that are new to it. If you're coming from Python or Java, then learn how to write C++. There are probably as many books on C++ as on any other language out there.

Re:C and C++ Prioritize Performance over Correctness by Gibbon1 on Friday August 18, 2023 @05:09PM (#37182135)
From my experience with embedded coding you are correct. Most stuff lives and dies enclosed in a single call chain and isn't subject to spooky action at a distance. And stuff that is I often firewall it behind a well tested API.
Re:C and C++ Prioritize Performance over Correctness by deterministic on Monday August 21, 2023 @05:48AM (#37207280)
Badly written C++ will be as slow as well written Java. Absolutely. But there is no way any Java code will perform better than well optimised C++. High performance C++ use custom allocators to completely avoid using malloc (for example).
Re:C and C++ Prioritize Performance over Correctness by overgard on Friday August 18, 2023 @04:37PM (#37181763)
I think the reason to use C/C++ over java has less to do with the various optimizations and more to do with control over memory layout (and thus at least indirect control over cache usage and so on). Plus you remove a lot of "noise" in terms of performance (GC hiccups, JIT translation, VM load time, etc.).
Re:C and C++ Prioritize Performance over Correctness by kllrnohj on Friday August 18, 2023 @04:57PM (#37182007)
It would be fascinating if you could give any such comparison examples. The only time I've seen JVM come anywhere close to C++ in normal usage is if the C++ code was written like it was Java - that is, lots of shared_ptr heap allocations for nearly everything. Or perhaps you're one of the rare few that write Java code like it was C instead? You can definitely get something fast like that, but it seems all too rare.
Re:C and C++ Prioritize Performance over Correctness by deterministic on Monday August 21, 2023 @05:46AM (#37207270)
There is no universe where well written and optimised C++ code will be out-performed by Java code. You might get similar performance in Java if you create zero objects (except during initialisation) and then manually manage the usage of objects at runtime. However only if the matching C++ code is written naively.