Live data from Hacker News

Why is Rust slightly slower than C?

github.com

191–200 of 251 posts

Re: Why is Rust slightly slower than C?

#191
post #58

Earlier quoted context omitted.

Their initial explanation is bounds checking specifically. While most abstractions are zero-cost, bounds checking isn't. I do wish there were a way to track expectations about integer ranges in a type system, but there currently aren't.

Bounds checking is a “zero cost abstraction” in the sense that it is both “pay for what you use” and “you couldn’t implement it yourself any better.” That said, the implemention is not as fast as it theoretically can be, because the compiler isn’t always removing bounds checks that are provably true. When const generic lands, you will be able to make those assertions! A basic implementation is in nightly.

What exactly does "you couldn't implement it yourself any better" mean if the current implementation isn't as fast as it could be?

Or are you saying that the current implementation fails to live up to the "zero cost abstraction" goal?

Re: Why is Rust slightly slower than C?

#192

Earlier quoted context omitted.

> It doesn't "cheat"; the semantics of the C standard library are just as much a part of the language as the rest. As I read K&R, strcpy, etc., where there, in a library of external functions, as a convenience and optional for the user, and NOT part of the definition of the C language. Your YMMV. The situation definitely is "cheating": In K&R, strcpy is clearly, syntax, semantics, original implementation, an external…

The currently used C standards have no direct relationship with K&R and they define both the language and the standard library functions.

Then they changed the language. C was not supposed to change.

Re: Why is Rust slightly slower than C?

#193

Earlier quoted context omitted.

This is a great comment, but it's in the wrong place. The OP is about using Rust/C to compose network and packet handling software; using Fortran there would be completely pointless. There is no easy gain to be found from vectorization in networking.

I was not bringing up vectorization in Fortran. E.g., in Fortran we can have Real X(20, 30,40), all Call addall(X, 20, 30,40, all) and have Subroutine Addall (Z, L, M, N, Result) Real Z(L, M, N), All, Result Integer L, M, N, I, J, K Sum = 0.0 Do 100 I = 1, L Do 100 J = 1, M Do 100 J = 1, N All = All + Z(I, J, K) 100 Continue So what is good here is that in the subroutine the parameter Z gets its array bounds PASSED f…

> leaving intermediate values in registers and not writing them back to storage in variables known to the source code

C compilers (with optimization on) don't spill intermediates either.

Your decades of experience are appreciated, but apparently your decades of experience don't cover looking at what optimizing C compilers learned to do in the last 30 years or so.

> Bluntly, C doesn't support arrays, and that's a bummer for the programmer

That's true.

> and also execution speed.

That isn't, or at least you haven't demonstrated it.

Re: Why is Rust slightly slower than C?

#194

Earlier quoted context omitted.

I get it now: My view of HN just went down to the sewer: We're into the old, bitter, go out back and fight it out, religious computer language wars. And there C is one of the grand sacred subjects: C is the greatest. The greatest, best ever. The greatest, fastest of all time, never to be improved on. Pure genius. Pure gold. And C programmers are like people who climb Mount Everest barefoot with no tools and the only…

You appear to be making your points in a way in which no developments from the part 10-20 or so years are intruding. Can you point to a extant PL/I compiler that produces good code on modern architectures? How sure are you that the issues that separated C, Fortran and PL/I historically are even remotely relevant now? I would imagine inlined functions (and for the brave, interprocedural analysis) renders many of your…

> You appear to be making your points in a way in which no developments from the part 10-20 or so years are intruding. Can you point to a extant PL/I compiler that produces good code on modern architectures? How sure are you that the issues that separated C, Fortran and PL/I historically are even remotely relevant now?

It's simple. Again, once again, over again, yet again, one more time, the main point is that functionality in a language definition and implemented in a compiler is faster than that functionality implemented in external functions. Simple. Dirt simple. An old point, and still both true and relevant and not changed in the last "10-20" years.

C is not supposed to have changed from K&R -- that it is backwards compatible was one of its main selling points and main reason to put up with such meager functionality that had programmers digging with a teaspoon.

Fortran and PL/I just illustrate the point. The point is thus illustrated. For this illustration, I can go back to old Fortran 66 and PL/I F level version 4. Still the point is illustrated and remains true.

And for the changes in C, does the language yet have definitions and support for multidimensional arrays and something decent in strings? And what about Malloc and Free -- have they been brought up to date?

I did NOT propose, I am NOT proposing, using Fortran or PL/I now. Instead, again, once again, over again, ..., I just made a point about language design and implementation. That this evidence is old is fully appropriate if we regard C as defined in K&R and not changed since. If people changed it, then it's a different language and likely not fully compatible with the past.

So, this thread was about C: I assumed C hasn't changed so assumed K&R C.

I don't want to use C but have, recently in one situation was essentially forced into it. But after that case, I left C. I'm not using Fortran or PL/I, either.

