News for hackers. Stuff that matters.
Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @07:22AM (#21583243)
I may eat downvotes but here's an honest opinion nonetheless: this article shows almost exactly why I wouldn't touch C++ with a ten-foot pole still, after running away screaming from it 11-12 years ago.

Not saying it's an unique problem with C++'s tooling. I'm just saying that it has been my experience that one is much more likely to stumble upon such horrors while working with C++ as opposed to at least 7 other languages I worked with.

I'll take the slower Rust and Go and Elixir, thank you very much.

Don't take this as trash-talking, though. I simply want to get things done, not fight my environment.

Re:Clang Format Tanks Performance by 0x0aff374668 on Wednesday November 20, 2019 @10:44AM (#21584853)
> Don't take this as trash-talking, though. I simply want to get things done, not fight my environment.

I think people don't understand just how badly C++ is broken in reality (vs conceptually) until their paycheck is tied to a real, years-old, multi-developer C++ project. It is not trash talking, it is pointing out truths.

It goes well beyond "any long-lived project has its issues." I think you and I (and many others) know that it is much larger than this, it is a fundamental issue with the language, the standards body surrounding it, and the mess of toolings.

This persists today because C++ is often used to bridge a performance gap between scripted languages and C among younger programmers and academics. (Admit it: we all like that sweet sweet dopamine hit when we take our Python to C++ and the syntax is almost the same.) But that slight performance boost and that "hey this looks like Python but it's really C++" feeling are soon demolished by the accretion of unresolved issues which have enumerated ad-nauseum on HN, including this article.

I've been working with C++ since 1993 (I got a big Borland C++ Compiler as an intern plus 12 VHS tapes of Bjarne himself explaining the language)... my experience with C++ continues right up to today as a contractor having to deal with Arm mbedOS (even Keil struggles to manage hypersensitivity, GNU is a trainwreck for embedded C++) and Tensorflow/OpenCV... and C++ has yet to shake off its flaws. In fact, I think it has added to them exponentially.

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @03:37PM (#21588213)
Yep, I am in full agreement with your points. Especially this:

> It goes well beyond "any long-lived project has its issues."

I heard this mantra as a justification for bad coding practices likely 500 times.

I appreciate that C++ still drives a lot of our virtual world but that's more a proof of the tenacity of those programs' creators than the virtues of C++ itself. IMO it's time tools like `ripgrep`, `dua` / `ds-cli` (both calculate directory contents' sizes) to become the norm: Rust tools, multicore friendly, insanely fast, and with no quirks and weirdly interacting CLI switches.

Re:Clang Format Tanks Performance by daemin on Wednesday November 20, 2019 @08:20AM (#21583631)
If you want to just get things done then this sort of complexity wouldn't impact you in the slightest using C++. You could just write code without needing to know this information. This is something for people really needing to squeeze performance out of systems.

If you are looking for performance though, other languages have similar but different pitfalls and bits of esoteric knowledge that you need. Constructs that you should use or avoid, the layout of data in memory, when to run or prevent running of a GC, etc.

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @08:52AM (#21583858)
True. I am simply looking for a compiled not-far-away-from-C++ levels of performance language, plus one with the least amount of pitfalls / gotchas / WTFs to avoid. So far Rust seems to hit a sweet spot, although its style (procedural) and syntax leave much to be desired.
Re:Clang Format Tanks Performance by electrograv on Wednesday November 20, 2019 @09:30AM (#21584189)
Achieving “not far away from C++ levels of performance” with anything but a procedural-style language is very difficult, rare, and usually comes with severe trade-offs, unfortunately.

For example: Haskell has been trying to bring the performance of C to a functional language for almost 30 years now. While Haskell is a great language with impressive performance for its category, I don’t think anyone believes it renders C/C++ obsolete for scenarios where predictably high performance is crucial.

This is an interesting topic to me, because while I try to find ways to write high-performance code as functionally as possible, I can’t seem to escape the relationship where more procedural-styled code usually yields consistently/predictably higher performance.

Functional (and other paradigms) can sometimes match C’s performance -- but in non-toy scenarios, the “sometimes” clause here compounds its probabilities to ultimately become “virtually never”, as the scope and complexity of a real project grows.

As a result, performance-critical projects written in languages without predictable performance characteristics often evolve into a situation later in development where 90% of your development time is spent poking at 'black boxes' (the compiler optimizer and garbage collector), hoping (sometimes futilely) that you can prod it into spitting out the procedural machine code patterns you already knew you needed anyway — if you’re lucky. And what makes this even worse is that this situation usually arises late enough into development that it's very costly (if not impossible) to backtrack and rewrite everything in a procedural language.

Of course, writing procedurally ends up trading off readability and robustness too human error, vs better performance. I too wish there was a better way to achieve both — I just haven’t found it yet.

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @09:36AM (#21584232)
Insightful. I will be treading a very similar path soon.

So, isn't there a way to emulate FP's `map` mechanics for example, in Rust, without losing performance?

EDIT: To reflect your edits, this is why I want to learn Rust. I am as happy as one can be with Elixir due to Erlang's transparent super-robust and predictable parallel performance; this thing simply does not lag! However, I'd like to be prepared and be able to utilise Rust on the performance hot-spots.

Re:Clang Format Tanks Performance by electrograv on Wednesday November 20, 2019 @10:13AM (#21584560)
Yes, both Rust and Zig are two of the most promising languages in this field IMO. Rust's generics is probably the best example of the 'best of both worlds' (high level abstraction, with predictably high performance), but it's also not without tradeoffs[1].

Rust is more mature, well-known, and akin to C++ in its philosophy (big language with powerful features and abstraction capabilities), while Zig[2] is less well-known but IMO a beautifully designed language akin to C in its emphasis on simplicity, while remaining modern in design (e.g. unlike Google Go, which is very simple but intentionally repeats many of C's mistakes[3]).

[1] Such powerful generics usually come with unavoidable explosions in compile time (and sometimes mental complexity for the coder). This is because while it may look like you're simply calling a library function and passing in a reference to your callback function, what it's really doing most of the time (to achieve good performance) is recompiling the entire library from scratch (or nearly so) with your function inlined. Also, the performance characteristics of any given block of code won’t be as obvious and predictable at-a-glance as it would be in a simpler language.

[2] https://ziglang.org/

[3] https://www.lucidchart.com/techblog/2015/08/31/the-worst-mis...

Re:Clang Format Tanks Performance by username90 on Wednesday November 20, 2019 @09:49AM (#21584351)
The major problem with functional programming is that many idioms requires a garbage collector. For example having pure operations on binary trees by just replacing dirty nodes leaving all old references intact wouldn't be possible in rust.
Re:Clang Format Tanks Performance by electrograv on Wednesday November 20, 2019 @10:24AM (#21584688)
Regarding binary trees: You can always go back to using old-fashioned arrays and indices. A few simple contiguous arrays of nodes (e.g. a key array and value array) is often all you need, where nodes simply refer to each other via int indices into these arrays. You may be surprised that this often yields some of the best-performing data structures, sometimes better than those using raw pointers and non-contiguous heap allocations.

And, this works in Rust just as it does in C. But at this point, you lose out on the benefits of Rust's static type system and borrow-checker:

1. The compiler will no longer be able to correctness-check the validity of these 'int' style references to other memory.

2. If something does go wrong and you read the array with a bad integer index, Rust will just 'panic' and crash the application. Unlike C, there will be no risk of memory errors or related security vulnerabilities. But on the other hand, each read being bounds-checked will make such Rust code slightly slower than is possible with C/C++. (Though I think you can use 'unsafe' blocks if you want to hyper-optimize akin to C.)

But regarding GC langauges, I generally agree; they're almost always more trouble than they're worth in any performance-sensitive context. You often end up using approaches like this to optimize around the GC (e.g. int indexes into pre-allocated arrays) which ultimately means you're coding C-style in a GC language anyway, which defeats the whole point of GC's productivity-enhancing benefit -- at that point, why not just go all the way and use C or Zig or Rust etc.?

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @09:55AM (#21584392)
It isn't entirely impossible. Erlang's processes (actors, not actual OS processes; they are like mini-green threads / fibers) very often just throw away all their data after they are done with their work, and that data is often entirely stored entirely in the stack, without any GC needed -- even if the language is GC-ed. Also, all GC is per actor so there's no stop-the-world GC pauses.

But the scenario you outline, yep, it does seem impossible with a borrow checker.

And don't get me wrong. I am gradually learning Rust and I love it. But I can see myself being a little grumpy with the procedural style and often quirky syntax. Oh well, can't have it all, right?

Re:Clang Format Tanks Performance by devit on Wednesday November 20, 2019 @08:19AM (#21583623)
While Go and Elixir might indeed be slower, Rust is designed to generate optimal code and offer zero-cost abstractions like C and C++ and I would expect it to do so in this case.
Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @08:54AM (#21583868)
That's what I keep hearing but here on HN periodically do pop up some benchmarks that show Rust being stomped.

But oh well, most benchmarks aren't pure or non-opinionated so there's that as well.

So far I like Rust quite a lot but -- as I said in another sibling comment -- its procedural style and syntax aren't stellar, in my eyes at least.

Re:Clang Format Tanks Performance by devit on Wednesday November 20, 2019 @09:22AM (#21584124)
Well, generally you can, approximately, given C/C++ code, produce Rust code that should give equivalent LLVM bitcode to the one produced by clang on the C/C++ code, so it should not be possible for Rust to routinely lose benchmarks to clang with a significant margin given enough effort in benchmark construction.
Re:Clang Format Tanks Performance by username90 on Wednesday November 20, 2019 @11:26AM (#21585276)
Only if you use unsafe Rust, otherwise you'd have to prove that safe Rust semantics are enough to describe the same operations used to squeeze out that performance in C/C++.
Re:Clang Format Tanks Performance by steveklabnik on Wednesday November 20, 2019 @12:49PM (#21586329)
It's more complicated than that. Compilers love restrictions, because that means they can optimize more aggressively. And there are features that Rust doesn't have that C++ does have that can make Rust faster; for example, because Rust doesn't have move constructors, moving is an incredibly cheap operation, whereas it can execute arbitrary code (including throwing an exception!) in C++.

There are also... I don't know how to frame this, social issues? Rust's guarantees often let you do some very aggressive things that you could write in C++, but wouldn't, because it's not maintainable. For example, if you must use reference counting, but don't need threads, Rust has a split between Rc<T> and Arc<T>. You can afford to not use the atomic version in Rust code because if you ever refactor later, you know the compiler will catch this for you, whereas you're just asking for latent, hard to track down bugs if you tried this in C++.

This goes both ways; there is some stuff that C++ is better at than Rust too. It's more complex than "unsafe Rust lets you do anything C++ can do directly and so if it's 100% unsafe in Rust it will be the same speed and if it's safe Rust it will be slower."

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @03:39PM (#21588235)
That's what I am falling in love with as I am learning Rust: both sensical and compiler-helping restrictions.

Rust can use less ways to do the same thing, I keep hearing from people, and I am hopeful it can be tackled in the future with clever deprecations or simply de facto libraries / ways of doing things.

Thanks for your hard work, it's much appreciated.

Re:Clang Format Tanks Performance by gameswithgo on Wednesday November 20, 2019 @09:11AM (#21584021)
since simd intrinsics got introduced there shouldn't be any reason for rust to get 'stomped'
Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @09:19AM (#21584097)
Sorry, I don't mean to be demeaning. I am aware Rust's creators make huge strides and I am absolutely on their side. I already prefer Rust over a lot of languages, C and C++ included.

Just having some healthy skepticism, apologies if it reads like anything else.

Re:Clang Format Tanks Performance by tzhenghao on Thursday November 21, 2019 @09:43PM (#21601972)
I’d agree with you, given the chance to do so in a codebase that has vanilla dependencies. If you work in robotics or do a lot of parallel computing for example, the libraries you use are almost always based on C++, so there’s not an easy way to code against them. Even if there is such a solution, a gap between the languages can lead to non-trivial behaviors and that in itself is more costly to fix than to write in the first class language C++ anyway.
Re:Clang Format Tanks Performance by wffurr on Wednesday November 20, 2019 @08:14AM (#21583582)
Rust is slower than C++?
Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @08:58AM (#21583902)
I am undecided on it since I am not in the C++ ecosystem for a long time now, and I am just a beginner in Rust. I get the message there are ways to write suboptimal (in terms of performance) code in Rust as well but I also keep hearing there are much less ways to shoot oneself in the foot, which works very well for me.

Truthfully, even if Rust can be 2x-5x slower than C++ somewhere, I'll still choose it as my static-and-strongly-typed / compiled-to-machine-code language regardless. Least WTFs per minute is my most important metric.

But I am open to see any and all benchmarks which show both strengths and weaknesses of Rust. So far I sadly haven't seen many of them (but I've been mightily impressed by `actix` in the TechEmpower benchmarks!).

Re:Clang Format Tanks Performance by The_rationalist on Wednesday November 20, 2019 @08:23AM (#21583652)
Yes, at best 20% slower but can be far more slower if you hit worst case scenario.
Re:Clang Format Tanks Performance by Ygg2 on Wednesday November 20, 2019 @08:49AM (#21583829)
Where did you pull those numbers?

Looking at https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

While the worst case is 40% slower (where even C perfored abysmally), most of the time the Rust is within a 10% margin of error, and in quite a few cases it's faster.

Re:Clang Format Tanks Performance by NoZZz on Thursday November 21, 2019 @02:10PM (#21598136)
Strange benchmark, comparing llvm(rust) to gcc(c++ code)
Re:Clang Format Tanks Performance by The_rationalist on Friday November 22, 2019 @03:38AM (#21603890)
As nozzz said, this is using clang. So I should correct my statement, c++ is in average 30% faster as gcc is in average 20% faster than llvm.
Re:Clang Format Tanks Performance by gameswithgo on Wednesday November 20, 2019 @09:13AM (#21584036)
"at best 20%" slower is a clearly non factual statement.

Probably the one scenario where it would be far more slower than that would be a heavily AVX-512 intrinsics program because Rust doesn't have those yet (I think), but then that isn't really either language anymore. its intrinsics.

Re:Clang Format Tanks Performance by The_rationalist on Friday November 22, 2019 @03:40AM (#21603902)
Re-read as 30% slower in average and then it's a factual statement.
Re:Clang Format Tanks Performance by johnzabroski on Wednesday November 20, 2019 @08:40AM (#21583758)
Why is Mozilla Firefox written in Rust, then?

The last time I looked at browser benchmarks was in 2016 when Google released benchmarks to brag about its performance over Firefox and Edge. It appears Webkit has released JetStream2 to benchmark JavaScript, WebAssembly and web workers, among other things. But I can't find data showing Mozilla Firefox is ~20% slower than Chrome/Edge today.

Re:Clang Format Tanks Performance by dthul on Wednesday November 20, 2019 @08:47AM (#21583818)
Using Firefox to evaluate Rust performance is probably not a good idea since only parts of it are written in Rust today.

But it's the first time I hear someone say that Rust is supposedly fundamentally slower than C++. I don't believe this to be a statement that can be substantiated, seeing that some of the fastest programs out there (ripgrep and hyper to name just two) are written in Rust.

Re:Clang Format Tanks Performance by The_rationalist on Friday November 22, 2019 @03:45AM (#21603921)
Facts: On average rust is 10% slower on the banchmarkgame: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

But they use llvm instead of gcc for c++, and gcc is in average 20% faster than clang. Therefore, rust is 30% slower, at least on heavily optimised programs.

And hyper is not the fastest http server, but the actix one is one of the fastest yes.

Re:Clang Format Tanks Performance by steveklabnik on Friday November 22, 2019 @08:58AM (#21605557)
> Facts: On average rust is 10% slower on the banchmarkgame

That does not show it's 10% slower on average; it shows that C++ is 0.9917938291x as fast as Rust. They both have some where they win by 30%, most are closer, Rust is just faster on average.

> But they use llvm instead of gcc for c++,

C++ g++ g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008

?

> gcc is in average 20% faster than clang.

Extremely citation needed.

Re:Clang Format Tanks Performance by igouy on Friday November 22, 2019 @10:40AM (#21606521)
> faster on average

What kind-of time measurements? What kind-of average?

This chart shows some time measurements:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

:and the geometric mean is faster for the C++ programs, but the median seems faster for the rust programs.

Statistical artifacts?

Re:Clang Format Tanks Performance by steveklabnik on Friday November 22, 2019 @10:52AM (#21606659)
> What kind-of time measurements? What kind-of average?

I interpreted the parent to mean a simple average, so I did exactly that: calculated the percentage for each, and averaged them. In the colloquial term, "mean".

Re:Clang Format Tanks Performance by igouy on Wednesday November 27, 2019 @11:46AM (#21649453)
typo ? "the colloquial term" is "average", the specific term is "mean".
Re:Clang Format Tanks Performance by The_rationalist on Friday November 22, 2019 @06:16PM (#21611132)
Geometric mean is the implicit default measurement for benchmarking
Re:Clang Format Tanks Performance by igouy on Friday November 22, 2019 @10:23AM (#21606313)
> On average rust is 10% slower on the banchmarkgame

If that claim was true …

> gcc is in average 20% faster than clang

and if that claim was true …

then we might guess the rust programs average could be 10% faster than the C++ programs average, if both used the same compiler backend

— because those rust programs were compiled with llvm but those c++ programs were compiled with gcc.

Re:Clang Format Tanks Performance by pdimitar on Wednesday November 20, 2019 @09:05AM (#21583957)
IMO the jury is still out. I am pretty sure that C++ veterans that can cleverly utilise any and all performance tricks (minus resorting to an assembly language) and can thus beat any other language.

But I -- like yourself -- am much more interested in the applicability of a language in everyday tools, plus in getting the most performance with the least amount of dark magic involved.

BTW, `ripgrep` definitely made me work better and faster.

Re:Clang Format Tanks Performance by The_rationalist on Friday November 22, 2019 @03:43AM (#21603911)
Firefox is far more than 20% slower than Chrome today but is not strictly a direct cause of using rust. https://www.phoronix.com/scan.php?page=article&item=firefox-...