[lld] 3eb16fe - [LLD] Have only one SpecificAllocator per type

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 14:09:17 PDT 2020


Author: Reid Kleckner
Date: 2020-06-02T14:09:09-07:00
New Revision: 3eb16fe4e945631988d6d302d0bc317d8c07279c

URL: https://github.com/llvm/llvm-project/commit/3eb16fe4e945631988d6d302d0bc317d8c07279c
DIFF: https://github.com/llvm/llvm-project/commit/3eb16fe4e945631988d6d302d0bc317d8c07279c.diff

LOG: [LLD] Have only one SpecificAllocator per type

Previously, the SpecificAllocator was a static local in the `make<T>`
function template. Using static locals is nice because they are only
constructed and registered if they are accessed. However, if there are
multiple calls to make<> with different constructor parameters, we would
get multiple static local variable instances. This is undesirable and
leads to extra memory allocations. I noticed there were two sources of
DefinedRegular allocations while checking heap profiles.

Added: 
    

Modified: 
    lld/include/lld/Common/Memory.h

Removed: 
    


################################################################################
diff  --git a/lld/include/lld/Common/Memory.h b/lld/include/lld/Common/Memory.h
index 41d8f15d7b34..f516a327cfb2 100644
--- a/lld/include/lld/Common/Memory.h
+++ b/lld/include/lld/Common/Memory.h
@@ -47,11 +47,20 @@ template <class T> struct SpecificAlloc : public SpecificAllocBase {
   llvm::SpecificBumpPtrAllocator<T> alloc;
 };
 
+// Use a static local for these singletons so they are only registered if an
+// object of this instance is ever constructed. Otherwise we will create and
+// register ELF allocators for COFF and the reverse.
+template <typename T>
+inline llvm::SpecificBumpPtrAllocator<T> &getSpecificAllocSingleton() {
+  static SpecificAlloc<T> instance;
+  return instance.alloc;
+}
+
 // Use this arena if your object has a destructor.
 // Your destructor will be invoked from freeArena().
 template <typename T, typename... U> T *make(U &&... args) {
-  static SpecificAlloc<T> alloc;
-  return new (alloc.alloc.Allocate()) T(std::forward<U>(args)...);
+  return new (getSpecificAllocSingleton<T>().Allocate())
+      T(std::forward<U>(args)...);
 }
 
 } // namespace lld


        


More information about the llvm-commits mailing list