Hacker News new | past | comments | ask | show | jobs | submit login

Related: https://old.reddit.com/r/rust/comments/1ha7uyi/memorysafe_pn...

C libraries aren't automatically the fastest option, there's a lot of C code which has stagnated on the performance front but is still widely used because it's battle tested and known to be robust by C standards.




There's still an element of truth in the idea that C is going to be faster by default. There's simply a much lower bar to writing fast (and unsafe) C. Fast Rust demands considerably more thoughtfulness from the programmer (at least for me).


> There's simply a much lower bar to writing fast (and unsafe) C.

That's kind of my point, writing a faster PNG decoder in C may be easier for you but convincing anyone to actually use it instead of the slower but proven safe-ish libpng would be an uphill battle. Trust in C code is extremely hard-won compared to Rust which uses little if any unsafe. The 'png' crate that Chrome is considering to replace libpng has no unsafe whatsoever and is still faster.


> Fast Rust demands considerably more thoughtfulness from the programmer (at least for me).

While fast code requires thoughtfulness regardless of the language, I think rust lets you focus on the fast aspect more because rustc ensures _some_ safety and correctness.

I can write fast and very unsafe C code fast, but I write code that just as fast , but safer in rust faster than in C.


It's entirely likely that you could write faster Rust in the same (nigh-infinite) time it'd take to write equally safe C. I intentionally avoided that comparison though. If you take a normal 10min function in C, it's going to compile into something reasonable and run fast. If you take the same 10min rust function, the language surface area is so much larger that there's a much higher chance that it won't.

Here's a more concrete, albeit irrelevant in practice example from writing most things in both languages:

https://news.ycombinator.com/item?id=42342382#42352053

Implemented in Rust over generic T, you need Wrapping<T> or the equivalent num_traits traits. The implementations for these take borrowed references. Rustc is pretty good at ensuring this becomes pass by value under the hood, but it's imperfect. I found instances of it failing in the test disassembly, even though an implementation for this never has to touch anything but registers. That's performance work that wouldn't have existed in C/C++ for these particular types.


For very simple things C might be accidentally faster.

But C lacks many modern data structures, which are critical for performance on modern hardware and bigger input data.


What do you mean by C lacks many modern data structures?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: