[Support] Make UniqueStringSaver wrap a StringSet
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 14 May 2020 15:11:44 +0000 (17:11 +0200)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 14 May 2020 15:11:44 +0000 (17:11 +0200)
This is slightly more efficient while providing exactly the same
semantics.

llvm/include/llvm/Support/StringSaver.h
llvm/lib/Support/StringSaver.cpp

index c54044e..b6b3c05 100644 (file)
@@ -9,7 +9,7 @@
 #ifndef LLVM_SUPPORT_STRINGSAVER_H
 #define LLVM_SUPPORT_STRINGSAVER_H
 
-#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Allocator.h"
@@ -36,12 +36,8 @@ public:
 ///
 /// Compared to StringSaver, it does more work but avoids saving the same string
 /// multiple times.
-///
-/// Compared to StringPool, it performs fewer allocations but doesn't support
-/// refcounting/deletion.
 class UniqueStringSaver final {
-  StringSaver Strings;
-  llvm::DenseSet<llvm::StringRef> Unique;
+  StringSet<BumpPtrAllocator &> Strings;
 
 public:
   UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {}
index f7ccfb9..999f37a 100644 (file)
@@ -19,8 +19,5 @@ StringRef StringSaver::save(StringRef S) {
 }
 
 StringRef UniqueStringSaver::save(StringRef S) {
-  auto R = Unique.insert(S);
-  if (R.second)                 // cache miss, need to actually save the string
-    *R.first = Strings.save(S); // safe replacement with equal value
-  return *R.first;
+  return Strings.insert(S).first->getKey();
 }