Zero Cost Abstractions

I have been learning Rust on the side for a few months and the buzzword “Zero Cost Abstraction” gets thrown around a lot Rust. But what exactly does the term “Zero Cost Abstraction” mean? Let’s break it down

Abstraction

We in the computer science business love using extremely complicated words to mean super simple things. Unfortunately, abstraction is one of those fancy words that actually means something simple.

An abstraction is something that hides the details of how something works and just exposes an interface for the user to use the thing.

Abstractions are everywhere in the world. In fact, without abstractions, the world would come to a screeching halt as everyone need to be an expert at everything. Let’s take an example to understand abstractions better.

As I am writing this, I am doing a load of dishes. I don’t know how the internals of the dish washing machine work. But I know that if I press the start button, the dish washer starts. Just for a moment think about knowing the answers to the following questions in order to use the dish washer everyday – At how many PSIs should the water be pumped into the machine? How is the soap receptacle plumbed to the washer unit? What should be wattage of the fuse for the heating coil?, etc. You would absolutely lose your mind if you have to know answers to these kind of internal details of things that you use in your day to day life.

The dish washing machine is an example of an abstraction. It hides the details of how exactly it washes and dries your dishes. But it allows a simple button interface for you to get your dish washing done.

Cost of Abstractions

Going back to the dish washing example – The dish washing machine comes with a cost. It requires electricity to operate and electricity costs money. You could definitely avoid paying for that cost by washing and drying your dishes by hand. But if you want to use the dish washing machine abstraction, you have to pay a cost for it.

So, there might be a cost associated with an abstraction that you are using. But, some abstractions cost nothing. Enter “Zero Cost Abstractions”

Zero Cost Abstractions

Zero cost abstractions are abstractions that hides some detail without adding additional costs to the problem you are solving. A fantastic example of a zero cost abstraction are TypeScript interfaces

interface MyInterface {
  myMethod(): void;
}

If you actually run this code on TypeScript playground, you will notice that notice that TypeScript compiler produces no output code – https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgLIE8CS5rycgbwChlTkBbdVCMACwHsATACgEoAuZAN3uEYG4iAXyA

Why you might ask? TypeScript interfaces are a compile time construct. They are used by the compiler for type-checking. But after the compiler is done compiling the TypeScript code, it has no need for data from interfaces anymore. So, it simply gets rid of the interfaces and thereby it avoids adding any cost to the output JavaScript. That’s why it is a zero cost abstraction.

Hopefully, this helps with understanding the buzzword “Zero Cost Abstractions”. If you know of other examples of zero cost abstractions, please leave a comment and let me know.

Leave a comment