[llvm-dev] [cfe-dev] RFC: Replacing the default CRT allocator on Windows

Alexandre Ganea via llvm-dev llvm-dev at lists.llvm.org
Mon Jul 6 10:51:43 PDT 2020


Hi Adrian!

I completely agree with you, we should be clear on the wording. This proposal is about replacing both the MS CRT malloc layer *AND* the HeapAlloc layer.

C++ --> {mimalloc|rpmalloc|snmalloc} --> VirtualAlloc (Win32)

The bottom line is that libraries in LLVM allocate a lot, lots of small allocations. I think that should be solved on its own, perhaps by using BumpPtrAllocator a bit more. But this is fragile, any new system in LLVM could later add lots of allocations, and induce heap locking further down the line.

The MS CRT malloc layer is a very thin wrapper over HeapAlloc. But the issue we want to solve is contention at the HeapAlloc layer level. There’s only one heap by default for the entire process, every thread allocating needs to “aquire” the heap, thus the lock. On the short term, I don’t see an easy way to solve this, except by bypassing HeapAlloc completely.

Reid mentioned that a Google toolchain/platform team experimented with tcmalloc3 (the one published here: https://github.com/google/tcmalloc - not the one in gperftools) integrated into LLVM and got similar results to ones in the review below.


De : Adrian McCarthy <amccarth at google.com>
Envoyé : July 6, 2020 11:52 AM
À : Alexandre Ganea <alexandre.ganea at ubisoft.com>
Cc : James Y Knight <jyknight at google.com>; LLVM Dev <llvm-dev at lists.llvm.org>; Clang Dev <cfe-dev at lists.llvm.org>
Objet : Re: [llvm-dev] [cfe-dev] RFC: Replacing the default CRT allocator on Windows

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."

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.

C++ --> malloc (CRT) --> HeapAlloc (Win32) --> VirtualMalloc (Win32)
           |                                       ^
           +---------------------------------------+

This proposal is talking about replacing the malloc layer.

The "low-fragmentation heap" and "segment heap" are features of process heaps (HeapAlloc).

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.

The only Windows app I worked on that switched malloc implementations was a cross-platform app.  On every platform, tcmalloc was a win, except 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.)

On Thu, Jul 2, 2020 at 8:57 PM Alexandre Ganea via llvm-dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>> wrote:
Thanks for the suggestion James, it reduces the commit by about ~900 MB (14,9 GB -> 14 GB).

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.


De : James Y Knight <jyknight at google.com<mailto:jyknight at google.com>>
Envoyé : July 2, 2020 6:08 PM
À : Alexandre Ganea <alexandre.ganea at ubisoft.com<mailto:alexandre.ganea at ubisoft.com>>
Cc : Clang Dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>>; LLVM Dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>>
Objet : Re: [cfe-dev] RFC: Replacing the default CRT allocator on Windows

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.

If the performance is good, seems like that might be the simplest choice

https://docs.microsoft.com/en-us/windows/win32/sbscs/application-manifests#heaptype

https://www.blackhat.com/docs/us-16/materials/us-16-Yason-Windows-10-Segment-Heap-Internals.pdf

On Thu, Jul 2, 2020, 12:20 AM Alexandre Ganea via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:
Hello,

I was wondering how folks were feeling about replacing the default Windows CRT allocator in Clang, LLD and other LLVM tools possibly.

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.

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.

Time to link clang.exe with LLD and -flto on 36-core:
  Windows CRT heap allocator: 38 min 47 sec
  mimalloc: 2 min 22 sec
  rpmalloc: 2 min 15 sec
  snmalloc: 2 min 19 sec

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.

Two questions arise:

  1.  The licencing. Should we embed one of these allocators into the LLVM tree, or keep them separate out-of-the-tree?
  2.  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.

Please see demo patch here: https://reviews.llvm.org/D71786

Thank you in advance for the feedback!
Alex.

_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
LLVM Developers mailing list
llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200706/9c912558/attachment-0001.html>


More information about the llvm-dev mailing list