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.

When zero cost abstractions aren't zero cost

166 pointsby harporoederalmost 4 years ago

15 comments

WiSaGaNalmost 4 years ago
According to this comment: <a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;p0ul6b&#x2F;when_zero_cost_abstractions_arent_zero_cost&#x2F;h8b7b2w&#x2F;" rel="nofollow">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;p0ul6b&#x2F;when_zero_cost...</a> , `rustc`&#x2F;`llvm` is indeed able to optimize the wrapped example to a big `memset`. Sure in this specific case, it is different from the first one which delayed the zeroing, but that is not unintelligently `clone` many times as claimed in the article.
stormbrewalmost 4 years ago
I don&#x27;t really feel like this is an issue of &quot;zero cost abstractions&quot;. The baseline expectation should be that it will do something reasonable, which in this case is probably `if Copy do bit-setting with memset&#x2F;bzero if possible, otherwise clone() loop` which is what it appears to do according to a reddit comment linked to elsewhere.<p>I&#x27;m not really sure it follows, though, that you should ever expect more than that. It&#x27;s nice if you can get it but the presence of a very specific optimization shouldn&#x27;t be taken as the new &quot;zero cost&quot; baseline, and I don&#x27;t think it should be taken as given that a newtype will inherit all possible optimizations of its enclosed type.
评论 #28124441 未加载
评论 #28128890 未加载
brundolfalmost 4 years ago
&gt; the desire to optimize one specific case while neglecting the general case<p>This feels like a recurring pattern in Rust&#x27;s design whether we&#x27;re talking about performance, what&#x27;s accepted by the compiler, etc. I&#x27;m not sure it&#x27;s a bad thing exactly, but it leads to a lot of unintuitive footguns. Of course the consequence of these footguns is de-optimization or a compiler error, instead of a runtime exception or a memory error. But nevertheless it can make for a confusing landscape of behavior to navigate.<p>Should they <i>not</i> try and spot-optimize common cases where they can, just for the sake of predictability? No, I don&#x27;t think so. And so I don&#x27;t know what the answer is. I just often (and even more so when I was new to it) find myself surprised by Rust&#x27;s general-rules-that-actually-have-notable-exceptions. Though who knows, maybe it&#x27;s just not possible to make a language this powerful that doesn&#x27;t run into this problem.
评论 #28132800 未加载
评论 #28129603 未加载
codetrotteralmost 4 years ago
There are many useful and interesting comments in the discussion about this on r&#x2F;rust.<p><a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;p0ul6b&#x2F;when_zero_cost_abstractions_arent_zero_cost&#x2F;" rel="nofollow">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;p0ul6b&#x2F;when_zero_cost...</a>
erk__almost 4 years ago
Some months ago I ran into the same problem in slightly different clothing<p><pre><code> &#x2F;&#x2F; 1st-way vec![0; len]; &#x2F;&#x2F; 2nd-way std::iter::repeat(0).take(len).collect::&lt;Vec&lt;_&gt;&gt;(); </code></pre> As told about in the blog post the first one will be able to be optimized via specializations to a single calloc call. The other here cannot use the same specialization as it does not seem to be able to specialize on the type of iterator yet. This means it will be a malloc followed by a memset when compiled.<p>godbolt: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;8bM7zz4E7" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;8bM7zz4E7</a><p>the specific specialization: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;master&#x2F;library&#x2F;alloc&#x2F;src&#x2F;vec&#x2F;spec_from_elem.rs#L35..L48" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;master&#x2F;library&#x2F;alloc&#x2F;...</a>
评论 #28124850 未加载
pdimitaralmost 4 years ago
What does that post prove except that &quot;allocating memory that must be zeroed on use but is never used&quot; is million times faster than &quot;allocate memory and zero it right away&quot;?<p>Sure, I can see how the code doesn&#x27;t immediately tell you the difference but <i>after</i> you understood it, isn&#x27;t it completely logical?<p>The title seems clickbaity and not accurate when you dig into the article.
评论 #28127084 未加载
评论 #28133571 未加载
hitekkeralmost 4 years ago
&gt; The point of this post is not to bash the Rust team, but rather to raise awareness. Language design is a difficult process full of contradictory tradeoffs. Without a clear vision of which properties you value above all else, it is easy to accidentally violate the guarantees you think you are making. This is especially true for complex systems languages like C++ and Rust which try to be all things to all people and leave no stone of potential optimization unturned.<p>This paragraph is worth highlighting. Say yes to everything and vision becomes meaningless. Can&#x27;t move in one direction when you&#x27;re pulled by an infinite number of stakeholders on all sides.
评论 #28124639 未加载
gridspyalmost 4 years ago
The article is not about what you think it&#x27;s about.<p>1. Turns out that zeroing u8 is a special case so if you allocate 17 GB of your custom structure Rust is gonna init that for you.<p>2. Okay, so you don&#x27;t understand how RefCell works? The caller to this function should be holding r, not passing it into the function. Of course that will just correctly Panic, so it&#x27;s not wonderful. But that is the point of RefCell.<p>Generally more stuff runs fast in Rust than in other languages, but you can always find bad counterexamples in any language.
评论 #28124861 未加载
the8472almost 4 years ago
&gt; However, when v has an arbitrary type, it will instead just clone() it for every single element of the array, which is many orders of magnitude slower. So much for zero cost abstraction.<p>Arguably the WrapperType, i.e. any arbitrary type is the <i>general case</i> and thus the baseline performance. `u8` is the special case (hence specialization) that performs the alloc_zeroed optimization. So it&#x27;s not really that the abstraction adds a cost. It&#x27;s just that the special case removes a cost paid by everything else.<p>In the future the vec initialization might be fixable, but this requires turning potentially undefined values into some valid if arbitrary bit patterns (i.e. llvm&#x27;s freeze) to compare it against zero.
CountHackulusalmost 4 years ago
There are no zero cost abstractions. (According to Chandler Carruth, and he makes a convincing point). <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=rHIkrotSwcc" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=rHIkrotSwcc</a>
评论 #28124644 未加载
评论 #28124796 未加载
评论 #28124857 未加载
评论 #28124250 未加载
jasonhanselalmost 4 years ago
Personally, this is why I think specialization is a bad idea. It means that minor refactors can have very strange impacts on performance, and that every API now has an (often undocumented) set of types for which it is unexpectedly much more performant.
评论 #28129063 未加载
评论 #28128783 未加载
worikalmost 4 years ago
Arguing against zero cost abstractions should use as a counter argument an abstraction that is advertised as zero cost.<p>I do not think wrapped types are so advertised.
评论 #28124860 未加载
yyykalmost 4 years ago
The &#x27;zero cost&#x27; attitude is one of C++ problems. Zero cost abstractions are never really entirely zero cost. You still pay, often in something that you didn&#x27;t bother to measure, which (in C++) is usually compile time or programmer productivity or programmer cognitive cost or compiler complexity.<p>There&#x27;s a reason a lot of the complaints about C++ mention bloat or the difficulty of practically choosing a safe subset (you could use a subset, but you&#x27;re very likely dependent on coworkers, existing codebases and library authors which may not share your subset). A lot of people added &#x27;zero cost&#x27; stuff, and all of that has a cost.<p>For example, lets say Rust&#x27;s newtype pattern worked perfectly everywhere, and all of the author&#x27;s examples run fine as u8 with no runtime cost. There would likely still be a small cognitive cost to learn this, a small cost to unwrap the types (for other programmers reading the code), and a tiny cost in compile time. Typedefs are worth it, and it&#x27;s all very reasonable to pay this!<p>But there&#x27;s still a cost, and when people never believe there&#x27;s a cost a language ends up like C++.
评论 #28124628 未加载
评论 #28124804 未加载
评论 #28124784 未加载
评论 #28125029 未加载
评论 #28125717 未加载
评论 #28124822 未加载
nathiasalmost 4 years ago
There is no zero cost abstractions.
hsn915almost 4 years ago
This is .. amazing? Using what essentially should amount to a strong typedef .. causes code to be millions of times slower.<p>This is worth bookmarking.
评论 #28124642 未加载
评论 #28124358 未加载
评论 #28124805 未加载
评论 #28124366 未加载
评论 #28124682 未加载