From 0ca8f5c6554d0bace18b72d705cb0ba3e4203c5d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 6 Jul 2013 16:58:17 -0700 Subject: [PATCH] Improve performance of ScanForFunctions(). Mostly affects games with a lot of modules and exports. --- Core/Debugger/SymbolMap.cpp | 39 +++++++++++++++++++++++++++++++++++++ Core/Debugger/SymbolMap.h | 9 +++++++++ Core/MIPS/MIPSAnalyst.cpp | 6 +++--- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index 83adaa5706..5c44418531 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -112,6 +112,7 @@ void SymbolMap::ResetSymbolMap() #endif entries.clear(); uniqueEntries.clear(); + entryRanges.clear(); } @@ -127,6 +128,7 @@ void SymbolMap::AddSymbol(const char *symbolname, unsigned int vaddress, size_t { entries.push_back(e); uniqueEntries.insert((const MapEntryUniqueInfo)e); + entryRanges[e.vaddress + e.size] = e.vaddress; } } @@ -193,6 +195,7 @@ bool SymbolMap::LoadSymbolMap(const char *filename) e.UndecorateName(); entries.push_back(e); uniqueEntries.insert((const MapEntryUniqueInfo)e); + entryRanges[e.vaddress + e.size] = e.vaddress; } } fclose(f); @@ -238,6 +241,41 @@ int SymbolMap::GetSymbolNum(unsigned int address, SymbolType symmask) const return -1; } +bool SymbolMap::GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask) const +{ + // entryRanges is indexed by end. The first entry after address should contain address. + // Otherwise, we have no entry that contains it, unless things overlap (which they shouldn't.) + const auto containingEntry = entryRanges.upper_bound(address); + if (containingEntry == entryRanges.end()) + return false; + + // The next most common case is a single symbol by start address. + // So we optimize for that by looking in our uniqueEntry set. + u32 start_address = containingEntry->second; + if (start_address <= address) + { + const MapEntryUniqueInfo searchKey = {start_address, start_address}; + const auto entry = uniqueEntries.find(searchKey); + if (entry != uniqueEntries.end() && (entry->type & symmask) != 0) + { + info->address = entry->vaddress; + info->size = entry->size; + return true; + } + } + + // Fall back to a slower scan. + int n = GetSymbolNum(address, symmask); + if (n != -1) + { + info->address = GetSymbolAddr(n); + info->size = GetSymbolSize(n); + return true; + } + + return false; +} + const char* SymbolMap::getDirectSymbol(u32 address) { for (auto it = entries.begin(), end = entries.end(); it != end; ++it) @@ -572,6 +610,7 @@ void SymbolMap::UseFuncSignaturesFile(const char *filename, u32 maxAddress) addr+=sig->size-4; //don't need to check function interior entries.push_back(e); uniqueEntries.insert((const MapEntryUniqueInfo)e); + entryRanges[e.vaddress + e.size] = e.vaddress; break; } sig++; diff --git a/Core/Debugger/SymbolMap.h b/Core/Debugger/SymbolMap.h index 6ebe949d1c..c29494ed39 100644 --- a/Core/Debugger/SymbolMap.h +++ b/Core/Debugger/SymbolMap.h @@ -20,6 +20,7 @@ #include "../../Globals.h" #include #include +#include enum SymbolType { @@ -27,6 +28,12 @@ enum SymbolType ST_DATA=2 }; +struct SymbolInfo +{ + u32 address; + u32 size; +}; + #ifdef _WIN32 struct HWND__; typedef struct HWND__ *HWND; @@ -42,6 +49,7 @@ public: void ResetSymbolMap(); void AnalyzeBackwards(); int GetSymbolNum(unsigned int address, SymbolType symmask=ST_FUNCTION) const; + bool GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask = ST_FUNCTION) const; const char *GetDescription(unsigned int address) const; #ifdef _WIN32 void FillSymbolListBox(HWND listbox, SymbolType symmask=ST_FUNCTION) const; @@ -96,6 +104,7 @@ private: std::set uniqueEntries; std::vector entries; + std::map entryRanges; }; extern SymbolMap symbolMap; diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index 969dad5cfe..bd395a6e9d 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -351,10 +351,10 @@ namespace MIPSAnalyst u32 addr; for (addr = startAddr; addr<=endAddr; addr+=4) { - int n = symbolMap.GetSymbolNum(addr,ST_FUNCTION); - if (n != -1) + SymbolInfo syminfo; + if (symbolMap.GetSymbolInfo(&syminfo, addr, ST_FUNCTION)) { - addr = symbolMap.GetSymbolAddr(n) + symbolMap.GetSymbolSize(n); + addr = syminfo.address + syminfo.size; continue; }