[llvm] f29c4c6 - Avoid calculating the string hash twice in GsymCreator::insertString.

Simon Giesecke via llvm-commits llvm-commits at lists.llvm.org
Wed May 19 03:33:00 PDT 2021


Author: Simon Giesecke
Date: 2021-05-19T10:06:47Z
New Revision: f29c4c60978c41914fadb064eec46086e66f07c0

URL: https://github.com/llvm/llvm-project/commit/f29c4c60978c41914fadb064eec46086e66f07c0
DIFF: https://github.com/llvm/llvm-project/commit/f29c4c60978c41914fadb064eec46086e66f07c0.diff

LOG: Avoid calculating the string hash twice in GsymCreator::insertString.

Do the single hash calculation before acquiring the lock, to reduce
lock contention. If Copy is true, and the string was not yet contained
in the StringStorage, use the new address from StringStorage, but
reuse the hash we already calculated.

Differential Revision: https://reviews.llvm.org/D102484

Added: 
    

Modified: 
    llvm/lib/DebugInfo/GSYM/GsymCreator.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp b/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
index ddce2b4b80f7..5c9c13883fb3 100644
--- a/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
+++ b/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
@@ -293,6 +293,9 @@ llvm::Error GsymCreator::finalize(llvm::raw_ostream &OS) {
 uint32_t GsymCreator::insertString(StringRef S, bool Copy) {
   if (S.empty())
     return 0;
+
+  // The hash can be calculated outside the lock.
+  CachedHashStringRef CHStr(S);
   std::lock_guard<std::recursive_mutex> Guard(Mutex);
   if (Copy) {
     // We need to provide backing storage for the string if requested
@@ -301,11 +304,11 @@ uint32_t GsymCreator::insertString(StringRef S, bool Copy) {
     // copied, but any string created by code will need to be copied.
     // This allows GsymCreator to be really fast when parsing DWARF and
     // other object files as most strings don't need to be copied.
-    CachedHashStringRef CHStr(S);
     if (!StrTab.contains(CHStr))
-      S = StringStorage.insert(S).first->getKey();
+      CHStr = CachedHashStringRef{StringStorage.insert(S).first->getKey(),
+                                  CHStr.hash()};
   }
-  return StrTab.add(S);
+  return StrTab.add(CHStr);
 }
 
 void GsymCreator::addFunctionInfo(FunctionInfo &&FI) {


        


More information about the llvm-commits mailing list