Hacker News new | ask | show | jobs
by ithkuil 1866 days ago
"Zero cost abstraction, for some definition of cost" :-)
1 comments

Well, yes. In that case every abstraction costs because at the very least you need to develop it and it costs.

I think there are two major contributors to cost of abstraction.

#1 is performance. Does it add stuff that is not strictly necessary but is just side effect of abstraction mechanism? Does it affect memory layout to be less than ideal? Does it prevent generated code to be ideal?

#2 is leaks. Leaks are cost. Leak is when the user of abstraction needs to be aware of more details than just the official abstraction. For example, regular expression is a leaky abstraction because, except for simple regexes, you need to know a lot about actual implementation of the regex so that you understand how to write so that it does not require infinite memory and finishes before heat death of the Universe.

Traditionally, "zero-cost" refers to #1 and I think this is very useful definition.

It is difficult to impossible to prevent #2. Using #1 as definition of zero-cost describes tools with which you can use to create your solution without hampering your performance (significantly). These tools do not need to be simple or even intuitive (and Rust is neither).