summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/StringSet.h
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2013-03-15 20:16:59 +0000
committerDaniel Dunbar <daniel@zuster.org>2013-03-15 20:16:59 +0000
commitdfe91cefd25614bc9ac1626d67df4d5ad5d3553f (patch)
tree9b28cdc2b33c84d6e2c63bd46e07ca5cc1694a8d /include/llvm/ADT/StringSet.h
parent1f81ebcbae0a319a9a62c3a29b4519e4ea40333e (diff)
downloadllvm-dfe91cefd25614bc9ac1626d67df4d5ad5d3553f.tar.gz
llvm-dfe91cefd25614bc9ac1626d67df4d5ad5d3553f.tar.bz2
llvm-dfe91cefd25614bc9ac1626d67df4d5ad5d3553f.tar.xz
[ADT] Fix StringSet::insert() to not allocate on every lookup.
- The previous implementation always constructed the StringMap entry, even if the key was present in the set. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@177178 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/StringSet.h')
-rw-r--r--include/llvm/ADT/StringSet.h26
1 files changed, 14 insertions, 12 deletions
diff --git a/include/llvm/ADT/StringSet.h b/include/llvm/ADT/StringSet.h
index b69a964a23..7bea577f34 100644
--- a/include/llvm/ADT/StringSet.h
+++ b/include/llvm/ADT/StringSet.h
@@ -18,23 +18,25 @@
namespace llvm {
- /// StringSet - A wrapper for StringMap that provides set-like
- /// functionality. Only insert() and count() methods are used by my
- /// code.
+ /// StringSet - A wrapper for StringMap that provides set-like functionality.
template <class AllocatorTy = llvm::MallocAllocator>
class StringSet : public llvm::StringMap<char, AllocatorTy> {
typedef llvm::StringMap<char, AllocatorTy> base;
public:
- bool insert(StringRef InLang) {
- assert(!InLang.empty());
- const char *KeyStart = InLang.data();
- const char *KeyEnd = KeyStart + InLang.size();
- llvm::StringMapEntry<char> *Entry = llvm::StringMapEntry<char>::
- Create(KeyStart, KeyEnd, base::getAllocator(), '+');
- if (!base::insert(Entry)) {
- Entry->Destroy(base::getAllocator());
+
+ /// insert - Insert the specified key into the set. If the key already
+ /// exists in the set, return false and ignore the request, otherwise insert
+ /// it and return true.
+ bool insert(StringRef Key) {
+ // Get or create the map entry for the key; if it doesn't exist the value
+ // type will be default constructed which we use to detect insert.
+ //
+ // We use '+' as the sentinel value in the map.
+ assert(!Key.empty());
+ StringMapEntry<char> &Entry = this->GetOrCreateValue(Key);
+ if (Entry.getValue() == '+')
return false;
- }
+ Entry.setValue('+');
return true;
}
};