News for hackers. Stuff that matters.
Re:Rust is now overall faster than C in benchmarks by dan-robertson on Sunday January 03, 2021 @06:18PM (#25626227)
The reason that C programs often perform well is that it’s so incredibly hard to do anything at all in C (especially something reliable) that one can usually only do the simplest thing possible and this typically means simple data structures, simple algorithms and arrays. In many ways, modern CPUs are particularly designed to run the machine code generated by C compilers on typical C code like this. Pointers and memory allocation are often painful or fickle so use of pointer-heavy data structures like linked lists is quite uncommon.

The reason that C programs often don’t perform as well as an equivalent rust program[1] is that it’s so incredibly hard to do anything at all in C (especially something reliable) that one can usually only do the simplest thing possible and this typically means simple data structures, simple algorithms and arrays

[1] suppose the programs are independently created at the same time by programmers of equal ability and are idiomatic in their languages. If a C program is rewritten in rust, it is likely to be faster, but that would likely also be true if it were rewritten in C.

Re:Rust is now overall faster than C in benchmarks by littlestymaar on Sunday January 03, 2021 @06:46PM (#25626421)
> The reason that C programs often don’t perform as well as an equivalent rust program[1] is that it’s so incredibly hard to do anything at all in C (especially something reliable) that one can usually only do the simplest thing possible and this typically means simple data structures, simple algorithms and arrays

Brian Cantrill talks[1] about exactly this: in his C version he was using AVL trees because they are easy to implement, while his Rust version was using B-trees just because he could. And as a result, his naive first attempt in Rust outperformed his long-optimized C code.

[1]: https://www.youtube.com/watch?v=HgtRAbE1nBM&t=43m15s

Re:Rust is now overall faster than C in benchmarks by jstimpfle on Monday January 04, 2021 @08:35PM (#25640933)
So he's comparing the AVL tree he wrote himself to a B-tree optimized to death by someone else?

Out of interest though, does he mention somewhere which actual implementation he used?

Re:Rust is now overall faster than C in benchmarks by aldonius on Tuesday January 05, 2021 @02:40AM (#25643154)
Yes, and I suppose that's some of the point: a performant library ecosystem is one hell of a feature.
Re:Rust is now overall faster than C in benchmarks by jstimpfle on Tuesday January 05, 2021 @03:45AM (#25643505)
It sure must be a nice feature - for fast results. Counterpoint, though. I barely used Rust, but when I wanted to play with the Xi editor I got to see the bad places that buying into such an ecosystem can get you to - places formerly pioneered by NPM. I had to download 100s of packages, (IIRC?) there were some build problems, all for something where I don't really see the value of noteworthy dependencies.

If you really need a Btree (like, if you want to make a fair benchmark for a presentation) then you'll absolutely find a reasonable implementation in C. After all, why would you need dependency management for something that should have 0 dependencies?

As an example of library data structures implemented in C - if you want, check out my Red-black tree (not a Btree): https://github.com/jstimpfle/rb3ptr/blob/master/rb3ptr.h . It's really easy to integrate into your project. I think the API (found in rb3ptr.h) is totally usable, and it might also be faster than what you can get with safe Rust - unless you can easily use intrinsically linked data structures in safe Rust.

Re:Rust is now overall faster than C in benchmarks by typedef_union on Monday January 04, 2021 @03:45AM (#25628961)
>The reason that C programs often don’t perform as well as an equivalent rust program[1] is that it’s so incredibly hard to do anything at all in C (especially something reliable) that one can usually only do the simplest thing possible and this typically means simple data structures, simple algorithms and arrays

Reliability is very often the name of the game with C, and part of the reason you might see it written in such a simplistic fashion. In embedded systems, we often follow very strict code convention that has strict requirements on how a C program is to be written. This includes everything from how memory is to be allocated, the maximum number of local variables, the maximum number of the arguments, various limits on a function, constraints to handle errors, etc. We do this because it minimizes potential hazards especially when working with limited memory and system resources.

C is an unforgiving language in that mistakes can occur silently and on mission critical hardware these mistakes can cost more than just your project milestones. These programs are very specialized and often have complex algorithms associated with them. Most of the systems I've worked with include various kinds of feedback control systems, and accompanying algorithms. If you're familiar with control systems, you'll know these algorithms are certainly not trivial by any means.

The challenge with C is more as a developer your C code needs to be perfect. Bugs just aren't an option like they are in other environments. Once a specialized piece of hardware ships it needs to work as intended under a myriad of conditions without powering off for the next 30 years (not always the case but more often than you might think). Sometimes this kind of software is going to be put a position it may have never been designed for. The best we can do is try to add various check/support mechanisms both in software and in hardware, and maintain as safe and hazard free software as possible.

In terms of performance, again coming from an embedded environment, there is almost always a spec we are aiming for. It needs to do X tasks in N time for example. Of course high level design questions like whether to poll or wait for interrupt, or how to broker data from shared resources is decided long before you get to the question of how should I, or if I even should, compare two strings. It is nevertheless advised to go with a trivial solution if the ends satisfy the needs.

Is C time-consuming to write? Is C a "hard" language? These are subjective and depend on the nature of the project. I would certainly never use C with only libc to write a webserver that's going to be serving SaaS Co's backend API.

Re:Rust is now overall faster than C in benchmarks by pjmlp on Monday January 04, 2021 @05:31AM (#25629597)
The irony is that most C developers rebelled against Pascal and similar languages for being programming with a straitjacket, and in the end need to comply with MISRA-C and similar security enforcement regulations to ship anything worth using when human lives are at risk.
Re:Rust is now overall faster than C in benchmarks by MaxBarraclough on Monday January 04, 2021 @07:47AM (#25630614)
And even MISRA C isn't enough to assure the absence of undefined behaviour.

C is a tricky beast to tame.

Re:Rust is now overall faster than C in benchmarks by tormeh on Monday January 04, 2021 @07:53AM (#25630664)
Not such a big deal in embedded, where the compiler and hardware is always the same, which means that the behavior is still predictable even if undefined
Re:Rust is now overall faster than C in benchmarks by enedil on Monday January 04, 2021 @09:45AM (#25631918)
This is not true. Undefined behaviour can mean that

``` bool b; if (b) printf("1 "); if (!b) printf("2 "); ```

might print `1 2 `.

Re:Rust is now overall faster than C in benchmarks by MaxBarraclough on Monday January 04, 2021 @11:03AM (#25633042)
Right on. Unless the compiler documentation promises a consistent handling of the various triggers of undefined behaviour, it is under no obligation to always generate code that handles UB consistently.

To my knowledge there is no C compiler that promises to always handle all forms of UB consistently. The closest you could get would probably be something like Valgrind.

Re:Rust is now overall faster than C in benchmarks by pjmlp on Monday January 04, 2021 @03:01PM (#25637049)
At least for this specific case Microsoft and Google are fixing it by always initializing the variables.
Re:Rust is now overall faster than C in benchmarks by greyhair on Monday January 04, 2021 @06:30PM (#25639739)
It's not like that is hard to fix, and modern compilers will flag uninitialized variables.

Use your brain and use your tools. C is not really that hard for a large number of problem sets.

Re:Rust is now overall faster than C in benchmarks by pjmlp on Tuesday January 05, 2021 @02:07AM (#25642951)
Yet, CVE database is full of well known companies that apparently lack such developers.

If their salaries cannot find them, where are they?

Do we have a candidate to prove their secure reports as being wrong?

Re:Rust is now overall faster than C in benchmarks by MaxBarraclough on Tuesday January 05, 2021 @06:39AM (#25644488)
> It's not like that is hard to fix

In an embedded context, such bugs may be extremely costly to fix, if it's even possible.

> modern compilers will flag uninitialized variables

Reading uninitialized variables is one of the more easily prevented forms of undefined behaviour. As pjmlp points out, undefined behaviour in C/C++ programs is one of the major sources of security vulnerabilities in today's software.

Re:Rust is now overall faster than C in benchmarks by blackrock on Sunday January 03, 2021 @07:36PM (#25626735)
Fascinating. I would’ve thought most commercial C programs would have heavily used linked lists, hashes (dictionary), and binary search trees all over the place.

I assume most C++ programs heavily use more of these advanced data structures, correct?

Re:Rust is now overall faster than C in benchmarks by jeltz on Monday January 04, 2021 @02:34AM (#25628639)
Heavy use of linked lists is one reason for why commercial C code often is slow. While there are some exceptions linked lists are slower than arrays for most things. Linked lists are easy to implement in C and therefore used a lot.
Re:Rust is now overall faster than C in benchmarks by wink on Sunday January 03, 2021 @07:44PM (#25626780)
You can't really compare C with a C++ stdlib or boost, or Qt. Well, some organizations surely have their own repo of debugged, tested, and optimized stuff but I guess a lot of them roll everything on their own again and again - and in C++ there is a rich ecosystem, at least for basic data structures.
Re:Rust is now overall faster than C in benchmarks by RcouF1uZ4gsC on Sunday January 03, 2021 @09:28PM (#25627384)
The other thing that C++ and Rust have for collections and algorithms is that they follow a standard pattern.

Because of that, it is usually pretty easy to substitute an optimized algorithm or a container for another one with minimal code changes. In C, because there is no standard for how to do containers and algorithms, it is likely that every library does something a little different in terms of conventions making it harder to swap in implementations.

Re:Rust is now overall faster than C in benchmarks by Skunkleton on Sunday January 03, 2021 @11:59PM (#25628056)
> I would’ve thought most commercial C programs would have heavily used linked lists, hashes (dictionary), and binary search trees all over the place.

Every large scale C code base I have worked on has had these things, and they were used as you say all over the place. If I were to encounter a code base that didn't have these things I would wonder why it was written in C in the first place.

Re:Rust is now overall faster than C in benchmarks by Leherenn on Monday January 04, 2021 @11:56AM (#25634022)
Conversely, I have only seen arrays being used, and never linked lists/hash maps in the few embedded code bases I have worked on.
Re:Rust is now overall faster than C in benchmarks by greyhair on Monday January 04, 2021 @06:32PM (#25639765)
And I have run across, and implemented, both linked lists, and arrays, in embedded systems. The implementation depended on what made the most sense for the particular problem being addressed.
[dead]
Re:Rust is now overall faster than C in benchmarks by latexr on Monday January 04, 2021 @05:03PM (#25638874)
From the guidelines[1]:

> Be kind. Don't be snarky.

> (…)

> When disagreeing, please reply to the argument instead of calling names.

> (…)

> Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something.

You’re being downvoted because your comment doesn’t add to the discussion. You’re (rudely) telling the parent poster they are wrong while providing no basis for the assertion.

[1]: https://news.ycombinator.com/newsguidelines.html