TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust: Zero-Cost Abstraction in Action

103 pointsby i_dursunover 5 years ago

13 comments

bddover 5 years ago
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 未加载
lasagnaphilover 5 years ago
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 未加载
tiziano88over 5 years ago
There is nothing about zero const abstractions in this article, just basic compiler optimizations.
评论 #22268754 未加载
评论 #22269241 未加载
评论 #22268675 未加载
SeekingMeaningover 5 years ago
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>
drejover 5 years ago
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>
univerioover 5 years ago
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 未加载
Ericson2314over 5 years ago
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 未加载
ojosilvaover 5 years ago
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.
pkilgoreover 5 years ago
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 未加载
incadenzaover 5 years ago
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_joeover 5 years ago
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>
shmerlover 5 years ago
<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 未加载
yahyaheeeover 5 years ago
This is neat but there is still cost in compile time
评论 #22269211 未加载