简体   繁体   English

“零成本抽象”是什么意思?

[英]What does 'Zero Cost Abstraction' mean?

I came across this term when exploring Rust.我在探索 Rust 时遇到了这个术语。

I saw different kinds of explanations regarding this and still don't quite get the ideas.我看到了关于这个的不同类型的解释,但仍然不太明白。

In The Embedded Rust Book , it said嵌入式 Rust 书中,它说

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:由于它们不包含数据,因此它们在运行时在 memory 中没有实际表示:

Does it mean the runtime is faster because there is no memory in runtime?这是否意味着运行时更快,因为运行时没有 memory?

Appreciate it if anyone can explain it in an easy to understand way.如果有人可以以易于理解的方式解释它,请欣赏它。

Zero Cost Abstractions means adding higher-level programming concepts, like generics, collections and so on do not come with a run-time cost, only compiler time cost (the code will be slower to compile).零成本抽象意味着添加更高级别的编程概念,如 generics、collections 等不带来运行时成本,只有编译器时间成本(代码编译速度会更慢)。 Any operation on zero-cost abstractions is as fast as you would write out matching functionality by hand using lower-level programming concepts (for loops, counters, ifs and so on).零成本抽象上的任何操作都与您使用较低级别的编程概念(for 循环、计数器、ifs 等)手动编写匹配功能一样快。

Zero cost abstractions are ones that bear no runtime costs in execution speed or memory usage.零成本抽象是在执行速度或 memory 使用方面不承担运行时成本的抽象。

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.相比之下,虚方法是高成本抽象的一个很好的例子:在许多 OO 语言中,方法调用者的类型是在运行时确定的,这需要维护一个查找表(运行时 memory 用法),然后实际执行查找(每个方法的运行时开销)调用,可能至少是一个额外的指针取消引用)与运行时类型来确定调用哪个版本的方法。 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.另一个很好的例子是垃圾收集:作为回报,您不必担心您通过 GC 暂停支付的 memory 分配的细节。

Rust though mostly tries to have zero cost abstractions: ones that let you have your cake and eat it too. Rust 虽然主要尝试实现零成本抽象:让您拥有蛋糕并吃掉它的抽象。 Ones that compiler can safely and correctly convert to forms that bear no extra indirection/memory usage.编译器可以安全且正确地转换为不承担额外间接/内存使用的 forms 的那些。 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.事实上,我知道的唯一一件事(如果我错了,有人会纠正我)你在运行时在 Rust 中真正付出的代价是边界检查。

The concept of zero cost abstractions originally came from the functional world.零成本抽象的概念最初来自功能世界。 However, the terminology comes from C++.但是,该术语来自 C++。 According to Bjarne Stroustrup,根据 Bjarne Stroustrup 的说法,

In general, C++ implementations obey the zero-overhead principle: What you don't use, you don't pay for.通常,C++ 实现遵循零开销原则:不使用什么,就不用付费。 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.如果世界上只有一种编程语言:无论是 Rust 还是 C++,零成本抽象将与大多数其他编译器优化没有区别。 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.这里的含义是,有无数其他语言可以让你做与 Rust 或 C++ 相同的事情,但成本不为零,而且通常是特定于运行时的成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM