Skip to content

Commit 75ea78a

Browse files
[DebugNames] Compare TableEntry names more efficiently (#79759)
TableEntry names are pointers into the string table section, and accessing their length requires a search for `\0`. However, 99% of the time we only need to compare the name against some other other, and such a comparison will fail as early as the first character. This commit adds a method to the interface of TableEntry so that such a comparison can be done without extracting the full name. It saves 10% in the time (1250ms -> 1100 ms) to evaluate the following expression. ``` lldb \ --batch \ -o "b CodeGenFunction::GenerateCode" \ -o run \ -o "expr Fn" \ -- \ clang++ -c -g test.cpp -o /dev/null &> output ```
1 parent 2492321 commit 75ea78a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,19 @@ class DWARFDebugNames : public DWARFAcceleratorTable {
543543
return StrData.getCStr(&Off);
544544
}
545545

546+
/// Compares the name of this entry against Target, returning true if they
547+
/// are equal. This is more efficient in hot code paths that do not need the
548+
/// length of the name.
549+
bool sameNameAs(StringRef Target) const {
550+
// Note: this is not the name, but the rest of debug_str starting from
551+
// name. This handles corrupt data (non-null terminated) without
552+
// overrunning the buffer.
553+
StringRef Data = StrData.getData().substr(StringOffset);
554+
size_t TargetSize = Target.size();
555+
return Data.size() > TargetSize && !Data[TargetSize] &&
556+
strncmp(Data.data(), Target.data(), TargetSize) == 0;
557+
}
558+
546559
/// Returns the offset of the first Entry in the list.
547560
uint64_t getEntryOffset() const { return EntryOffset; }
548561
};

llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() {
921921
if (Hdr.BucketCount == 0) {
922922
// No Hash Table, We need to search through all names in the Name Index.
923923
for (const NameTableEntry &NTE : *CurrentIndex) {
924-
if (NTE.getString() == Key)
924+
if (NTE.sameNameAs(Key))
925925
return NTE.getEntryOffset();
926926
}
927927
return std::nullopt;
@@ -945,7 +945,7 @@ DWARFDebugNames::ValueIterator::findEntryOffsetInCurrentIndex() {
945945
continue;
946946

947947
NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index);
948-
if (NTE.getString() == Key)
948+
if (NTE.sameNameAs(Key))
949949
return NTE.getEntryOffset();
950950
}
951951
return std::nullopt;

0 commit comments

Comments
 (0)