I'm using Microsoft's .NET version of Visual Basic (VB). Right, laughter, please, except it appears that .NET VB and .NET C# differ essentially only in syntactic sugar (there are translations both directions), and I prefer the VB flavor of that sugar as easier to teach, learn, read, and write than C# which borrowed some of the C syntax that K&R asserted was idiosyncratic and in places really obscure.

For this thread, I was surprised that Rust would not run circles around old, standard K&R C. Soooo, the answer is in part that the C of this thread is not old K&R C. Okay, C has changed and is no longer seriously backwards compatible. Okay. Okay to know, but for a huge list of reasons in my work I'm eager to f'get about C and its digging with a teaspoon.

If the new versions of C do not do well handling multidimensional arrays passed as arguments, then C should still be consider TOO SLOW, and too clumsy.

But, whether or not I use C, and whatever I think of it, the point remains: Functionality in the language is faster than that implemented via external functions.

Of course, this point is important in general when considering extensible languages. Sooo, maybe if could do for syntax as much as has been done for semantics, then we could do better on extensible languages. But then we could extend 1000s of ways, maybe once for each application, and then have to worry about the 1000s of cases of documentation, etc. So, extensible, when do it, has pros and cons.

I see: Discussing language design, especially for sacred C, is heresy in the religious language wars. Forbidden topic. Who'd uh thunk -- HN is full of hyper sensitive and hostile religious bigots.

Re: Why is Rust slightly slower than C?

#195
post #145

Earlier quoted context omitted.

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 evidenc…

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…

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?

#196
post #195

Earlier quoted context omitted.

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…

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.

> 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?

#197
post #160

Earlier quoted context omitted.

Bounds checking is a “zero cost abstraction” in the sense that it is both “pay for what you use” and “you couldn’t implement it yourself any better.” That said, the implemention is not as fast as it theoretically can be, because the compiler isn’t always removing bounds checks that are provably true. When const generic lands, you will be able to make those assertions! A basic implementation is in nightly.

This is taking zero-cost abstraction to the extreme, and I think waters down the concept to the point that it almost isn't useful. One can argue that any feature is a zero-cost abstraction for the exact set of trade-offs it has (in the limit: "you couldn't implement a feature that executes this exact sequence of instructions 'mov ...' any better if you did it by hand"). I think focusing on a hypothetical perfect code…

Maybe! I think that the concept is a bit more focused than most people think it is. I can appreciate this perspective though. It's a fuzzy concept, so there's decent room to poke and prod around the edges :)

Re: Why is Rust slightly slower than C?

#198

Earlier quoted context omitted.

Bounds checking is a “zero cost abstraction” in the sense that it is both “pay for what you use” and “you couldn’t implement it yourself any better.” That said, the implemention is not as fast as it theoretically can be, because the compiler isn’t always removing bounds checks that are provably true. When const generic lands, you will be able to make those assertions! A basic implementation is in nightly.

What exactly does "you couldn't implement it yourself any better" mean if the current implementation isn't as fast as it could be? Or are you saying that the current implementation fails to live up to the "zero cost abstraction" goal?

The implementation of bounds checking couldn't be written any better: https://doc.rust-lang.org/1.37.0/src/core/slice/mod.rs.html#...

    #[inline]
    fn get(self, slice: &[T]) -> Option {
        if self 
This is exactly how you'd write the feature, by hand, if you were implementing the language.

That the optimizer could, but does not, do as much optimization as it theoretically can, means that it has more work to do. But that's different than the feature being written in a sub-optimal way.

Re: Why is Rust slightly slower than C?

#199
post #139

Earlier quoted context omitted.

I’m not 100% sure if there’s a source exactly, but we don’t like safety and correctness to depend on what flags you pass or do not pass. We don’t offer a fast-math flag either for similar reasons. The odd one out is overflow, and that’s only because it’s well defined (a “program error”) and not UB to overflow in Rust. This gets checked in debug but not currently release, though the spec allows for it.

What do you think of Julia's macro-based approach? That is, there are `@inbounds` and `@fastmath` macros that turn off bounds checking/enable fast-math flags in the following expression. `@fastmath` works simply by swapping functions (eg `+`) with versions (eg, `Base.FastMath.add_fast`) that have the appropriate llvm flags. When testing Julia libraries, all `@inbounds` are ignored (ie, it'll emit bounds checks anyway…

People have already implemented them, yeah. Dropbox did for some of their code, IIRC.

I think having the speedbump is worth it.

Re: Why is Rust slightly slower than C?

#200
post #110

Earlier quoted context omitted.

RefCell has an internal semaphore. It's used specifically for multi threaded scenarios. If someone is writing a multi threaded C app, they will likely be using semaphores as well. At least, they should be. Rust just enforces it. So, I wouldn't say rust is "slower" in the regard.

Iirc RefCell is marked !Sync, I thought Mutex was the multithreadong analog?

That's correct.
Post reply on HN