Skip to content

Commit f14f2ef

Browse files
Module: Hash functions only once during loading.
This fixes the loading speed regression from hrydgard#10501.
1 parent 473c872 commit f14f2ef

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Core/HLE/sceKernelModule.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ static Module *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 loadAdd
12681268
scan = g_Config.bFuncReplacements;
12691269
#endif
12701270

1271-
bool gotSymbols = scan && reader.LoadSymbols();
1271+
bool insertSymbols = scan && !reader.LoadSymbols();
12721272
std::vector<SectionID> codeSections = reader.GetCodeSections();
12731273
for (SectionID id : codeSections) {
12741274
u32 start = reader.GetSectionAddr(id);
@@ -1280,8 +1280,9 @@ static Module *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 loadAdd
12801280
if (end > module->textEnd)
12811281
module->textEnd = end;
12821282

1283-
if (scan)
1284-
MIPSAnalyst::ScanForFunctions(start, end, !gotSymbols);
1283+
if (scan) {
1284+
MIPSAnalyst::ScanForFunctions(start, end, insertSymbols);
1285+
}
12851286
}
12861287

12871288
// Some games don't have any sections at all.
@@ -1290,14 +1291,18 @@ static Module *__KernelLoadELFFromPtr(const u8 *ptr, size_t elfSize, u32 loadAdd
12901291
u32 scanEnd = module->textEnd;
12911292
// Skip the exports and imports sections, they're not code.
12921293
if (scanEnd >= std::min(modinfo->libent, modinfo->libstub)) {
1293-
MIPSAnalyst::ScanForFunctions(scanStart, std::min(modinfo->libent, modinfo->libstub) - 4, !gotSymbols);
1294+
MIPSAnalyst::ScanForFunctions(scanStart, std::min(modinfo->libent, modinfo->libstub) - 4, insertSymbols);
12941295
scanStart = std::min(modinfo->libentend, modinfo->libstubend);
12951296
}
12961297
if (scanEnd >= std::max(modinfo->libent, modinfo->libstub)) {
1297-
MIPSAnalyst::ScanForFunctions(scanStart, std::max(modinfo->libent, modinfo->libstub) - 4, !gotSymbols);
1298+
MIPSAnalyst::ScanForFunctions(scanStart, std::max(modinfo->libent, modinfo->libstub) - 4, insertSymbols);
12981299
scanStart = std::max(modinfo->libentend, modinfo->libstubend);
12991300
}
1300-
MIPSAnalyst::ScanForFunctions(scanStart, scanEnd, !gotSymbols);
1301+
MIPSAnalyst::ScanForFunctions(scanStart, scanEnd, insertSymbols);
1302+
}
1303+
1304+
if (scan) {
1305+
MIPSAnalyst::FinalizeScan(insertSymbols);
13011306
}
13021307
}
13031308

Core/MIPS/MIPSAnalyst.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,16 @@ namespace MIPSAnalyst {
878878

879879
for (auto iter = functions.begin(), end = functions.end(); iter != end; iter++) {
880880
AnalyzedFunction &f = *iter;
881+
if (Memory::IsValidRange(f.start, f.end - f.start + 4)) {
882+
continue;
883+
}
881884

882885
// This is unfortunate. In case of emuhacks or relocs, we have to make a copy.
883886
buffer.resize((f.end - f.start + 4) / 4);
884887
size_t pos = 0;
885888
for (u32 addr = f.start; addr <= f.end; addr += 4) {
886889
u32 validbits = 0xFFFFFFFF;
887-
MIPSOpcode instr = Memory::Read_Instruction(addr, true);
890+
MIPSOpcode instr = Memory::ReadUnchecked_Instruction(addr, true);
888891
if (MIPS_IS_EMUHACK(instr)) {
889892
f.hasHash = false;
890893
goto skip;
@@ -1015,7 +1018,7 @@ namespace MIPSAnalyst {
10151018
return furthestJumpbackAddr;
10161019
}
10171020

1018-
void ScanForFunctions(u32 startAddr, u32 endAddr, bool insertSymbols) {
1021+
void ScanForFunctions(u32 startAddr, u32 endAddr, bool &insertSymbols) {
10191022
std::lock_guard<std::recursive_mutex> guard(functions_lock);
10201023

10211024
AnalyzedFunction currentFunction = {startAddr};
@@ -1163,7 +1166,9 @@ namespace MIPSAnalyst {
11631166
g_symbolMap->AddFunction(DefaultFunctionName(temp, iter->start), iter->start, iter->end - iter->start + 4);
11641167
}
11651168
}
1169+
}
11661170

1171+
void FinalizeScan(bool insertSymbols) {
11671172
HashFunctions();
11681173

11691174
std::string hashMapFilename = GetSysDirectory(DIRECTORY_SYSTEM) + "knownfuncs.ini";

Core/MIPS/MIPSAnalyst.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ namespace MIPSAnalyst
105105
// If we have loaded symbols from the elf, we'll register functions as they are touched
106106
// so that we don't just dump them all in the cache.
107107
void RegisterFunction(u32 startAddr, u32 size, const char *name);
108-
void ScanForFunctions(u32 startAddr, u32 endAddr, bool insertSymbols);
108+
void ScanForFunctions(u32 startAddr, u32 endAddr, bool &insertSymbols);
109+
void FinalizeScan(bool insertSymbols);
109110
void ForgetFunctions(u32 startAddr, u32 endAddr);
110111
void PrecompileFunctions();
111112
void PrecompileFunction(u32 startAddr, u32 length);

0 commit comments

Comments
 (0)