From be653ec8d4b0a5944fcf084a911389cb0c5cc205 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 23:07:38 -0400 Subject: [PATCH 11/31] Remove allocator from collections --- llvm/include/llvm/ADT/StringMap.h | 33 +++++++------------------- llvm/include/llvm/ADT/StringMapEntry.h | 25 +++++++------------ llvm/unittests/ADT/StringMapTest.cpp | 14 +++++------ 3 files changed, 23 insertions(+), 49 deletions(-) diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index 5f463cfef943..3b40bba37f58 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -104,10 +104,8 @@ public: /// keys that are "strings", which are basically ranges of bytes. This does some /// funky memory allocation and hashing things to make it extremely efficient, /// storing the string data *after* the value in the map. -template +template class StringMap : public StringMapImpl { - AllocatorTy Allocator; - public: using MapEntryTy = StringMapEntry; @@ -116,14 +114,6 @@ public: explicit StringMap(unsigned InitialSize) : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))) {} - explicit StringMap(AllocatorTy A) - : StringMapImpl(static_cast(sizeof(MapEntryTy))), Allocator(A) { - } - - StringMap(unsigned InitialSize, AllocatorTy A) - : StringMapImpl(InitialSize, static_cast(sizeof(MapEntryTy))), - Allocator(A) {} - StringMap(std::initializer_list> List) : StringMapImpl(List.size(), static_cast(sizeof(MapEntryTy))) { for (const auto &P : List) { @@ -132,11 +122,10 @@ public: } StringMap(StringMap &&RHS) - : StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {} + : StringMapImpl(std::move(RHS)) {} - StringMap(const StringMap &RHS) - : StringMapImpl(static_cast(sizeof(MapEntryTy))), - Allocator(RHS.Allocator) { + StringMap(const StringMap &RHS) : + StringMapImpl(static_cast(sizeof(MapEntryTy))) { if (RHS.empty()) return; @@ -156,7 +145,7 @@ public: } TheTable[I] = MapEntryTy::Create( - static_cast(Bucket)->getKey(), Allocator, + static_cast(Bucket)->getKey(), static_cast(Bucket)->getValue()); HashTable[I] = RHSHashTable[I]; } @@ -171,7 +160,6 @@ public: StringMap &operator=(StringMap RHS) { StringMapImpl::swap(RHS); - std::swap(Allocator, RHS.Allocator); return *this; } @@ -183,16 +171,13 @@ public: for (unsigned I = 0, E = NumBuckets; I != E; ++I) { StringMapEntryBase *Bucket = TheTable[I]; if (Bucket && Bucket != getTombstoneVal()) { - static_cast(Bucket)->Destroy(Allocator); + static_cast(Bucket)->Destroy(); } } } free(TheTable); } - AllocatorTy &getAllocator() { return Allocator; } - const AllocatorTy &getAllocator() const { return Allocator; } - using key_type = const char *; using mapped_type = ValueTy; using value_type = StringMapEntry; @@ -321,7 +306,7 @@ public: if (Bucket == getTombstoneVal()) --NumTombstones; - Bucket = MapEntryTy::Create(Key, Allocator, std::forward(Args)...); + Bucket = MapEntryTy::Create(Key, std::forward(Args)...); ++NumItems; assert(NumItems + NumTombstones <= NumBuckets); @@ -339,7 +324,7 @@ public: for (unsigned I = 0, E = NumBuckets; I != E; ++I) { StringMapEntryBase *&Bucket = TheTable[I]; if (Bucket && Bucket != getTombstoneVal()) { - static_cast(Bucket)->Destroy(Allocator); + static_cast(Bucket)->Destroy(); } Bucket = nullptr; } @@ -355,7 +340,7 @@ public: void erase(iterator I) { MapEntryTy &V = *I; remove(&V); - V.Destroy(Allocator); + V.Destroy(); } bool erase(std::string_view Key) { diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index 93e13b5bb16c..66a30698d787 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -33,22 +33,14 @@ protected: /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it /// could be reused elsewhere, maybe even taking an llvm::function_ref to /// type-erase the allocator and put it in a source file. - template static void *allocateWithKey(size_t EntrySize, size_t EntryAlign, - std::string_view Key, AllocatorTy &Allocator); -}; - -// Define out-of-line to dissuade inlining. -template -void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign, - std::string_view Key, - AllocatorTy &Allocator) { + std::string_view Key) { 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); + void *Allocation = safe_malloc(AllocSize); assert(Allocation && "Unhandled out-of-memory"); // Copy the string information. @@ -58,6 +50,7 @@ void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign, Buffer[KeyLength] = 0; // Null terminate for convenience of clients. return Allocation; } +}; /// StringMapEntryStorage - Holds the value in a StringMapEntry. /// @@ -117,11 +110,11 @@ public: /// Create a StringMapEntry for the specified key construct the value using /// \p InitiVals. - template - static StringMapEntry *Create(std::string_view key, AllocatorTy &allocator, + template + static StringMapEntry *Create(std::string_view key, InitTy &&... initVals) { return new (StringMapEntryBase::allocateWithKey( - sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator)) + sizeof(StringMapEntry), alignof(StringMapEntry), key)) StringMapEntry(key.size(), std::forward(initVals)...); } @@ -134,12 +127,10 @@ public: /// Destroy - Destroy this StringMapEntry, releasing memory back to the /// specified allocator. - template void Destroy(AllocatorTy &allocator) { + void Destroy() { // Free memory referenced by the item. - size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1; this->~StringMapEntry(); - allocator.Deallocate(static_cast(this), AllocSize, - alignof(StringMapEntry)); + std::free(static_cast(this)); } }; diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 5211f01bbd73..28d710fe69e9 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -223,13 +223,12 @@ TEST_F(StringMapTest, IterationTest) { // Test StringMapEntry::Create() method. TEST_F(StringMapTest, StringMapEntryTest) { - MallocAllocator Allocator; StringMap::value_type *entry = StringMap::value_type::Create( std::string_view(testKeyFirst, testKeyLength), 1u); EXPECT_STREQ(testKey, entry->first().data()); EXPECT_EQ(1u, entry->second); - entry->Destroy(Allocator); + entry->Destroy(); } // Test insert() method. @@ -238,7 +237,7 @@ TEST_F(StringMapTest, InsertTest) { testMap.insert( StringMap::value_type::Create( std::string_view(testKeyFirst, testKeyLength), - testMap.getAllocator(), 1u)); + 1u)); assertSingleItemMap(); } @@ -353,15 +352,14 @@ TEST_F(StringMapTest, MoveOnly) { StringMap t; t.insert(std::make_pair("Test", MoveOnly(42))); std::string_view Key = "Test"; - StringMapEntry::Create(Key, t.getAllocator(), MoveOnly(42)) - ->Destroy(t.getAllocator()); + StringMapEntry::Create(Key, MoveOnly(42)) + ->Destroy(); } TEST_F(StringMapTest, CtorArg) { std::string_view Key = "Test"; - MallocAllocator Allocator; - StringMapEntry::Create(Key, Allocator, Immovable()) - ->Destroy(Allocator); + StringMapEntry::Create(Key, Immovable()) + ->Destroy(); } TEST_F(StringMapTest, MoveConstruct) { -- 2.20.1.windows.1