Skip to content

Commit b24a304

Browse files
authored
[lld-macho] Always store symbol name length eagerly (NFC) (#106906)
The only instance where we weren't already passing a `StringRef` with a known length to `Symbol`'s constructor is where the argument is a string literal. Even in that case, lazy `strlen` calls don't make sense, as the compiler can constant-evaluate the `StringRef(const char*)` constructor. For symbols that go into the symbol table we need the length when calculating the hash anyway. We could get away with not calling `getName()` for local symbols, but the total contribution of `strlen` to the run time is already below 1%, so that would just complicate the code for a negligible benefit.
1 parent 1c874bb commit b24a304

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

lld/MachO/Symbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ uint64_t Symbol::getLazyPtrVA() const {
5252
uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
5353
uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
5454
55-
Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec,
55+
Defined::Defined(StringRef name, InputFile *file, InputSection *isec,
5656
uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,
5757
bool isPrivateExtern, bool includeInSymtab,
5858
bool isReferencedDynamically, bool noDeadStrip,

lld/MachO/Symbols.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ namespace macho {
2121

2222
class MachHeaderSection;
2323

24-
struct StringRefZ {
25-
StringRefZ(const char *s) : data(s), size(-1) {}
26-
StringRefZ(StringRef s) : data(s.data()), size(s.size()) {}
27-
28-
const char *data;
29-
const uint32_t size;
30-
};
31-
3224
class Symbol {
3325
public:
3426
enum Kind {
@@ -45,11 +37,7 @@ class Symbol {
4537

4638
Kind kind() const { return symbolKind; }
4739

48-
StringRef getName() const {
49-
if (nameSize == (uint32_t)-1)
50-
nameSize = strlen(nameData);
51-
return {nameData, nameSize};
52-
}
40+
StringRef getName() const { return {nameData, nameSize}; }
5341

5442
bool isLive() const { return used; }
5543
bool isLazy() const {
@@ -96,15 +84,15 @@ class Symbol {
9684
InputFile *getFile() const { return file; }
9785

9886
protected:
99-
Symbol(Kind k, StringRefZ name, InputFile *file)
100-
: symbolKind(k), nameData(name.data), file(file), nameSize(name.size),
87+
Symbol(Kind k, StringRef name, InputFile *file)
88+
: symbolKind(k), nameData(name.data()), file(file), nameSize(name.size()),
10189
isUsedInRegularObj(!file || isa<ObjFile>(file)),
10290
used(!config->deadStrip) {}
10391

10492
Kind symbolKind;
10593
const char *nameData;
10694
InputFile *file;
107-
mutable uint32_t nameSize;
95+
uint32_t nameSize;
10896

10997
public:
11098
// True if this symbol was referenced by a regular (non-bitcode) object.
@@ -116,7 +104,7 @@ class Symbol {
116104

117105
class Defined : public Symbol {
118106
public:
119-
Defined(StringRefZ name, InputFile *file, InputSection *isec, uint64_t value,
107+
Defined(StringRef name, InputFile *file, InputSection *isec, uint64_t value,
120108
uint64_t size, bool isWeakDef, bool isExternal, bool isPrivateExtern,
121109
bool includeInSymtab, bool isReferencedDynamically, bool noDeadStrip,
122110
bool canOverrideWeakDef = false, bool isWeakDefCanBeHidden = false,
@@ -206,7 +194,7 @@ enum class RefState : uint8_t { Unreferenced = 0, Weak = 1, Strong = 2 };
206194

207195
class Undefined : public Symbol {
208196
public:
209-
Undefined(StringRefZ name, InputFile *file, RefState refState,
197+
Undefined(StringRef name, InputFile *file, RefState refState,
210198
bool wasBitcodeSymbol)
211199
: Symbol(UndefinedKind, name, file), refState(refState),
212200
wasBitcodeSymbol(wasBitcodeSymbol) {
@@ -238,7 +226,7 @@ class Undefined : public Symbol {
238226
// to regular defined symbols in a __common section.
239227
class CommonSymbol : public Symbol {
240228
public:
241-
CommonSymbol(StringRefZ name, InputFile *file, uint64_t size, uint32_t align,
229+
CommonSymbol(StringRef name, InputFile *file, uint64_t size, uint32_t align,
242230
bool isPrivateExtern)
243231
: Symbol(CommonKind, name, file), size(size),
244232
align(align != 1 ? align : llvm::PowerOf2Ceil(size)),
@@ -255,7 +243,7 @@ class CommonSymbol : public Symbol {
255243

256244
class DylibSymbol : public Symbol {
257245
public:
258-
DylibSymbol(DylibFile *file, StringRefZ name, bool isWeakDef,
246+
DylibSymbol(DylibFile *file, StringRef name, bool isWeakDef,
259247
RefState refState, bool isTlv)
260248
: Symbol(DylibKind, name, file), shouldReexport(false),
261249
refState(refState), weakDef(isWeakDef), tlv(isTlv) {
@@ -301,6 +289,7 @@ class DylibSymbol : public Symbol {
301289
}
302290

303291
bool shouldReexport : 1;
292+
304293
private:
305294
RefState refState : 2;
306295
const bool weakDef : 1;

0 commit comments

Comments
 (0)