HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustCritical

What does 'Zero Cost Abstraction' mean?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
costzerodoesabstractionmeanwhat

Problem

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.

Solution

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.

Context

Stack Overflow Q#69178380, score: 115

Revisions (0)

No revisions yet.