[lld] [lld-macho] Implement symbol string deduplication (PR #123874)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 22 09:36:57 PST 2025


================
@@ -1540,9 +1540,24 @@ StringTableSection::StringTableSection()
     : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
 
 uint32_t StringTableSection::addString(StringRef str) {
+  // If deduplication is disabled, just add the string
+  if (!config->deduplicateSymbolStrings) {
+    uint32_t strx = size;
+    strings.push_back(str);
+    size += str.size() + 1; // +1 for null terminator
+    return strx;
+  }
+
+  // Deduplicate strings
+  llvm::CachedHashStringRef hashedStr(str);
+  auto it = stringMap.find(hashedStr);
+  if (it != stringMap.end())
+    return it->second;
+
   uint32_t strx = size;
-  strings.push_back(str); // TODO: consider deduplicating strings
-  size += str.size() + 1; // account for null terminator
+  stringMap[hashedStr] = strx;
+  strings.push_back(str);
+  size += str.size() + 1;
----------------
alx32 wrote:

Great point - how about the new version slightly different than your suggestion -  using `try_emplace` ? 

https://github.com/llvm/llvm-project/pull/123874


More information about the llvm-commits mailing list