News for hackers. Stuff that matters.
Re:Why is Rust slightly slower than C? by ncmncm on Wednesday September 11, 2019 @09:21PM (#20946690)
C is the wrong language to compare to. More precisely, Rust should be able to beat C, routinely. Presumably it will, when it gets more optimizer attention. With any luck, that improvement can go into LLVM proper, and speed up many other languages besides.

Why should Rust beat C? First, it does not suffer from the pointer aliasing faults C has. Second, const really means const, where in C the compiler has to assume any pointer-to-const might be use to write through. Because alias analysis is ironclad, copies can be elided even if some function borrows a reference to the copy.

At a larger scale, improved abstraction tools mean more powerful libraries may be written and used, that would be unavailable to the C coder.

It is routine for C++ programs to be substantially faster than a C program that attempts the same job, even though C++ suffers from the same aliasing flaws C has. In the finance world, you would be laughed out of the room for proposing C for a performance-critical task.

This is why articles about other languages comparing them to some fraction of C performance are amusing. C is a low bar. If you can't beat C, you are Doing It Wrong. In Rust's case, that would be "still doing it wrong"; we may assume fixing this is on the schedule, after various "getting it right" and "compiling faster" goals are met.

Re:Why is Rust slightly slower than C? by codesushi42 on Wednesday September 11, 2019 @09:28PM (#20946757)
It is routine for C++ programs to be substantially faster than a C program that attempts the same job, even though C++ suffers from the same aliasing flaws C has. In the finance world, you would be laughed out of the room for proposing C for a performance-critical task.

That depends on how you write your C++ programs. Virtual functions, runtime type information, STL, RAII ownership can all be performance hits in C++ when compared to C.

Rust has optimization issues of its own apparently.

C isn't a low bar. It is the benchmark.

Re:Why is Rust slightly slower than C? by fluffything on Thursday September 12, 2019 @05:19AM (#20949552)
> C isn't a low bar. It is the benchmark.

C isn't the benchmark, assembly is.

C forces you to write code closer to what assembly looks like than Rust or C++. That's good if you can write good assembly, and bad if you cannot. That's particularly bad for writing robust code that doesn't have security vulnerabilities, because the C programmer is required to manually write all checks correctly, while C++ and Rust have abstractions that automatically write the checks for you.

You don't have to use them in those languages if you don't want to.

As I mentioned in the other thread, I think this article is comparing GCC vs LLVM, so the info in the post is kind of moot. But if the authors really believe that Rust bound checks are the culprit of the performance difference, they can just remove them. Takes one line of code, and would show if they are right or wrong.

Re:Why is Rust slightly slower than C? by jeremyjh on Wednesday September 11, 2019 @09:43PM (#20946873)
Is there really a performance cost from RAII? Presumably whatever a destructor has to do to release resources, C code would also have to do.
Re:Why is Rust slightly slower than C? by comex on Wednesday September 11, 2019 @11:15PM (#20947566)
Indeed, RAII is a zero-cost abstraction compared to running the same destructors manually. On the other hand, C++ makes it really easy to write programs that copy and destroy things when it isn't necessary.

For example, if a function takes a `std::string` as an argument (by value), any string you pass in will be copied into a new allocation, which will then have to be deallocated. That's fine if the function really needs its own allocation – but it might not. In that case you can avoid the copy by changing the argument type to `const std::string &` or `std::string_view` (the latter being new in C++17)... but the difference is subtle enough that even an experienced programmer might not notice the extraneous copy.

Don't believe me? Consider that in 2014, "std::string was responsible for almost half of all allocations in the Chrome browser process"! [1]

(Rust does a better job here by requiring more explicitness if you want to make expensive copies.)

Oh, there's also an issue where the presence of a destructor pessimizes the calling convention for passing and returning objects of that type by value, but only slightly, and the issue will be addressed in the future. [2]

[1] https://groups.google.com/a/chromium.org/forum/#!msg/chromiu...

[2] https://quuxplusone.github.io/blog/2018/05/02/trivial-abi-10...

Re:Why is Rust slightly slower than C? by hendzen on Wednesday September 11, 2019 @11:28PM (#20947646)
even if you take the string as `const std::string &` you will still end up with implicit string construction if someone passes a char* . Sure you can use std::string_view as an argument type to prevent this simple case, but it doesn't work in all cases.

Consider a std::map<std::string, Blah> for example, if you have a char* and you want to index in to the map you are going to also end up with an intermediate string construction b/c STL associative containers don't have heterogenous lookup. Note that this was fixed in std::map::find in C++14 [0], but still is there for operator[].

[0] - https://en.cppreference.com/w/cpp/container/map/find

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @12:46AM (#20948087)
This is obsolete and faulty advice.

Given a modern compiler and library, a by-value string temporary can be passed down a chain of calls with no allocations beyond the first, and returned, likewise.

Passing a reference means the optimizer cannot optimize accesses, because it doesn't know what other pointers might be aliasing it. string_view has the same problem.

Quotes about Chromium and Firefox are likewise obsolete. Old code, old coding standards. Neither uses RAII, so they pay a 20-30% runtime penalty. With a modern library, short strings do no allocation. (IIRC Firefox uses 16-bit characters, so they get less.)

That said, if the runtime performance of code trafficking in string objects matters, you are Doing It Wrong.

Re:Why is Rust slightly slower than C? by comex on Thursday September 12, 2019 @01:27AM (#20948305)
> Given a modern compiler and library, a by-value string temporary can be passed down a chain of calls with no allocations beyond the first, and returned, likewise.

For calls (as opposed to returns): If you use std::move, yes. Otherwise, no. But using std::move is similar to changing the type in that it requires noticing the problem first.

> Passing a reference means the optimizer cannot optimize accesses, because it doesn't know what other pointers might be aliasing it. string_view has the same problem.

For `const std::string &`, the optimizer has to assume that the string pointer and length could change, which is indeed a problem as it has to keep reloading them if you call, e.g., `c_str()` or `size()` multiple times (with other things in between). For both `const std::string &` and `std::string_view`, the optimizer has to assume that the string data could change, but not the pointer or length, which is much less of a problem because you don't usually repeatedly load the same piece of string data in a loop. Therefore, `std::string_view` is a decent choice.

Passing `std::string` by value does indeed have the advantage that the compiler indeed could theoretically assume the string data is not aliased. But I just checked and none of Clang, GCC, MSVC, or ICC actually do so:

https://gcc.godbolt.org/z/DZPsuU

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @01:52AM (#20948429)
Again, doing it wrong is Doing It Wrong.

But moving can pessimize string-passing and -returning. Just write the code in the clearest way possible, and optimize hot paths where it turns out to matter, according to measurements. People have been demonstrated to be very poor at picking which those are, a priori.

Re:Why is Rust slightly slower than C? by codesushi42 on Thursday September 12, 2019 @09:28PM (#20958270)
Quotes about Chromium and Firefox are likewise obsolete

Yeah, because neither of those teams had any idea what they were doing, obviously. Even with C++ 11 at their disposal. But somehow, interestingly enough, you do.

Neither uses RAII, so they pay a 20-30% runtime penalty

Who knows where you pulled that number from.

I guess then, according to you at least, there are no performance penalties to be paid for any C++ language features. Please, continue to impart your knowledge and unreferenced benchmark tests on us all.

Re:Why is Rust slightly slower than C? by fluffything on Thursday September 12, 2019 @05:21AM (#20949563)
> C code would also have to do.

Well that's the point: C does not have to do it.

RAII protects you from, e.g., leaking memory. But C can just leak memory, and there isn't really anything substantially wrong with that (you can't get UB from leaking memory).

Checking error values, releasing all resources, doing bound checks, and all the other things that both C++ and Rust do by default are more expensive than doing nothing. Sure, they are zero-cost, in the sense that if you were to write C code that does those, the C code wouldn't be faster. But as mentioned, C code is not required to do these.

With C, you have to manually write the code for doing those things. With C++ and Rust, you have to write the code to opt-out of doing those things (with Rust, removing a bound check is a one liner).

It's a matter of language defaults. I know what the better default for me is.

Re:Why is Rust slightly slower than C? by unrealhoang on Thursday September 12, 2019 @05:39AM (#20949659)
You can write zero ops destructor as well. So yes, RAII is zero-cost abstraction.
Re:Why is Rust slightly slower than C? by fluffything on Thursday September 12, 2019 @05:51AM (#20949712)
I literally said so.
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @09:50AM (#20951410)
You wrote that in C++ and Rust it is necessary to "opt out" of properly freeing memory. That is a false statement. It is not just incidentally false, it is fundamentally false.
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @07:02AM (#20950086)
If your constructor news memory, then if you don't write a destructor, then nothing deletes the memory. You don't need to "opt out". It is hard to guess where you get this idea.

It is bad form to code memory leaks, so normally one doesn't. Instead, one normally allocates in such a way that the automatically-generated destructor frees the memory.

But neither case requires writing more code to "opt in" or to "opt out". It is the same in Rust.

Re:Why is Rust slightly slower than C? by fulafel on Thursday September 12, 2019 @09:55AM (#20951453)
We all know the acronym stands for "Resource Allocation Is Initialization" - and that in the real world the two often have no reason to be semantically coupled. IOW lots of optimization patterns involve decoupling initialization from allocation and the RAII meme encourages programmers to codify the coupling because C++'s shortcomings make it attractive.
Re:Why is Rust slightly slower than C? by verall on Thursday September 12, 2019 @02:34PM (#20954762)
That is what RAII stands for, but in practice good use of C++ RAII is all about explicitly separating interface, allocation, and ownership, such that you only pay for as much initialization as you need when you need it.

This takes great care which takes time and leads to slow development. I think of C++ as worth it when this level of care is necessary to create a well-performing system.

I would agree that most toy examples don't do a good job of explaining how to use RAII in a careful high performing way

Re:Why is Rust slightly slower than C? by bt848 on Thursday September 12, 2019 @12:36AM (#20948037)
In what way can “STL” be considered a performance hit? It looks like you just threw out every C++-related word you know.
Re:Why is Rust slightly slower than C? by verall on Thursday September 12, 2019 @01:05AM (#20948184)
They probably meant, like everyone else who says that STL is a perf hit, "using STL containers is a perf hit"
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @01:25AM (#20948295)
You mean, passing along lies, knowingly or otherwise?
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @12:57AM (#20948139)
I have carefully benchmarked STL code against C-like loops and never got any measurable difference, even through multiple layers of abstraction.

Similarly, virtual function calls on modern CPUs are essentially free if the branch predictor can predict correctly, the usual case.

FUD from the '90s is way past its sell-by date.

Re:Why is Rust slightly slower than C? by pjmlp on Thursday September 12, 2019 @02:36AM (#20948639)
Nowadays, it is ironic given how bad C compilers used to generate code all the way up to the late 90's.
Re:Why is Rust slightly slower than C? by harry8 on Thursday September 12, 2019 @12:13AM (#20947895)
> In the finance world, you would be laughed out of the room for proposing C for a performance-critical task.

This has not been my experience and I'm greatly relieved that I haven't been subjected to such a hostile and destructive working environment in finance as yourself.

The correct response is always: "Show the benchmark numbers and make your case that the trade off is worth it." Then there is discussion on how compelling a case has been made.

Re:Why is Rust slightly slower than C? by Quekid5 on Thursday September 12, 2019 @06:48AM (#20949997)
> The correct response is always: "Show the benchmark numbers and make your case that the trade off is worth it." Then there is discussion on how compelling a case has been made.

Where do you work that allows you to implement everything twice, once in C and once in C++, for a proper comparison to be made?

(I realize that's not quite what you said, but if we're going with 'benchmark', then presumably those places have already done the benchmarks and decided that C++ is generally superior performance-wise.)

Re:Why is Rust slightly slower than C? by harry8 on Thursday September 12, 2019 @07:52PM (#20957713)
As an example, usually there is a default C++ implementation because C++ makes things easier for the programmer so it makes sense if you're at the end of the food chain that requires perfomance (ie you're not using a memory managed language in the first place because that's easier still). So then you benchmark it thorougly. Get stuck into all the perf numbers and analysing those. You note why you think you can shave X off because the latency is due to i, j and k measures that look as though they could be improved so that's probably worth a go. Now you can (at least sometimes) in your lab, replace one part with (say in this example, C code) and see if your perf stat ratios improve the way you think they should. Yes it is difficult. Yes there are plenty of times where you see nothing because the slow from the code you changed is actually manifesting in cache misses elsewhere.

Micro benchmarks sadly have a way of showing nothing much at that end of things. Showing it's no worse/better doesn't even mean it's no worse if the additional/reduced pressure on caches doesn't result in much in the way of additional/reduced cache spills. So you end up doing mirco optimisations that each, taken alone don't do much. But when you get them all together you cross a threshold that has a considerable improvement. I believe SQLite did something like this although probably in a more restrained fashion as they have other goals than being the lowest latency solution against similarly motivated competitors.

Then of course at some point you get a reputation for success so you show the problem and the numbers identifying it and just say "I want to invest X of my time trying Y which I believe has a reasonable chance of helping" And you are told to focus on that and please let them know if you have any more good news. ;-) Sometimes your ideas are genuinely a wash and not worth merging but it is noted that it was worth trying and you're thanked for your efforts.

As somebody else noted in this thread the game has moved beyond much of this kind of thing in some, possibly many instances.

Re:Why is Rust slightly slower than C? by Quekid5 on Friday September 13, 2019 @05:09PM (#20966592)
Are you agreeing or disagreeing?

If you're disagreeing, frankly, almost everything you said sounds like a bunch of platitudes... and I don't understand what you disagree with.

If you're agreeing, frankly, almost everything you said sounds like a bunch of platitudes... and I have no idea what your central point is.

Re:Why is Rust slightly slower than C? by harry8 on Saturday September 14, 2019 @09:26AM (#20970626)
I felt it a reasonable answer, if you think not I probably can't help you. I have no idea what you think I'm aggreeing or disagreeing with. Did you reply to the wrong comment?
Re:Why is Rust slightly slower than C? by hermitdev on Thursday September 12, 2019 @12:29AM (#20947995)
Having spent 15 years in finance, I've never seen C used for perfomance critical code. It's always been C++. Some shops use Java or C# with GC effectively disabled, but those are rare. If you can preallocate all of the memory you need, this may be fine, but not all trading systems have that luxury.
Re:Why is Rust slightly slower than C? by harry8 on Thursday September 12, 2019 @12:55AM (#20948126)
I'm also sorry if your exeprience has been similarly hostile. This is not a comment about which languages get used in which shops.

Whatever your technology choice you would expect to have to make a case, using evidence, and have that case properly listened to (or why did they hire you?). For performance critical technology you would expect that evidence to include benchmark numbers.

Any other approach to considering technology suggestions, involving "laughing out of the room", in the absence of evidence is completely unprofessional, very silly, utterly rude, purile, immature and wholly unacceptable. (And just quietly on that basis alone, I'd be pretty confident my solutions are faster than any put together by a team that ignores evidence and belittles talent. Again I say this is not about one technology vs another, this about "Laughing out of the room." Having said that I've sometimes used C and it has worked really quite well in those particular cases. I can say the same of C++ fwiw).

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @01:23AM (#20948285)
I have not encountered any hostility. But if you don't know how to get better performance from C++ than you can get from C and lots of extra work, you probably will not be hired, most places.

It's not about hostility, it's about competence. Some places just demand competence more than others.

The laughter would not be about measurable performance; you might well be able to get C code to go as fast. The laughter would be over your apparent fear of the C++ compiler, and the suggestion to spend their money on a predictably inferior, more expensive, less maintainable solution.

You might as well suggest installing fluorescent lights in the office, or 10baseT network hubs, or CRT monitors. Those would get a jolly laugh, too, completely without hostility, but you still might not be invited back.

Re:Why is Rust slightly slower than C? by harry8 on Thursday September 12, 2019 @02:48AM (#20948695)
This conversation is now bordering on unhelpful. C has uses, some of them are compelling. Every finance shop I've worked in uses the linux kernel and drivers extensively. These are (mostly? all?) written in C. As is git. Nobody laughs. C++ has uses. Some of them are compelling. For a given case where these uses dominate nobody laughs. Claiming C is as obsolete as CRT monitors or 10baseT without any supporting evidence? Yeah, no thanks. Someone could take that the wrong way. Actually isn't this what Alexandrescou says about C++ because he's all about D nowadays? What Eckel did when he became all about Java (or is he now all scala?). Last time I saw footage of Stanley Lippman speaking he was being brutally disparaging of C++ and invoking in his defence Richard Gabriel describing C as "a virus" to make him look less hostile in comparison to other language warriors and wars. [2]

It just isn't a meaningful conversation without a specific case, with specific requirements and where a specific solution is advanced with appropriate evidence. If in your experience there has never been a case for C, in your own work in the very broad finance domain, over C++. Great. Your experience differs from mine. I've had circumstance where I've made the case, then implemented and released it and watched the p&l go quite well as a result. I do know how to program in C++ where that is appropriate and indeed I do. No really. Also Java, and Python, and (gasp, even) Perl, and... Like any and every experienced programmer, right?

Claiming someone is "scared of the c++" compiler in the absence of evidence for that is probably not your best comment here. It's actually imho the very worst thing about c++ as a language. That is the casual derogatory, frequently unsubstantiated, statements the pervade the surrounding culture. Linus had something to say about it back in the day [1] so it's not like the C++ folk have an exclusive license on that kind of attitude... His tone there doesn't make me laugh.

Two Cases you might like to consider. (1) Optimising the very last cycle matters, how hard you have to work to shake it out, not so much. Maybe you haven't had that, um, pleasure. (2) You have a reason to want to parse and transform the source. (Given (1) above the reason for doing so might be obvious but I probably shouldn't say too much more about it, also you might laugh! ;-)

[1] http://harmful.cat-v.org/software/c++/linus [2] https://www.youtube.com/watch?v=SLLOSAm-OS0

Re:Why is Rust slightly slower than C? by fnord123 on Thursday September 12, 2019 @06:58AM (#20950055)
Maybe in ncmncm's circle they laugh at C developers, but trying to extrapolate that across the finance industry is not credible.

Performance at the sharp end comes from preallocating memory, reducing syscalls, removing PIC (drop the GOT), static linking (enable LTO), using LTO, using simd where possible, hand rolling important functions in assembly, cpusets, reducing network hops, attempting to get the program to fit in L1 cache, putting the code into the switch, moving to fpga, moving to asic.

There's loads more optimizations but almost none of them have anything to do with C++ over C.

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @07:59AM (#20950424)
You do all those things with C++ (or, for fpga, asic, etc., without). There is exactly zero benefit to dropping to C first.

And, nobody suggested anybody actually laughs at C developers. We just don't see them.

Re:Why is Rust slightly slower than C? by fnord123 on Thursday September 12, 2019 @08:23AM (#20950598)
> You do all those things with C++ (or, for fpga, asic, etc., without). There is exactly zero benefit to dropping to C first.

That's my point. The performance tuning is generally not between C++ or C hence why you cannot extrapolate your experiences to the rest of the finance industry.

>And, nobody suggested anybody actually laughs at C developers.

You said, "In the finance world, you would be laughed out of the room for proposing C for a performance-critical task."

Then in discussing the nature of the laughter you said "The laughter would not be about measurable performance; you might well be able to get C code to go as fast. The laughter would be over your apparent fear of the C++ compiler, and the suggestion to spend their money on a predictably inferior, more expensive, less maintainable solution."

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @09:06AM (#20950953)
We have a case in English, as in all languages in its cohort, for discussion of counterfactuals, signaled by "would". If there were anyone proposing C for performance-critical code, they would be laughed at. Happily, none are.
Re:Why is Rust slightly slower than C? by 4D1 on Thursday September 12, 2019 @04:14PM (#20955938)
This conversation is no longer of any benefit, but for the sake of you and your co-workers... Saying that C is predictably inferior, more expensive, and less maintainable shows a blatant amount of incompetence on how either languages operate. C++ only offers abstractions to (debatably) more easily allow for an object oriented design for codebases. Semantically everything that you can do in C++, you can do in C.

There are plenty of features in C++ that are beneficial, particularly the handful of mechanisms that allow you to move away from #defines, but to compare performance between C and C++ simply shows ignorance.

Re:Why is Rust slightly slower than C? by ncmncm on Saturday September 14, 2019 @03:31AM (#20969445)
You say you cannot write a C++ program faster than your C program, and I believe you. I say I can, and you may believe me.
Re:Why is Rust slightly slower than C? by hermitdev on Thursday September 12, 2019 @08:01PM (#20957764)
I didn't say there was hostility towards one tech stack or another, I was just stating my observations/experience. In my experience, once a firm has one or two tech stacks, they don't really want to deviate too far from what's entrenched because, well, they need to have the talent on hand to support everything. This becomes exponentially harder if every developer chooses their language/framework of choice. Generally, though, if there's a compelling reason to break from the core competencies of the firm, exceptions may be granted (this was how Python first got its foothold in my first firm - I could easily, e.g. generate, Python wrappers over my C++ APIs that allowed me to do everything my business users wanted while still maintaining compliance control and audits over data changes).

Typically, you can usually tell the age of the trading firm by the tech stack. Older companies are usually on C++ and have millions upon millions of existing legacy code that works, so there's little impetus to rewrite in the latest shiny. Younger firms are more likely to have embraced Java and younger yet, C#. This doesn't always hold true, though, it's just a generalization of my experience.

In my 15 years in finance, I spent 9 years at my first firm, and when I joined it was purely C++, with some perl to move data around between vendors and systems. There had been some Java, but we had a bad interaction with Sun Microsystems (at the time) and our CEO came down hard and kicked Java to the curb (and we migrated off of Sun/Solaris onto Intel x86 & Linx & C++). Towards the last several years I was at that shop, C#/WPF had been embraced for all new UI work, and even some rewrites of existing MFC apps were in progress. Python had also largely replaced Perl for all new scripting/data munging and had also been embraced by the quants. All of the servers still ran on Linux and were in C++ when I left ~6 years ago. There was even talk of migrating from Sybase on Linux to MS SQL Server. That didn't happen in my time, but from former colleagues I've kept in touch with, they've pushed forward on that. I don't know if they're using SQL Server on Windows or on Linux, now that that's an option.

My current shop is the oldest shop I've ever worked for (founded in the 70s) and is entirely a Windows/C# shop. They brought me on this year to help with their build out of Python at the firm (mostly for our quants). I'm actually really enjoying this position, because they're new to Python, but I've got +15 years experience with it, there's a lot of greenfield development and next to no existing legacy code, so I largely get to drive style and architecture.

Re:Why is Rust slightly slower than C? by fnord123 on Thursday September 12, 2019 @06:38AM (#20949941)
I've also worked in finance and nobody I've known reaches for C++ for performance over C - only convenience/comfort. For performance, one examines the compiled output, analyze the instructions, try to prevent pipeline stalls, etc. And if the compiler is generating dissastisfactory code, use assembly for those parts.
Re:Why is Rust slightly slower than C? by sharpneli on Thursday September 12, 2019 @02:15AM (#20948546)
”even though C++ suffers from the same aliasing flaws C has.”

C no longer has them. Not since a standard that’s 20 years old (C99). restrict qualifier handles that.

Unfortunately C++ committee has not included restrict in C++, but it is available in all major compilers as an extension.

Re:Why is Rust slightly slower than C? by masklinn on Thursday September 12, 2019 @03:05AM (#20948779)
C absolutely has them, you can opt out of aliasing on an individual basis but aliasing flaws are the default.

So much so that rustc regularly has to disable their noalias annotations because its pervasive noaliasing exercises rarely used LLVM paths and leads to miscompilation. Outputting noalias is currently disabled again in IR generation[0][1].

Note from downthread comments in [1] that it's completely possible to create valid C code which miscompiles on both GCC and LLVM (though GCC has been fixed since), it's just unlikely that a regular C dev would write the mix of unrolled loops, inlined functions and restrict annotations which triggers the issue.

[0] https://github.com/rust-lang/rust/issues/54462

[1] https://github.com/rust-lang/rust/issues/54878

Re:Why is Rust slightly slower than C? by Omin on Thursday September 12, 2019 @03:08AM (#20948797)
restrict is so rarely used that optimizations based on non-aliasing data are broken in both major compilers. A fact which has bitten Rust repeatedly.
Re:Why is Rust slightly slower than C? by masklinn on Thursday September 12, 2019 @04:12AM (#20949158)
FWIW the last round was an issue with the interaction between noaliasing and loop unrolling (after inlining) as unrolling would fail to "split" the noalias between unrolled iteration, so it was not the noalias handling which was broken but the loop unrolling pass.

And my understanding's it's since been fixed in GCC.

I don't doubt for a second there will be new miscompilations discovered after that one's fixed though.

Re:Why is Rust slightly slower than C? by tom_mellior on Thursday September 12, 2019 @02:17AM (#20948552)
> It is routine for C++ programs to be substantially faster than a C program that attempts the same job

Can you point us to a concrete benchmark to illustrate this?

Re:Why is Rust slightly slower than C? by xigency on Wednesday September 11, 2019 @11:38PM (#20947701)
Is there an element of runtime correctness and safety as well? Are we comparing programs that have proper bounds checking to ones that don't?
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @02:06AM (#20948490)
One hopes that the optimizer has arranged to check the bounds at the start of the loop, and not in it. Apparently Rust's doesn't, yet, but it seems usually to happen to overlap the checking it does with other stalls, so it might not usually matter much.
Re:Why is Rust slightly slower than C? by kzrdude on Thursday September 12, 2019 @02:18AM (#20948558)
The actual bounds checking instructions are often negligible in terms of overhead, but it can inflict damage by inhibiting other optimizations and loop optimizations, like reduction or vectorization.
Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @02:26AM (#20948599)
Low-hanging fruit. But there are still bigger fish to fry.

Lower-hanging bigger fish?

Rust isn't mature yet. Give it ten years. It will either exceed C performance, or fade away. It's still too early to say which.

Re:Why is Rust slightly slower than C? by kzrdude on Thursday September 12, 2019 @10:59AM (#20952219)
What's the bigger things that need solving in Rust?
Re:Why is Rust slightly slower than C? by ncmncm on Saturday September 14, 2019 @03:32AM (#20969451)
Compile speed. Feature completeness. Tooling maturity.
Re:Why is Rust slightly slower than C? by kzrdude on Wednesday September 18, 2019 @04:24PM (#21009713)
Compile speed is the one I agree on, the others I haven't really run into.
Re:Why is Rust slightly slower than C? by kahlonel on Thursday September 12, 2019 @10:13AM (#20951644)
> it does not suffer from the pointer aliasing faults C has.

There's a `restrict` keyword in C99.

> const really means const, where in C the compiler has to assume any pointer-to-const might be use to write through

Lookup what `const int *const ptr` does in C.

Re:Why is Rust slightly slower than C? by account42 on Friday September 13, 2019 @06:59AM (#20960952)
Look up what (int*)ptr does in C.
Re:Why is Rust slightly slower than C? by freejulian85 on Wednesday September 11, 2019 @09:29PM (#20946765)
I didn’t realize it was possible to be so wrong.
Re:Why is Rust slightly slower than C? by dang on Wednesday September 11, 2019 @11:54PM (#20947790)
Ok, but please don't be rude or post shallow dismissals here. If you know more, it would be great to share some of what you know. Then we can all learn something. Alternatively, it's fine not to post even when someone else is wrong.

https://news.ycombinator.com/newsguidelines.html

Re:Why is Rust slightly slower than C? by monocasa on Wednesday September 11, 2019 @09:53PM (#20946959)
I love C, and have written tons of C++:

What the parent is saying is essentially right. C++'s compile time polymorphism and lamdas can allow faster than C given the right subset of language chosen. For instance std::sort regularly outperforms qsort(3).

Re:Why is Rust slightly slower than C? by otabdeveloper2 on Thursday September 12, 2019 @01:50AM (#20948419)
> given the right subset of language chosen

Yes, if by that you mean "idiomatic C++ as C++ programmers write it".

It's the inexperienced "C/C++" (no such thing, BTW) that are 'choosing subsets', and doing it wrong 90% of the time.

Re:Why is Rust slightly slower than C? by monocasa on Thursday September 12, 2019 @04:06PM (#20955848)
> It's the inexperienced "C/C++" (no such thing, BTW) that are 'choosing subsets'

That's blatantly not true. Strousup himself contributed to the joint strike fighter C++ coding standard which has things like "Allocation/deallocation from/to the free store (heap) shall not occur after initialization." which means that the STL is off the table for example.

Beyond that, there's pieces like RTTI/dynamic_cast that should be avoided for a lot of reasons, perf being one of them.

Re:Why is Rust slightly slower than C? by ncmncm on Friday September 13, 2019 @12:12AM (#20959112)
Most of the STL does no allocation. The containers allocate only at known places, permitted during initialization. They also know how to allocate from provided bespoke stores, which are permitted, in JSF code, even after initialization is done.

So, no, the STL is very much on the table, as in any competently constructed work product.

Re:Why is Rust slightly slower than C? by ncmncm on Thursday September 12, 2019 @02:04AM (#20948478)
There's no substitute for competence, in any language. Fear of the C++ compiler is not a great predictor of competence.