summaryrefslogtreecommitdiff
path: root/src/libs/clangsupport/stringcache.h
diff options
context:
space:
mode:
authorMarco Bubke <marco.bubke@qt.io>2017-10-05 17:36:44 +0200
committerTim Jenssen <tim.jenssen@qt.io>2017-10-05 17:48:23 +0000
commitbe939a80db584aecf94fc83305cd1529f4ddb51d (patch)
treeaf5c25b466a77bd922815a67205553f50bb5aae6 /src/libs/clangsupport/stringcache.h
parentd2e15e5f1efa1ef160a2d4f40ab61180477d1f14 (diff)
downloadqt-creator-be939a80db584aecf94fc83305cd1529f4ddb51d.tar.gz
Clang: Improve locking of string cache
The string cache is only very seldom written but very often read. To improve thread scaling it is faster to lock it only for write operations. So we use a shared mutex which is locked in shared mode for read operations and locked exclusively for write operations. Change-Id: I2cc742d1b9cc15c162be40ab339fa8310640bc44 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Diffstat (limited to 'src/libs/clangsupport/stringcache.h')
-rw-r--r--src/libs/clangsupport/stringcache.h77
1 files changed, 43 insertions, 34 deletions
diff --git a/src/libs/clangsupport/stringcache.h b/src/libs/clangsupport/stringcache.h
index d3de7fe4dd..c09fd199c1 100644
--- a/src/libs/clangsupport/stringcache.h
+++ b/src/libs/clangsupport/stringcache.h
@@ -33,7 +33,7 @@
#include <utils/smallstringfwd.h>
#include <algorithm>
-#include <mutex>
+#include <shared_mutex>
#include <vector>
namespace ClangBackEnd {
@@ -54,6 +54,8 @@ public:
NonLockingMutex& operator=(const NonLockingMutex&) = delete;
void lock() {}
void unlock() {}
+ void lock_shared() {}
+ void unlock_shared() {}
};
template <typename StringType, typename IndexType>
@@ -122,23 +124,34 @@ public:
IndexType stringId(Utils::SmallStringView stringView)
{
- std::lock_guard<Mutex> lock(m_mutex);
+ std::shared_lock<Mutex> sharedLock(m_mutex);
+ Found found = find(stringView);
+
+ if (found.wasFound)
+ return found.iterator->id;
+
+ sharedLock.unlock();
+ std::lock_guard<Mutex> exclusiveLock(m_mutex);
- return ungardedStringId(stringView);
+ found = find(stringView);
+ if (!found.wasFound) {
+ IndexType index = insertString(found.iterator, stringView, IndexType(m_indices.size()));
+ found.iterator = m_strings.begin() + index;;
+ }
+
+ return found.iterator->id;
}
template <typename Container>
std::vector<IndexType> stringIds(const Container &strings)
{
- std::lock_guard<Mutex> lock(m_mutex);
-
std::vector<IndexType> ids;
ids.reserve(strings.size());
std::transform(strings.begin(),
strings.end(),
std::back_inserter(ids),
- [&] (const auto &string) { return this->ungardedStringId(string); });
+ [&] (const auto &string) { return this->stringId(string); });
return ids;
}
@@ -150,7 +163,7 @@ public:
StringType string(IndexType id) const
{
- std::lock_guard<Mutex> lock(m_mutex);
+ std::shared_lock<Mutex> sharedLock(m_mutex);
return m_strings.at(m_indices.at(id)).string;
}
@@ -158,28 +171,25 @@ public:
template<typename Function>
StringType string(IndexType id, Function storageFunction)
{
- std::lock_guard<Mutex> lock(m_mutex);
+ std::shared_lock<Mutex> sharedLock(m_mutex);
- IndexType index;
+ if (IndexType(m_indices.size()) > id && m_indices.at(id) >= 0)
+ return m_strings.at(m_indices.at(id)).string;
- if (IndexType(m_indices.size()) <= id) {
- StringType string{storageFunction(id)};
- index = insertString(find(string).iterator, string, id);
- } else {
- index = m_indices.at(id);
- if (index < 0) {
- StringType string{storageFunction(id)};
- index = insertString(find(string).iterator, string, id);
- }
- }
+ sharedLock.unlock();
+ std::lock_guard<Mutex> exclusiveLock(m_mutex);
+ IndexType index;
+
+ StringType string{storageFunction(id)};
+ index = insertString(find(string).iterator, string, id);
- return m_strings.at(index).string;
+ return m_strings[index].string;
}
std::vector<StringType> strings(const std::vector<IndexType> &ids) const
{
- std::lock_guard<Mutex> lock(m_mutex);
+ std::shared_lock<Mutex> sharedLock(m_mutex);
std::vector<StringType> strings;
strings.reserve(ids.size());
@@ -200,12 +210,21 @@ public:
template<typename Function>
IndexType stringId(Utils::SmallStringView stringView, Function storageFunction)
{
- std::lock_guard<Mutex> lock(m_mutex);
+ std::shared_lock<Mutex> sharedLock(m_mutex);
Found found = find(stringView);
- if (!found.wasFound)
- insertString(found.iterator, stringView, storageFunction(stringView));
+ if (found.wasFound)
+ return found.iterator->id;
+
+ sharedLock.unlock();
+ std::lock_guard<Mutex> exclusiveLock(m_mutex);
+
+ found = find(stringView);
+ if (!found.wasFound) {
+ IndexType index = insertString(found.iterator, stringView, storageFunction(stringView));
+ found.iterator = m_strings.begin() + index;
+ }
return found.iterator->id;
}
@@ -216,16 +235,6 @@ public:
}
private:
- IndexType ungardedStringId(Utils::SmallStringView stringView)
- {
- Found found = find(stringView);
-
- if (!found.wasFound)
- insertString(found.iterator, stringView, IndexType(m_indices.size()));
-
- return found.iterator->id;
- }
-
Found find(Utils::SmallStringView stringView)
{
return findInSorted(m_strings.cbegin(), m_strings.cend(), stringView, compare);