<div dir="ltr">Let's make sure we're all working from the set set of assumptions.  There are several layers of allocators in a Windows application, and I think it's worth being explicit, since I've already seen comments referring to multiple different "allocators."<div><br></div><div>The C++ allocators and new and delete operators are generally built on malloc from the C run-time library.  malloc implementations typically rely on process heaps (Win32 HeapAlloc) and/or go directly to virtual memory (Win32 VirtualAlloc).  HeapAlloc itself uses VirtualAlloc.</div><div><br></div><div><font face="monospace" size="1">C++ --> malloc (CRT) --> HeapAlloc (Win32) --> VirtualMalloc (Win32)</font></div><div><font face="monospace" size="1">           |                                       ^</font></div><div><font face="monospace" size="1">           +---------------------------------------+</font></div><div><font face="monospace" size="1"><br></font></div><div>This proposal is talking about replacing the malloc layer.<br></div><div><br></div><div>The "low-fragmentation heap" and "segment heap" are features of process heaps (HeapAlloc).</div><div><br></div><div>Since those are in different layers, there's some orthogonality there.  If your malloc implementation uses HeapAlloc, then tweaking process heap features may affect performance assuming the bottleneck isn't in malloc itself.  If you replace the malloc implementation with one that completely bypasses the process heap by going directly to virtual memory, that's a horse of a different color.</div><div><br></div><div>The only Windows app I worked on that switched malloc implementations was a cross-platform app.  On every platform, tcmalloc was a win, <i>except </i>for Windows.  We kept Windows on the default Microsoft implementation because it performed better.  (The app was a multithreaded real-time graphics simulation.  The number of threads was low, maybe 4 or 5.)<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 2, 2020 at 8:57 PM Alexandre Ganea via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-CA">
<div class="gmail-m_1170423376599759767WordSection1">
<p class="MsoNormal"><span>Thanks for the suggestion James, it reduces the commit by about ~900 MB (14,9 GB -> 14 GB).<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Unfortunately it does not solve the performance problem. The heap is global to the application and thread-safe, so every malloc/free locks it, which evidently doesn’t scale. We could manually create
 thread-local heaps, but I didn’t want to go there. Ultimately allocated blocks need to share ownership between threads, and at that point it’s like re-writing a new allocator. I suppose most non-Windows platforms already have lock-free thread-local arenas,
 which probably explains why this issue has gone (mostly) unnoticed.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span lang="FR">De :</span></b><span lang="FR"> James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.com</a>>
<br>
<b>Envoyé :</b> July 2, 2020 6:08 PM<br>
<b>À :</b> Alexandre Ganea <<a href="mailto:alexandre.ganea@ubisoft.com" target="_blank">alexandre.ganea@ubisoft.com</a>><br>
<b>Cc :</b> Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>; LLVM Dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Objet :</b> Re: [cfe-dev] RFC: Replacing the default CRT allocator on Windows<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Have you tried Microsoft's new "segment heap" implementation? Only apps that opt-in get it at the moment. Reportedly edge and chromium are getting large memory savings from switching, but I haven't seen performance comparisons.<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If the performance is good, seems like that might be the simplest choice <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#heaptype" target="_blank">https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#heaptype</a><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://www.blackhat.com/docs/us-16/materials/us-16-Yason-Windows-10-Segment-Heap-Internals.pdf" target="_blank">https://www.blackhat.com/docs/us-16/materials/us-16-Yason-Windows-10-Segment-Heap-Internals.pdf</a><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, Jul 2, 2020, 12:20 AM Alexandre Ganea via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><span lang="FR-CA">Hello,</span><u></u><u></u></p>
<p class="MsoNormal"><span lang="FR-CA"> </span><u></u><u></u></p>
<p class="MsoNormal">I was wondering how folks were feeling about replacing the default Windows CRT allocator in Clang, LLD and other LLVM tools possibly.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">The CRT heap allocator on Windows doesn’t scale well on large core count machines. Any multi-threaded workload in LLVM that allocates often is impacted by this. As a result, link
 times with ThinLTO are extremely slow on Windows. We’re observing performance inversely proportional to the number of cores. The more cores the machines has, the slower ThinLTO linking gets.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">We’ve replaced the CRT heap allocator by modern lock-free thread-cache allocators such as rpmalloc (unlicence), mimalloc (MIT licence) or snmalloc (MIT licence). The runtime performance
 is an order of magnitude faster.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Time to link clang.exe with LLD and -flto on 36-core:<u></u><u></u></p>
<p class="MsoNormal">  Windows CRT heap allocator: 38 min 47 sec<u></u><u></u></p>
<p class="MsoNormal">  mimalloc: 2 min 22 sec<u></u><u></u></p>
<p class="MsoNormal">  rpmalloc: 2 min 15 sec<u></u><u></u></p>
<p class="MsoNormal">  snmalloc: 2 min 19 sec<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">We’re running in production with a downstream fork of LLVM + rpmalloc for more than a year. However when cross-compiling some specific game platforms we’re using other downstream
 forks of LLVM that we can’t change.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Two questions arise:<u></u><u></u></p>
<ol start="1" type="1">
<li class="gmail-m_1170423376599759767m-664316201346764609msolistparagraph">
The licencing. Should we embed one of these allocators into the LLVM tree, or keep them separate out-of-the-tree?
<u></u><u></u></li><li class="gmail-m_1170423376599759767m-664316201346764609msolistparagraph">
If the answer for above question is “yes”, given the tremendous performance speedup, should we embed one of these allocators into Clang/LLD builds by default? (on Windows only) Considering that Windows doesn’t have a LD_PRELOAD mechanism.<u></u><u></u></li></ol>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Please see demo patch here:
<span lang="FR-CA"><a href="https://reviews.llvm.org/D71786" target="_blank"><span lang="EN-CA">https://reviews.llvm.org/D71786</span></a></span><u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thank you in advance for the feedback!<u></u><u></u></p>
<p class="MsoNormal">Alex.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><u></u><u></u></p>
</blockquote>
</div>
</div>
</div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>