<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 25, 2016 at 9:09 AM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span><blockquote type="cite"><div>On Mar 25, 2016, at 9:01 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 25, 2016 at 8:59 AM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">It would, it is just hard to write :)<div><br></div><div>Usually the pattern is that the client side has two statements: 1 for the allocation and 1 for the insertion. So you can get the number of buckets between the allocation and the insertion, and verify that it is the same after. Here everything happens in the constructor...</div></div></blockquote><div><br></div><div>I was thinking you could count the moves - if the initial size is set to small, you should see a reallocation during the init list ctor execution, and you should see a bunch of move constructions. With the size set correctly you shouldn't see any move constructions, only copy constructions (in either case you should see exactly as many copy constructions as there are elements in the init list).<br></div></div></div></div></div></blockquote><div><br></div></span><div>OK, but we first need to figure the move-elision thing :(</div><div><br></div><div>So when we start with (StringMap)Map.insert(std::make_pair(Twine(i).str(), CountCtorCopyAndMove()));</div></div></div></blockquote><div><br></div><div>Pretty sure we have to have a move here ^ (there's a distinct object that can't be constructed in place inside the pair)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div>We have a first potential move to create the pair. </div><div>Then we can have a second move when the pair is moved as a parameter to insert().</div><div><br></div><div>std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {</div><div>  return emplace_second(KV.first, std::move(KV.second));<br></div></div></div></blockquote><div><br></div><div>std::move doesn't move anything - it just lets the lvalue be treated as an xvalue. (it's a static cast to T&& type)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div>}<br><br></div><div>Here we have an explicit move (can it be legally elided or is it forced?).</div><div><br></div><div>In emplace_second we forward (another potential move):</div><div><br></div><div>  template <typename... ArgsTy><br>  std::pair<iterator, bool> emplace_second(StringRef Key, ArgsTy &&... Args) {<br>....</div><div>    Bucket = MapEntryTy::Create(Key, Allocator, std::forward<ArgsTy>(Args)...);<br></div></div></div></blockquote><div><br></div><div>std::forward doesn't move anything either - same deal as std::move (except it's applying whatever type the parameter was, rather than necessarily casting to an rvalue type)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div><br></div><div>In `create`, we forward again (yet another potential move?):</div><div><br></div><div>  template <typename AllocatorTy, typename... InitTy><br>  static StringMapEntry *Create(StringRef Key, AllocatorTy &Allocator,<br>                                InitTy &&... InitVals) {<br>....</div><div>    // Construct the value.<br>    new (NewItem) StringMapEntry(KeyLength, std::forward<InitTy>(InitVals)...);<br><br></div><div>Finally the last move is when we construct the value in the entry:</div><div><br></div><div>  StringMapEntry(unsigned strLen, InitTy &&... InitVals)<br>      : StringMapEntryBase(strLen), second(std::forward<InitTy>(InitVals)...) {}<br></div></div></div></blockquote><div><br></div><div>Then this is actually going to move.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div><br></div><div><br></div><div>So a potential of 6 moves, but clang will perform only 3, but MSVC will perform 4.</div></div></div></blockquote><div><br>My /guess/ is that MSVC is doing an extra move in make_pair. A test case using emplace should produce a predictable/guaranteed number of moves instead.<br><br>For DenseMap, since we don't have emplace there yet, you could at least use std::pair's piecewise_construct, to ensure that no extra moves are happening through library layers, etc.<br><br><br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span><font color="#888888"><div><br></div><div><br></div><div>-- </div><div>Mehdi</div></font></span><div><div><div><br></div><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br>At least that's how I'm picturing it.<br><br>- Dave</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span><font color="#888888"><div><br></div><div>-- </div><div>Mehdi</div></font></span><div><div><div><br></div><div><div><div><blockquote type="cite"><div>On Mar 25, 2016, at 7:11 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br><div><p dir="ltr">Worth a test?</p>
<div class="gmail_quote">On Mar 24, 2016 11:02 PM, "Mehdi Amini via llvm-commits" <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: mehdi_amini<br>
Date: Fri Mar 25 00:57:47 2016<br>
New Revision: 264383<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=264383&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=264383&view=rev</a><br>
Log:<br>
StringMap: reserve appropriate size when initializing from an initializer list<br>
<br>
From: Mehdi Amini <<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ADT/StringMap.h<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/StringMap.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringMap.h?rev=264383&r1=264382&r2=264383&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringMap.h?rev=264383&r1=264382&r2=264383&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/StringMap.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/StringMap.h Fri Mar 25 00:57:47 2016<br>
@@ -233,7 +233,7 @@ public:<br>
       Allocator(A) {}<br>
<br>
   StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)<br>
-      : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {<br>
+      : StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {<br>
     for (const auto &P : List) {<br>
       insert(P);<br>
     }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>
</div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div></div>
</div></blockquote></div></div></div><br></div></blockquote></div><br></div></div>