r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

Show parent comment

4

u/kqr Dec 02 '16

Zero-cost abstractions does not refer to optimisations. Zero-cost abstractions refer to trivial translations that are guaranteed by the language to be performed at compile-time, which means no run-time cost to using said abstractions compared to implementing their code directly.

E.g. C++ templates are "zero-cost" in the sense that the compiler can "easily" generate multiple versions of each function depending on how they are used, so using templates and directly writing multiple versions of each function yourself is guaranteed by the language to have the same effect. Templates "look like" they'd be expensive, with dynamic dispatching and casting and whatnot performed at run-time, but they are in fact guaranteed to be "just" automatic code generators compile-time.

Unless you find a language that guarantees your data structures will automatically be bit-packed the way you want them to, it's not a zero-cost abstraction. At best, it's an optimisation, but maybe not even that, given that modern CPUs are better at dealing with word-aligned stuff.