Index: lld/trunk/COFF/Symbols.h =================================================================== --- lld/trunk/COFF/Symbols.h +++ lld/trunk/COFF/Symbols.h @@ -79,7 +79,8 @@ explicit Symbol(Kind K, StringRef N = "") : SymbolKind(K), IsExternal(true), IsCOMDAT(false), WrittenToSymtab(false), PendingArchiveLoad(false), IsGCRoot(false), - IsRuntimePseudoReloc(false), Name(N) {} + IsRuntimePseudoReloc(false), NameSize(N.size()), + NameData(N.empty() ? nullptr : N.data()) {} const unsigned SymbolKind : 8; unsigned IsExternal : 1; @@ -106,7 +107,10 @@ unsigned IsRuntimePseudoReloc : 1; protected: - StringRef Name; + // Symbol name length. Assume symbol lengths fit in a 32-bit integer. + uint32_t NameSize; + + const char *NameData; }; // The base class for any defined symbols, including absolute symbols, @@ -129,7 +133,7 @@ // Symbols defined via a COFF object file or bitcode file. For COFF files, this // stores a coff_symbol_generic*, and names of internal symbols are lazily // loaded through that. For bitcode files, Sym is nullptr and the name is stored -// as a StringRef. +// as a decomposed StringRef. class DefinedCOFF : public Defined { friend Symbol; Index: lld/trunk/COFF/Symbols.cpp =================================================================== --- lld/trunk/COFF/Symbols.cpp +++ lld/trunk/COFF/Symbols.cpp @@ -20,6 +20,9 @@ using namespace lld::coff; +static_assert(sizeof(SymbolUnion) <= 48, + "symbols should be optimized for memory usage"); + // Returns a symbol name for an error message. std::string lld::toString(coff::Symbol &B) { if (Config->Demangle) @@ -39,11 +42,15 @@ // name. Object files contain lots of non-external symbols, and creating // StringRefs for them (which involves lots of strlen() on the string table) // is a waste of time. - if (Name.empty()) { + if (NameData == nullptr) { auto *D = cast(this); - cast(D->File)->getCOFFObj()->getSymbolName(D->Sym, Name); + StringRef NameStr; + cast(D->File)->getCOFFObj()->getSymbolName(D->Sym, NameStr); + NameData = NameStr.data(); + NameSize = NameStr.size(); + assert(NameSize == NameStr.size() && "name length truncated"); } - return Name; + return StringRef(NameData, NameSize); } InputFile *Symbol::getFile() { @@ -67,9 +74,10 @@ // MinGW specific. void Symbol::replaceKeepingName(Symbol *Other, size_t Size) { - StringRef OrigName = Name; + StringRef OrigName = getName(); memcpy(this, Other, Size); - Name = OrigName; + NameData = OrigName.data(); + NameSize = OrigName.size(); } COFFSymbolRef DefinedCOFF::getCOFFSymbol() {