Skip to content

Commit 5aa6b87

Browse files
committed
[llvm-nm] Improve performance while faking symbols from function starts
By default `nm` will look into `LC_FUNCTION_STARTS` for binaries that have the flag `MH_NLIST_OUTOFSYNC_WITH_DYLDINFO` set unless `--no-dyldinfo` flag is passed. The implementation that looked for those `LC_FUNCTION_STARTS` in the symbol list was a double nested loop that checked the symbol list over and over again for each of the `LC_FUNCTION_STARTS` entries. For binaries with couple million function starts and hundreds of thousands of symbols, the double nested loop doesn't seem to finish and takes hours even in powerful machines. Instead of the nested loop, exchange time for memory and add all the addresses of the symbols into a set that can be checked then for each of the `LC_FUNCTION_STARTS` very quickly. What took hours and hours and did not seem to finish now takes less than 10 seconds. Fixes #93944
1 parent 0c2913a commit 5aa6b87

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

llvm/tools/llvm-nm/llvm-nm.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "llvm/ADT/StringSwitch.h"
19+
#include "llvm/ADT/SmallSet.h"
1920
#include "llvm/BinaryFormat/COFF.h"
2021
#include "llvm/BinaryFormat/MachO.h"
2122
#include "llvm/BinaryFormat/XCOFF.h"
@@ -1615,12 +1616,11 @@ static void dumpSymbolsFromDLInfoMachO(MachOObjectFile &MachO,
16151616
}
16161617
// See if these addresses are already in the symbol table.
16171618
unsigned FunctionStartsAdded = 0;
1619+
SmallSet<uint64_t, 32> SymbolAddresses;
1620+
for (unsigned J = 0; J < SymbolList.size(); ++J)
1621+
SymbolAddresses.insert(SymbolList[J].Address);
16181622
for (uint64_t f = 0; f < FoundFns.size(); f++) {
1619-
bool found = false;
1620-
for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
1621-
if (SymbolList[J].Address == FoundFns[f] + BaseSegmentAddress)
1622-
found = true;
1623-
}
1623+
bool found = SymbolAddresses.contains(FoundFns[f] + BaseSegmentAddress);
16241624
// See this address is not already in the symbol table fake up an
16251625
// nlist for it.
16261626
if (!found) {

0 commit comments

Comments
 (0)