[lld] [lld-macho] Implement symbol string deduplication (PR #123874)
Carlo Cabrera via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 22 00:55:31 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;
----------------
carlocab wrote:
I think something like this should work and is simpler:
```suggestion
uint32_t strx = size;
if (config->deduplicateSymbolStrings) {
llvm::CachedHashStringRef hashedStr(str);
auto it = stringMap.find(hashedStr);
if (it != stringMap.end())
return it->second;
stringMap[hashedStr] = strx;
}
strings.push_back(str);
size += str.size() + 1; // account for null terminator
```
https://github.com/llvm/llvm-project/pull/123874
More information about the llvm-commits
mailing list