KC S
KC S

Reputation: 4915

What does 'Zero Cost Abstraction' mean?

I came across this term when exploring Rust.

I saw different kinds of explanations regarding this and still don't quite get the ideas.

In The Embedded Rust Book, it said

Type states are also an excellent example of Zero Cost Abstractions

  • the ability to move certain behaviors to compile time execution or analysis.

These type states contain no actual data, and are instead used as markers.

Since they contain no data, they have no actual representation in memory at runtime:

Does it mean the runtime is faster because there is no memory in runtime?

Appreciate it if anyone can explain it in an easy to understand way.

Upvotes: 53

Views: 29300

Answers (4)

Jared Smith
Jared Smith

Reputation: 21926

Zero cost abstractions are ones that bear no runtime costs in execution speed or memory usage.

By contrast, virtual methods are a good example of a costly abstraction: in many OO languages the type of the method's caller is determined at runtime which requires maintaining a lookup table (runtime memory usage) and then actually performing the lookup (runtime overhead per method call, likely at least an extra pointer dereference) with the runtime type to determine which version of the method to call. Another good example would be garbage collection: in return for being able to not worry about the details of memory allocation you pay with GC pauses.

Rust though mostly tries to have zero cost abstractions: ones that let you have your cake and eat it too. Ones that compiler can safely and correctly convert to forms that bear no extra indirection/memory usage. In fact, the only thing that I'm aware of (somebody more knowledgeable correct me if I'm wrong) that you really pay for at runtime in Rust is bounds checking and dyn (see the bit about virtual methods above).

Upvotes: 21

Juliano Silva
Juliano Silva

Reputation: 286

Zero-cost abstraction refers to the concept of using abstractions in code that are both expressive and efficient, without incurring any additional runtime overhead. This concept is particularly associated with the C++ programming language, which was one of the first languages to pioneer metaprogramming, generic programming, and object-oriented programming, all of which can be used to achieve zero-cost abstractions.

On the other hand we have functional programming, while it can also make use of abstractions, is not typically associated with zero-cost abstraction, as its emphasis on immutability can require additional memory allocation and copy-on-write operations (following gust comment), which can impact performance in certain use cases, such as real-time game programming or matrix computing.

Upvotes: -7

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83758

Zero Cost Abstractions means adding higher-level programming concepts, like generics, collections and so on do not come with a run-time cost, only compile time cost (the code will be slower to compile). Any operation on zero-cost abstractions is as fast as you would write out matching functionality by hand using lower-level programming concepts like for loops, counters, ifs and using raw pointers.

Or another way to view this is that using zero-cost abstraction tools, functions, templates, classes and such come with "zero cost" for the performance of your code.

Upvotes: 98

raianmr
raianmr

Reputation: 147

The concept of zero cost abstractions originally came from the functional world. However, the terminology comes from C++. According to Bjarne Stroustrup,

In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better.

This quote along with most answers fail to deliver the idea in its entirety because the context in which these were said isn't explicitly stated.

If there was only one programming language in the world: be it Rust or C++, zero-cost abstractions would be indistinguishable from most other compiler optimizations. The implication here is that there are countless other languages that let you do the same things as Rust or C++, but with a nonzero and often runtime-specific cost.

Upvotes: 7

Related Questions