10 ms·
> Any C alternative will be expected to be on par with C in performance. The problem is that C have practically no checks, so any safety checks put into the com
by woodruffw 4y ago
> Any C alternative will be expected to be on par with C in performance. The problem is that C have practically no checks, so any safety checks put into the competing language will have a runtime cost, which often is unacceptable. This leads to a strategy of only having checks in "safe" mode. Where the "fast" mode is just as "unsafe" as C.
I don't think this is true, in the general case: Rust has shown that languages can be safe in ways that improve runtime performance.
In particular, languages like Rust allow programmers to express stronger compile-time constraints on runtime behavior, meaning that the compiler can safely omit bounds and other checks that an ordinary C program would require for safety. Similarly, Rust's (lack of) mutable aliasing opens up entire classes of optimizations that are extremely difficult on C programs (to the extent that Rust regularly exposes bugs in LLVM's alias analysis, due to a lack of exercise on C/C++ inputs).
Edit: Other examples include ergonomic static dispatch (Rust makes things like `foo: impl Trait` look dynamic, but they're really static under the hood) and the entire notion of a "zero-cost abstraction" (Rust's abstractions are no worse than their "as if" equivalent, meaning that the programmer is restricted in their ability to create suboptimal implementations).
- chaeronanaut 4y agothis pretty much summarises my opinion - one nitpick - i assume you meant "omit bounds and other checks", not "emit bounds and other checks" which seems to mean the opposite of what you're intending
- woodruffw 4y agoYes, thank you! That's an embarrassing typo! (And thanks to the other person as well, who presumably deleted the same comment after seeing yours.)
- zozbot234 4y agoRust does emit bounds and other checks, though. Optimization passes can usually clear some of them away, but you'd need to check the assembly output to be sure.
- kitd 4y agoYes, that's "omit". "Emit" means "to send out", eg "emit a strange noise", "emit radiation".
- Shish2k 4y agoIt is specifically both: - trying to access an arbitrary element in a slice, the compiler will emit bounds checks (`if index > len: panic()`) to avoid an uncontrolled out-of-bounds memory access — https://godbolt.org/z/cbY5ebzvK https://godbolt.org/z/cbY5ebzvK (note how if you comment out the assert, the code barely changes, because the compiler is adding an invisible assert of its own) - if the compiler can infer that `index` will always be less than `len`, then it will omit the bounds check — https://godbolt.org/z/TTashYnjd https://godbolt.org/z/TTashYnjd
- deleted 4y ago[deleted]
- jiggawatts 4y agoNot to mention that both C++ and Rust can specialise algorithms and containers for specific types, whereas in C most developers resort to void* and function pointers. It's not unusual to see C programs written in a "typical" C style become dramatically faster when rewritten in a more modern language. For example, typical C programs also don't use hashtables even when this makes the most sense, causing weird performance cliffs due to O(n^2) algorithms all over the place. Why not hashtables? Because they're not generic, so they're a pain to use. Not impossible of course, it's just that C developers avoid them. Similarly, "strongly typed" containers full of simple struct types enable compiler auto-vectorisation that's often unavailable in C for the same kind of reason. Last but not least, you would have to be a masochist to write heavily multi-threaded code in C... so hardly anybody does. These days, that's throwing away over 90% of the computer power in a typical PC, let alone a server.
- cmeacham98 4y agoI totally agree with you, but are we really expecting "a typical PC" to have 10+ threads?
- nickysielicki 4y agoIf not today, definitely tomorrow.
- woodruffw 4y agoWhy not? I just opened htop on my desktop, and I have 789 threads running over 117 processes, with 12 logical cores.
- piperswe 4y agoMy Ryzen 5 3600X (a years-old mid-range CPU) has 6 cores and 12 threads, I wouldn't say that's atypical.
- aldanor 4y agoGiven that many phones these days have half a dozen cores...
- 4y ago
- pkrumins 4y ago
- hobofan 4y agoWhy do you keep posting this? One would think by the 10th heavily downvoted comment in the same vein you would have figured out by now that the flaming is neither clever nor appreciated.
- dang 4y agoWould you please stop trolling HN? You've been here for 15 years. You have a distinguished history of writing good articles. You didn't use to post crap comments like this or https://news.ycombinator.com/item?id=32387218 https://news.ycombinator.com/item?id=32387218 and we need you to stop it. If you don't we will have to ban you, which I would hate to do. If you have some substantive critique to make about Rust in appropriate contexts, that's fine of course. But this sort of programming language flamewar is definitely not fine. It leads to lame, dumb threads that we're trying to avoid here. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- cesarb 4y agoMy favorite example of a safety feature in Rust which also improves runtime performance is string slices, which are implemented as a pair of a pointer to the first character and the length (used for bounds checking). Not only does this avoid having to scan for the NUL terminator to find the string length (that is, O(1) instead of O(N), which can make the difference between an O(N) and O(N^2) algorithm), but also it allows taking substrings without copying or modifying the original string (also helped by the borrow checker allowing the programmer to safely omit defensive copies of strings).
- alcover 4y agoMade this C lib for exactly the reasons you mention. I love C but got mad at the constant bounds errors and copying. https://github.com/alcover/buffet https://github.com/alcover/buffet
- pjmlp 4y agoAdditionally, back in the 8 and 16 bit home computer days, C programms were full of inline Assembly, because C compilers generated garbage machine code. It was 40 years of throwing UB optimizations at it, that made its current performance come true. This is nothing special, any language can eventually reach that reality with similar investment.
- mlindner 4y agoThere are in fact ways that idiomatic Rust can be even faster than idiomatic C.
- WalterBright 4y agoC has a huge performance burden from 0 terminated strings. Programs are constantly running strlen() or equivalent to get the length. Length-delineated strings, like what D has, are an order of magnitude faster.
- EdSchouten 4y agoExactly! And because you can't create a substring without mutating the original value, you see that C code often needs to resort to unnecessary copying as well.
- jstimpfle 4y agoNo. You can't create a zero-terminated substring other than a proper suffix or a buffer copy. But that's not really a surprise right? Well then, don't use zero-terminated strings for proper string processing. You don't have to use zero-termination, even when some programmers in the 70s and 80s were convinced enough of it that abominations like strtok() landed in the standard.
- xigoi 4y ago>You don't have to use zero-termination You can choose between zero-termination and having to convert strings back and forth when using idiomatic libraries.
- jstimpfle 4y agoIdiomatic is the wrong word here, because it's certainly not idiomatic to do extra allocations when unneeded. Most APIs let you give the length explicitly if it makes any sense. A not very well-known fact is that even printf format strings let you do printf("%.*s\n", 3, "Hello") which only prints "Hel\n". This is in POSIX C, just not sure when it was standardized.
- jstimpfle 4y ago_There are_ programs that are constantly running strlen(). C strings are the default builtin string representation that has an acceptable tradeoff for performance vs space and simplicity for where they are used: Mostly in string literals, which are expected to be small strings. Mostly for printf() and friends. Zero-terminated strings are space efficient and don't allow bike shedding like length-prefixed strings do. And don't get us started about allocation strategies. "A magnitude faster" for doing what? Typical usages of zero-terminated strings are performance-uncritical. And note that zero-terminated doesn't preclude using separate length fields. Sane programs use store length of strings explicitly where strings get longer and/or performance is a concern, just as it is the case with other types of arrays.
- logicchains 4y agoRust does runtime bounds checks on array access, which the compiler can't elide in non-trivial cases (e.g. binary search), so if you want to write such algorithms as fast as in C, you need to use a lot of unsafe array indexing.
- bitcharmer 4y agoYou might be referring to slices. Arrays in rust have to have a known length at compile time.
- masklinn 4y agoRustc may still need to do runtime bounds check if it can’t conclude that a dynamically computed index is in-bounds. The length is known statically, but whether the index is in-bounds may not be. The binary search GP talks about is exactly one such case, go on Godbolt, write up a simple binary search (with a static size so you get code), and you’ll see that the compiler checks against the literal array size on every iteration.
- bitcharmer 4y agoI have no idea how I totally missed that from the GP's comment. Thanks for correcting me.
- kaba0 4y agoI would be interested in how big of a difference does it make — these branches will always go one way, so they are trivial to branch predict. Other than very slightly increasing the code size (which can have a big effect if the i cache is no longer sufficient), I fail to see it doing too badly as is.
- YetAnotherNick 4y agoBut not always. Some algorithms cannot be written in a way that would remove run time bound checking from rust e.g. KMP.