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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

But I was helping the compiler

72 点作者 aw1621107将近 5 年前

9 条评论

comex将近 5 年前
Two important points:<p>- For std::array of uint8_t – an in-place container of a plain-old-data type – the move constructor and the copy constructor, if not optimized away, will do the exact same thing! Both boil down to copying the bytes from the old object to the new object. So there is never any benefit to calling std::move on such an object. This is different from out-of-line containers like std::vector, where the std::vector object itself consists only of a pointer and some lengths. In that case, the move constructor just copies the pointer and length values themselves and nulls them out in the original object, without copying the underlying data. But the copy constructor has to allocate a whole new heap buffer, copy the underlying data, and initialize the new vector object with <i>that</i> pointer.<p>- The godbolt links are building without optimization enabled (without -O settings)! GCC apparently still performs (non-mandatory) NRVO in this case, probably because that optimization is implemented on the frontend rather than the backend, and the frontend tends to care less about -O settings. But GCC does not perform the usual suite of backend optimizations, including inlining and memcpy elimination. If optimizations were enabled, GCC might be able to optimize both versions into the same thing, depending on what actually happens in “&#x2F;&#x2F; Do some computation”. It doesn’t make sense to make performance comparisons with optimization disabled, unless you’re trying to improve the performance of debug builds themselves.
评论 #24237789 未加载
评论 #24239934 未加载
tylerhou将近 5 年前
1) It&#x27;s useless to reason about the performance of generated assembly unless you use -O{1,2,3}. Another commenter says that they&#x27;re surprised std::move isn&#x27;t inlined; that&#x27;s because -O{1,2,3} isn&#x27;t passed.<p>2) If you care that much about performance, you can avoid all copies by returning by reference in this case.<p>3) The author seems to misunderstand std::move — in this case, std::move can <i>only</i> pessimize performance and never optimize because the move constructor of std::array is the exact same as the copy constructor.<p>For example, move constructors help performance for std::vector because the move constructor will copy the backing buffer&#x27;s pointer instead of copying the entire backing buffer itself.<p>Since std::array&lt;T&gt; is a thin wrapper around T[], the entire array is stored on the stack, and there is no possible optimization that a move could make — stack values <i>must</i> be copied (or otherwise initialized) in move constructors. In fact, since std::array is an aggregate type, there isn&#x27;t even a user-defined move constructor (or copy constructor)! <a href="https:&#x2F;&#x2F;github.com&#x2F;gcc-mirror&#x2F;gcc&#x2F;blob&#x2F;master&#x2F;libstdc%2B%2B-v3&#x2F;include&#x2F;std&#x2F;array#L112" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcc-mirror&#x2F;gcc&#x2F;blob&#x2F;master&#x2F;libstdc%2B%2B-...</a>
评论 #24238084 未加载
notacoward将近 5 年前
This is the thing that always drives me nuts about C++: the compiler always trying to second-guess me, or me having to second-guess it. If I had wanted to spend time thinking about compiler internals I would have made that choice decades ago (after I actually did work on compilers a bit along with everything else). If I want low-level control to achieve best performance or efficiency, I&#x27;ll just use C and be careful to avoid its deficiencies. Been there, done that, I&#x27;m actually pretty good at it. Then I&#x27;ll link that with code written in a language that lets me express higher-level intent at a decent level of abstraction, which is impossible with the $#@! compiler in my face all the time.
评论 #24239532 未加载
评论 #24237833 未加载
评论 #24245607 未加载
评论 #24243291 未加载
lilyball将近 5 年前
Why does adding the std::move() change the generated code though? It changes the prvalue to an xvalue, but std::move is equivalent to a static_cast, and shouldn’t both the prvalue and the xvalue versions end up calling the same std::array move constructor?<p>Edit: I just took a look at the actual godbolt links. And it looks like the std::move version actually puts a call to std::move in the generated assembly. I’m kind of surprised this is actually a literal function call, I would have assumed std::move would be always inlined.
评论 #24236871 未加载
评论 #24236807 未加载
评论 #24236964 未加载
jeffbee将近 5 年前
Depending on what is in the body of the function, I usually prefer to see an output iterator taken as a function parameter, instead of functions returning whole collections. In the article&#x27;s style the caller is forced to deal with std::array even if that&#x27;s not suitable at the call site. With an output iterator and a template function the caller gets to pick whatever suits the purpose best. Not having to think about how the output was allocated and deleted is just a bonus of this style.
knorker将近 5 年前
Never use std::move on an rvalue. That is always wrong, and shows a misunderstanding of std::move.<p>std::move doesn&#x27;t move. It should maybe have been called std::lvalue_to_rvalue.
评论 #24242542 未加载
CyberRabbi将近 5 年前
You don’t need a copy here. A const reference to the internal array is all you need, and it’s faster.
platinumrad将近 5 年前
In addition to the significant issues pointed out by comex and tylerhou, NRVO is not in play here as `gfx` is not a non-volatile object with automatic storage duration.
marta_morena_25将近 5 年前
You really don&#x27;t need to read assembly for any but the most rare cases. What you need is a profiler to give you the hotspots and you need to stop prematurely optimizing on a line-by-line basis. Just stop it. Optimize O(n) performance (but also don&#x27;t go wild, unless there is a business reason), keep your fingers off micro-optimizations, unless so indicated by the profiler and only if there is a business reason as well...
评论 #24238272 未加载
评论 #24239062 未加载
评论 #24238289 未加载