3 ms·
There is no such thing as a zero-cost abstraction. While it would probably be incorrect to claim zero cost across the board, there are abstractions that can be
by tspiteri 10y ago
There is no such thing as a zero-cost abstraction.
While it would probably be incorrect to claim zero cost across the board, there are abstractions that can be optimized to zero cost. For example, in Rust you don't use pointers that can be NULL, but the Option type which can be None. In the usual case, the optimizer translates this to the same assembly as using a raw pointer with NULL instead of None. So in the usual case, there is such a thing as zero-cost abstraction.
- qwertyuiop924 10y agoBut the way that you use Option might differ from the way you use NULL, resulting in costs in the implementation that may not exist otherwise. This is unlikely for that exact case, but very common with other abstractions.
- tspiteri 10y agoThis is exactly why I was careful enough to include the phrase "in the usual case", because it would not be too hard to doctor a situation where the compiler will not optimize the Option away, either because the compiler is not perfect, or because of some clever juggling, or for some other reason. But in the usual case, you get zero-cost abstraction.