4 ms·
"Zero cost abstractions" is just fancy-speak for "exploiting optimization passes" to convert dumb code generated by a template- or macro-system into machine cod
by flohofwoe 6y ago
"Zero cost abstractions" is just fancy-speak for "exploiting optimization passes" to convert dumb code generated by a template- or macro-system into machine code that would be generated from "less dumb code".
You get the same "zero cost abstraction" effect in lower-level languages like C (but you need to feed it "dumb code" to see a similar effect). Nobody talks about "zero cost abstractions" in the context of C programming because C doesn't encourage to "obfuscate" source code with high level abstractions.
Not sure if optimizer passes are the main reason for Rust being slow though, I'd guess it's more the static code analysis, e.g. a C/C++ compiler with static code analysis enabled is also many times slower than regular compilation (but Rust should have an advantage there, because Rust doesn't allow as much freedom as "less correct" languages, so the static analysis can be more focused).
- mlindner 6y agoExcept you can't build a lot of those same abstractions in C, at least not in a way that doesn't involve throwing void pointers everywhere. Also the compiler has no idea what it's looking at so it can't optimize it as well because of pointer aliasing rules. So no, you can't build the same abstractions in C as cheaply as you can in Rust.
- flohofwoe 6y agoThat assumes that "abstractions" are a good thing in the first place though. I haven't seen much evidence for this assumption in real world code so far (in the context of "understanding what this piece of code actually does"). I've mainly been exposed to C++ code though (where "over-abstraction" is quite common), but I also haven't seen much Rust code so far that I would consider particularly "straightforward" and/or "readable". YMMV of course.
- steveklabnik 6y agoRust heavily relies on such things for safety checks, without loss of speed. We could get the same safety with runtime checks, but that would compromise other goals.
- gpderetta 6y agoC is an an abstraction over assembly that is an abstraction over microcode and so on and on. So C programmers must agree that some abstraction is good and it is at least plausible that C is not the best abstraction layer for evry single application.
- mlindner 6y agoIf abstractions were bad ideas we wouldn't have compilers and we'd handcode in assembly. We also wouldn't have the hardware continue to be pretending to be a PDP-8 machine even though it's completely reordering the instructions that it runs and predicting every branch taken. Our world is full of abstractions in basically every single thing we use. Also to be clear, I mean "abstractions" not "obfuscation" which many people (common in Java and C++) call "abstraction" even though it is nothing of the sort.
- AaronFriel 6y agoIt isn't "fancy speak" because those abstractions - language features - are intentionally so as not to have a non-zero cost implementation. It isn't an optimization pass and the programmer doesn't need to worry about a degenerate case causing it to fall through to a less optimized implementation.