[PATCH] D95654: ADT: Sink the guts of StringMapEntry::Create into StringMapEntryBase

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 28 17:46:42 PST 2021


dexonsmith created this revision.
dexonsmith added a reviewer: dblaikie.
Herald added a subscriber: ributzka.
dexonsmith requested review of this revision.
Herald added a project: LLVM.

Sink the interesting parts of StringMapEntry::Create into a new function
StringMapEntryBase::allocateWithKey that's only templated on the
allocator, taking the entry size and alignment as parameters.

I double-checked this doesn't impact clang's compile time performance here:
https://llvm-compile-time-tracker.com/compare.php?from=2d430f902d72b8a1d3bc036a80273ca80af1e338&to=63e5ea3bf2a2b59508e452aee6a9dc0eb369f527&stat=instructions


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95654

Files:
  llvm/include/llvm/ADT/StringMapEntry.h


Index: llvm/include/llvm/ADT/StringMapEntry.h
===================================================================
--- llvm/include/llvm/ADT/StringMapEntry.h
+++ llvm/include/llvm/ADT/StringMapEntry.h
@@ -27,8 +27,34 @@
   explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
 
   size_t getKeyLength() const { return keyLength; }
+
+protected:
+  template <typename AllocatorTy>
+  static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
+                               StringRef Key, AllocatorTy &Allocator);
 };
 
+// Define out-of-line to dissuade inlining.
+template <typename AllocatorTy>
+void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
+                                          StringRef Key,
+                                          AllocatorTy &Allocator) {
+  size_t KeyLength = Key.size();
+
+  // Allocate a new item with space for the string at the end and a null
+  // terminator.
+  size_t AllocSize = EntrySize + KeyLength + 1;
+  void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
+  assert(Allocation && "Unhandled out-of-memory");
+
+  // Copy the string information.
+  char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
+  if (KeyLength > 0)
+    ::memcpy(Buffer, Key.data(), KeyLength);
+  Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
+  return Allocation;
+}
+
 /// StringMapEntryStorage - Holds the value in a StringMapEntry.
 ///
 /// Factored out into a separate base class to make it easier to specialize.
@@ -90,26 +116,9 @@
   template <typename AllocatorTy, typename... InitTy>
   static StringMapEntry *Create(StringRef key, AllocatorTy &allocator,
                                 InitTy &&... initVals) {
-    size_t keyLength = key.size();
-
-    // Allocate a new item with space for the string at the end and a null
-    // terminator.
-    size_t allocSize = sizeof(StringMapEntry) + keyLength + 1;
-    size_t alignment = alignof(StringMapEntry);
-
-    StringMapEntry *newItem =
-        static_cast<StringMapEntry *>(allocator.Allocate(allocSize, alignment));
-    assert(newItem && "Unhandled out-of-memory");
-
-    // Construct the value.
-    new (newItem) StringMapEntry(keyLength, std::forward<InitTy>(initVals)...);
-
-    // Copy the string information.
-    char *strBuffer = const_cast<char *>(newItem->getKeyData());
-    if (keyLength > 0)
-      memcpy(strBuffer, key.data(), keyLength);
-    strBuffer[keyLength] = 0; // Null terminate for convenience of clients.
-    return newItem;
+    return new (StringMapEntryBase::allocateWithKey(
+        sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
+        StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
   }
 
   /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95654.320014.patch
Type: text/x-patch
Size: 2841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210129/8d5181c8/attachment.bin>


More information about the llvm-commits mailing list