[Lldb-commits] [PATCH] D32306: Remove lock from ConstString::GetLength

Scott Smith via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 20 11:44:28 PDT 2017


scott.smith created this revision.

ConstStrings are immutable, so there is no need to grab even a reader lock in order to read the length field.


Repository:
  rL LLVM

https://reviews.llvm.org/D32306

Files:
  source/Utility/ConstString.cpp


Index: source/Utility/ConstString.cpp
===================================================================
--- source/Utility/ConstString.cpp
+++ source/Utility/ConstString.cpp
@@ -42,10 +42,10 @@
     return *reinterpret_cast<StringPoolEntryType *>(ptr);
   }
 
-  size_t GetConstCStringLength(const char *ccstr) const {
+  static size_t GetConstCStringLength(const char *ccstr) {
     if (ccstr != nullptr) {
-      const uint8_t h = hash(llvm::StringRef(ccstr));
-      llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
+      // Since the entry is read only, and we derive the entry entirely from the
+      // pointer, we don't need the lock.
       const StringPoolEntryType &entry = GetStringMapEntryFromKeyData(ccstr);
       return entry.getKey().size();
     }
@@ -218,10 +218,8 @@
   if (m_string == rhs.m_string)
     return false;
 
-  llvm::StringRef lhs_string_ref(m_string,
-                                 StringPool().GetConstCStringLength(m_string));
-  llvm::StringRef rhs_string_ref(
-      rhs.m_string, StringPool().GetConstCStringLength(rhs.m_string));
+  llvm::StringRef lhs_string_ref(GetStringRef());
+  llvm::StringRef rhs_string_ref(rhs.GetStringRef());
 
   // If both have valid C strings, then return the comparison
   if (lhs_string_ref.data() && rhs_string_ref.data())
@@ -240,7 +238,7 @@
 }
 
 size_t ConstString::GetLength() const {
-  return StringPool().GetConstCStringLength(m_string);
+  return Pool::GetConstCStringLength(m_string);
 }
 
 bool ConstString::Equals(const ConstString &lhs, const ConstString &rhs,
@@ -255,10 +253,8 @@
     return false;
 
   // perform case insensitive equality test
-  llvm::StringRef lhs_string_ref(
-      lhs.m_string, StringPool().GetConstCStringLength(lhs.m_string));
-  llvm::StringRef rhs_string_ref(
-      rhs.m_string, StringPool().GetConstCStringLength(rhs.m_string));
+  llvm::StringRef lhs_string_ref(lhs.GetStringRef());
+  llvm::StringRef rhs_string_ref(rhs.GetStringRef());
   return lhs_string_ref.equals_lower(rhs_string_ref);
 }
 
@@ -270,10 +266,8 @@
   if (lhs_cstr == rhs_cstr)
     return 0;
   if (lhs_cstr && rhs_cstr) {
-    llvm::StringRef lhs_string_ref(
-        lhs_cstr, StringPool().GetConstCStringLength(lhs_cstr));
-    llvm::StringRef rhs_string_ref(
-        rhs_cstr, StringPool().GetConstCStringLength(rhs_cstr));
+    llvm::StringRef lhs_string_ref(lhs.GetStringRef());
+    llvm::StringRef rhs_string_ref(rhs.GetStringRef());
 
     if (case_sensitive) {
       return lhs_string_ref.compare(rhs_string_ref);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32306.95995.patch
Type: text/x-patch
Size: 2543 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20170420/b8afe222/attachment-0001.bin>


More information about the lldb-commits mailing list