TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Rust: Zero-Cost Abstraction in Action

103 点作者 i_dursun超过 5 年前

13 条评论

bdd超过 5 年前
These are not about abstractions but compile time optimizations. These are also not implemented in the Rust compiler but LLVM. So any any language frontend in front of LLVM would yield the same optimizations. According to Wikipedia the list is:<p>&gt; [...] variety of front ends: languages with compilers that use LLVM include ActionScript, Ada, C#, Common Lisp, Crystal, CUDA, D, Delphi, Dylan, Fortran, Graphical G Programming Language,Halide, Haskell, Java bytecode, Julia, Kotlin, Lua, Objective-C, OpenGL Shading Language, Ruby, Rust, Scala, Swift, Xojo, and Zig.<p>Sometimes I think mention of Rust in the title just gets upvotes without even reading the article, here.
评论 #22269526 未加载
评论 #22274215 未加载
评论 #22269517 未加载
评论 #22277216 未加载
评论 #22268948 未加载
评论 #22270277 未加载
lasagnaphil超过 5 年前
This isn’t really talking about Rust, it’s actually talking about the optimization capabilities of LLVM (the compiler backend, which quite a lot of languages use, such as Clang for C++, Rust, Swift, Julia, Zig, ...) These languages all have similar chances of performing those same optimizations, at least in those simple cases.<p>What I’m interested is how the IL code for the compiler frontends for each of those languages are more well-optimizable for LLVM (in practical situations, not just a few lines of simple numerical code.) I’ve heard that you need to be careful about encoding IL code in the right way such that LLVM does not generate needless memcpy’s or something but I’m not that much of a compiler expert...
评论 #22268787 未加载
评论 #22274249 未加载
tiziano88超过 5 年前
There is nothing about zero const abstractions in this article, just basic compiler optimizations.
评论 #22268754 未加载
评论 #22269241 未加载
评论 #22268675 未加载
SeekingMeaning超过 5 年前
For anyone interested in reading more about this, I would recommend Zero Cost Abstractions[1] by withoutboats, who is a contributor to Rust.<p>1: <a href="https:&#x2F;&#x2F;boats.gitlab.io&#x2F;blog&#x2F;post&#x2F;zero-cost-abstractions&#x2F;" rel="nofollow">https:&#x2F;&#x2F;boats.gitlab.io&#x2F;blog&#x2F;post&#x2F;zero-cost-abstractions&#x2F;</a>
drej超过 5 年前
This is not Rust specific, this is a compiler thing, both LLVM and GCC can detect sums and generate closed form formulas instead. There are other fun algorithm detections - e.g. if you try to do bitcounts yourself, LLVM will use popcnt instead.<p>Compilers are awesome, check out Matt Godbolt&#x27;s talk on this very topic: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=nAbCKa0FzjQ" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=nAbCKa0FzjQ</a>
univerio超过 5 年前
I assume it&#x27;s doing `(N-2)(N-3)&#x2F;2 + 2N - 3` instead of `N(N-1)&#x2F;2` due to overflow concerns? But couldn&#x27;t `(N-2)(N-3)` also possibly overflow, just supporting a larger range of `N`?
评论 #22269382 未加载
Ericson2314超过 5 年前
The earlier transformations are actually more impressive. Constant folding is almost always has a good RoI, so lots of compilers do it, and it&#x27;s simple because, well, it&#x27;s a constant there is no variables or partial eval needed. The others require lots of inlining before the final rule fires, and so are more ambitious.<p>Seeing what<p><pre><code> pub fn sum3(n: i32) -&gt; i32 { (1..n).sum() + (1..2*n).sum() + (1..(n + 2)).sum() } </code></pre> does would be more interesting to me.<p>Also, while all the inline is rustc, I assume the &quot;triangle number trick&quot; is LLVM.
评论 #22270491 未加载
ojosilva超过 5 年前
I wonder if the const syntax introduced in ES6 will ever result in const folding, or any JIT optimizations for code running in JS runtimes. Apparently, from what I&#x27;ve read, const is being ignored as far as optimiztions go and is only used as a way to prevent the developer from ever reassigning certain variables.
pkilgore超过 5 年前
The ocaml compiler does constant folding too, and I consistently look at the (usually javascript because of Bucklescript) output in amazement when it finds shit like that. Unrolling all my tail recursion is great too.
评论 #22281398 未加载
incadenza超过 5 年前
Are compiler optimizations like summing a series done on an ad-hoc basis? Certainly the compiler couldn’t have discovered or inferred (not sure what term to use) that formula, no?<p>Just generally curious.
评论 #22268720 未加载
评论 #22268711 未加载
boomer_joe超过 5 年前
There is nothing impressive about this. <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;S2tDDh" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;S2tDDh</a>
shmerl超过 5 年前
<i>&gt; he was very disappointed because Rust version was twice as fast than the C version which was hand-optimised by pulling off all the tricks he knew to make it perform well.</i><p>Why disappointed? It just highlights the quality of Rust&#x27;s approach.
评论 #22273277 未加载
yahyaheee超过 5 年前
This is neat but there is still cost in compile time
评论 #22269211 未加载