Skip to content

Commit 27eb30d

Browse files
committed
[SharedCache] Optimizations regarding stl container usage in SharedCache.cpp
1 parent c34be29 commit 27eb30d

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

view/sharedcache/core/SharedCache.cpp

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ void SharedCache::PerformInitialLoad()
349349
if (primaryCacheHeader.branchPoolsCount)
350350
{
351351
std::vector<uint64_t> addresses;
352+
addresses.reserve(primaryCacheHeader.branchPoolsCount);
352353
for (size_t i = 0; i < primaryCacheHeader.branchPoolsCount; i++)
353354
{
354355
addresses.push_back(baseFile->ReadULong(primaryCacheHeader.branchPoolsOffset + (i * m_dscView->GetAddressSize())));
@@ -410,7 +411,6 @@ void SharedCache::PerformInitialLoad()
410411

411412
if (primaryCacheHeader.branchPoolsCount)
412413
{
413-
std::vector<uint64_t> pool {};
414414
for (size_t i = 0; i < primaryCacheHeader.branchPoolsCount; i++)
415415
{
416416
MutableState().imageStarts["dyld_shared_cache_branch_islands_" + std::to_string(i)] =
@@ -424,6 +424,7 @@ void SharedCache::PerformInitialLoad()
424424

425425
dyld_subcache_entry2 _entry {};
426426
std::vector<dyld_subcache_entry2> subCacheEntries;
427+
subCacheEntries.reserve(subCacheCount);
427428
for (size_t i = 0; i < subCacheCount; i++)
428429
{
429430
baseFile->Read(&_entry, primaryCacheHeader.subCacheArrayOffset + (i * sizeof(dyld_subcache_entry2)),
@@ -514,7 +515,6 @@ void SharedCache::PerformInitialLoad()
514515

515516
if (primaryCacheHeader.branchPoolsCount)
516517
{
517-
std::vector<uint64_t> pool {};
518518
for (size_t i = 0; i < primaryCacheHeader.branchPoolsCount; i++)
519519
{
520520
MutableState().imageStarts["dyld_shared_cache_branch_islands_" + std::to_string(i)] =
@@ -635,7 +635,6 @@ void SharedCache::PerformInitialLoad()
635635

636636
if (primaryCacheHeader.branchPoolsCount)
637637
{
638-
std::vector<uint64_t> pool {};
639638
for (size_t i = 0; i < primaryCacheHeader.branchPoolsCount; i++)
640639
{
641640
MutableState().imageStarts["dyld_shared_cache_branch_islands_" + std::to_string(i)] =
@@ -651,6 +650,7 @@ void SharedCache::PerformInitialLoad()
651650
dyld_subcache_entry2 _entry {};
652651

653652
std::vector<dyld_subcache_entry2> subCacheEntries;
653+
subCacheEntries.reserve(subCacheCount);
654654
for (size_t i = 0; i < subCacheCount; i++)
655655
{
656656
baseFile->Read(&_entry, primaryCacheHeader.subCacheArrayOffset + (i * sizeof(dyld_subcache_entry2)),
@@ -1037,7 +1037,6 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
10371037
return;
10381038

10391039
WillMutateState();
1040-
std::vector<std::pair<uint64_t, uint64_t>> rewrites;
10411040

10421041
dyld_cache_header baseHeader;
10431042
file->Read(&baseHeader, 0, sizeof(dyld_cache_header));
@@ -1195,7 +1194,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11951194
value += slideAmount;
11961195
}
11971196
pageOffset += delta;
1198-
rewrites.emplace_back(loc, value);
1197+
file->WritePointer(loc, value);
11991198
}
12001199
catch (MappingReadException& ex)
12011200
{
@@ -1273,15 +1272,15 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
12731272
{
12741273
uint64_t value = slideInfo.auth.offsetFromSharedCacheBase;
12751274
value += mapping.slideInfoV3.auth_value_add;
1276-
rewrites.emplace_back(loc, value);
1275+
file->WritePointer(loc, value);
12771276
}
12781277
else
12791278
{
12801279
uint64_t value51 = slideInfo.plain.pointerValue;
12811280
uint64_t top8Bits = value51 & 0x0007F80000000000;
12821281
uint64_t bottom43Bits = value51 & 0x000007FFFFFFFFFF;
12831282
uint64_t value = (uint64_t)top8Bits << 13 | bottom43Bits;
1284-
rewrites.emplace_back(loc, value);
1283+
file->WritePointer(loc, value);
12851284
}
12861285
}
12871286
catch (MappingReadException& ex)
@@ -1326,12 +1325,12 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13261325
if (slideInfo.auth.auth)
13271326
{
13281327
uint64_t value = mapping.slideInfoV5.value_add + slideInfo.auth.runtimeOffset;
1329-
rewrites.emplace_back(loc, value);
1328+
file->WritePointer(loc, value);
13301329
}
13311330
else
13321331
{
13331332
uint64_t value = mapping.slideInfoV5.value_add + slideInfo.regular.runtimeOffset;
1334-
rewrites.emplace_back(loc, value);
1333+
file->WritePointer(loc, value);
13351334
}
13361335
}
13371336
catch (MappingReadException& ex)
@@ -1348,33 +1347,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13481347
}
13491348
}
13501349
}
1351-
for (const auto& [loc, value] : rewrites)
1352-
{
1353-
file->WritePointer(loc, value);
1354-
#ifdef SLIDEINFO_DEBUG_TAGS
1355-
uint64_t vmAddr = 0;
1356-
{
1357-
for (uint64_t off = baseHeader.mappingOffset; off < baseHeader.mappingOffset + baseHeader.mappingCount * sizeof(dyld_cache_mapping_info); off += sizeof(dyld_cache_mapping_info))
1358-
{
1359-
dyld_cache_mapping_info mapping;
1360-
file->Read(&mapping, off, sizeof(dyld_cache_mapping_info));
1361-
if (mapping.fileOffset <= loc && loc < mapping.fileOffset + mapping.size)
1362-
{
1363-
vmAddr = mapping.address + (loc - mapping.fileOffset);
1364-
break;
1365-
}
1366-
}
1367-
}
1368-
Ref<TagType> type = m_dscView->GetTagType("slideinfo");
1369-
if (!type)
1370-
{
1371-
m_dscView->AddTagType(new TagType(m_dscView, "slideinfo", "\xF0\x9F\x9A\x9E"));
1372-
type = m_dscView->GetTagType("slideinfo");
1373-
}
1374-
m_dscView->AddAutoDataTag(vmAddr, new Tag(type, "0x" + to_hex_string(file->ReadULong(loc)) + " => 0x" + to_hex_string(value)));
1375-
#endif
1376-
}
1377-
m_logger->LogDebug("Applied slide info for %s (0x%llx rewrites)", file->Path().c_str(), rewrites.size());
1350+
// m_logger->LogDebug("Applied slide info for %s (0x%llx rewrites)", file->Path().c_str(), rewrites.size());
13781351
file->SetSlideInfoWasApplied(true);
13791352
}
13801353

@@ -1808,6 +1781,7 @@ bool SharedCache::LoadImageWithInstallName(std::string installName, bool skipObj
18081781
reader.Seek(targetImage->headerLocation);
18091782

18101783
std::vector<MemoryRegion*> regionsToLoad;
1784+
regionsToLoad.reserve(targetImage->regions.size());
18111785

18121786
for (auto& region : targetImage->regions)
18131787
{
@@ -1852,13 +1826,7 @@ bool SharedCache::LoadImageWithInstallName(std::string installName, bool skipObj
18521826
return false;
18531827
}
18541828

1855-
std::vector<MemoryRegion*> regions;
1856-
for (auto& region : regionsToLoad)
1857-
{
1858-
regions.push_back(region);
1859-
}
1860-
1861-
SharedCache::InitializeHeader(m_dscView, vm.get(), *h, regions);
1829+
SharedCache::InitializeHeader(m_dscView, vm.get(), *h, regionsToLoad);
18621830

18631831
if (!skipObjC)
18641832
{
@@ -2594,6 +2562,8 @@ void SharedCache::InitializeHeader(
25942562
memset(&sym, 0, sizeof(sym));
25952563
auto N_TYPE = 0xE; // idk
25962564
std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>> symbolInfos;
2565+
symbolInfos.reserve(header.symtab.nsyms);
2566+
25972567
for (size_t i = 0; i < header.symtab.nsyms; i++)
25982568
{
25992569
reader->Read(&sym, header.symtab.symoff + i * sizeof(nlist_64), sizeof(nlist_64));
@@ -2658,9 +2628,9 @@ void SharedCache::InitializeHeader(
26582628
}
26592629
else
26602630
view->DefineAutoSymbol(symbolObj);
2661-
symbolInfos.push_back({sym.n_value, {type, symbol}});
2631+
symbolInfos.emplace_back(sym.n_value, std::make_pair(type, std::move(symbol)));
26622632
}
2663-
MutableState().symbolInfos[header.textBase] = symbolInfos;
2633+
MutableState().symbolInfos[header.textBase] = std::move(symbolInfos);
26642634
}
26652635

26662636
if (header.exportTriePresent && header.linkeditPresent && vm->AddressIsMapped(header.linkeditSegment.vmaddr))
@@ -2797,7 +2767,6 @@ std::vector<Ref<Symbol>> SharedCache::ParseExportTrie(std::shared_ptr<MMappedFil
27972767

27982768
try
27992769
{
2800-
// FIXME we can absolutely predict this size
28012770
std::vector<Ref<Symbol>> symbols;
28022771
auto [begin, end] = linkeditFile->ReadSpan(header.exportTrie.dataoff, header.exportTrie.datasize);
28032772
ReadExportNode(symbols, header, begin, end, begin, header.textBase, "");
@@ -2821,7 +2790,6 @@ std::shared_ptr<std::unordered_map<uint64_t, Ref<Symbol>>> SharedCache::GetExpor
28212790
}
28222791
else
28232792
{
2824-
// TODO does this have to be a functor? can't we just pass the accessor? if not, why?
28252793
std::shared_ptr<MMappedFileAccessor> linkeditFile = provideLinkeditFile();
28262794
if (!linkeditFile)
28272795
{
@@ -2830,11 +2798,12 @@ std::shared_ptr<std::unordered_map<uint64_t, Ref<Symbol>>> SharedCache::GetExpor
28302798
return nullptr;
28312799
}
28322800

2833-
auto exportList = SharedCache::ParseExportTrie(linkeditFile, header);
2801+
// FIXME: This is the only place ParseExportTrie is used, it can be optimized for the output we need here.
2802+
std::vector<Ref<Symbol>> exportList = SharedCache::ParseExportTrie(linkeditFile, header);
28342803
auto exportMapping = std::make_shared<std::unordered_map<uint64_t, Ref<Symbol>>>(exportList.size());
2835-
for (const auto& sym : exportList)
2804+
for (auto& sym : exportList)
28362805
{
2837-
exportMapping->insert_or_assign(sym->GetAddress(), sym);
2806+
exportMapping->insert_or_assign(sym->GetAddress(), std::move(sym));
28382807
}
28392808
MutableState().exportInfos.emplace(header.textBase, exportMapping);
28402809
if (didModifyExportList)
@@ -2847,6 +2816,7 @@ std::shared_ptr<std::unordered_map<uint64_t, Ref<Symbol>>> SharedCache::GetExpor
28472816
std::vector<std::string> SharedCache::GetAvailableImages()
28482817
{
28492818
std::vector<std::string> installNames;
2819+
installNames.reserve(State().headers.size());
28502820
for (const auto& header : State().headers)
28512821
{
28522822
installNames.push_back(header.second.installName);
@@ -2862,26 +2832,51 @@ std::vector<std::pair<std::string, Ref<Symbol>>> SharedCache::LoadAllSymbolsAndW
28622832
std::lock_guard initialLoadBlock(m_viewSpecificState->viewOperationsThatInfluenceMetadataMutex);
28632833

28642834
bool doSave = false;
2865-
std::vector<std::pair<std::string, Ref<Symbol>>> symbols;
2835+
std::vector<std::shared_ptr<std::unordered_map<uint64_t, Ref<Symbol>>>> exportLists;
2836+
exportLists.reserve(State().images.size());
2837+
2838+
size_t totalSymbolCount = 0;
2839+
28662840
for (const auto& img : State().images)
28672841
{
28682842
auto header = HeaderForAddress(img.headerLocation);
28692843
auto exportList = GetExportListForHeader(*header, [&]() {
2870-
try {
2871-
auto mapping = MMappedFileAccessor::Open(m_dscView, m_dscView->GetFile()->GetSessionId(), header->exportTriePath)->lock();
2872-
return mapping;
2873-
}
2874-
catch (...)
2875-
{
2876-
m_logger->LogWarn("Serious Error: Failed to open export trie %s for %s", header->exportTriePath.c_str(), header->installName.c_str());
2877-
return std::shared_ptr<MMappedFileAccessor>(nullptr);
2878-
}
2879-
}, &doSave);
2880-
if (!exportList)
2844+
try {
2845+
auto mapping = MMappedFileAccessor::Open(
2846+
m_dscView,
2847+
m_dscView->GetFile()->GetSessionId(),
2848+
header->exportTriePath
2849+
)->lock();
2850+
return mapping;
2851+
}
2852+
catch (...)
2853+
{
2854+
m_logger->LogWarn("Serious Error: Failed to open export trie %s for %s",
2855+
header->exportTriePath.c_str(),
2856+
header->installName.c_str());
2857+
return std::shared_ptr<MMappedFileAccessor>(nullptr);
2858+
}
2859+
}, &doSave);
2860+
2861+
exportLists.push_back(exportList);
2862+
if (exportList)
2863+
{
2864+
totalSymbolCount += exportList->size();
2865+
}
2866+
}
2867+
2868+
std::vector<std::pair<std::string, Ref<Symbol>>> symbols;
2869+
symbols.reserve(totalSymbolCount);
2870+
2871+
for (size_t i = 0; i < exportLists.size(); ++i)
2872+
{
2873+
if (!exportLists[i])
28812874
continue;
2882-
for (const auto& [_, symbol] : *exportList)
2875+
2876+
const auto& img = State().images[i];
2877+
for (const auto& [name, symbol] : *exportLists[i])
28832878
{
2884-
symbols.push_back({img.installName, symbol});
2879+
symbols.emplace_back(img.installName, symbol);
28852880
}
28862881
}
28872882

@@ -3144,6 +3139,7 @@ extern "C"
31443139
*count = value.size();
31453140

31463141
std::vector<const char*> cstrings;
3142+
cstrings.reserve(value.size());
31473143
for (size_t i = 0; i < value.size(); i++)
31483144
{
31493145
cstrings.push_back(value[i].c_str());
@@ -3567,7 +3563,9 @@ void SharedCache::Load(DeserializationContext& context)
35673563
{
35683564
std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>>
35693565
symbolInfos;
3570-
for (auto& si : symbolInfo["value"].GetArray())
3566+
auto symbolInfoArray = symbolInfo["value"].GetArray();
3567+
symbolInfos.reserve(symbolInfoArray.Size());
3568+
for (auto& si : symbolInfoArray)
35713569
{
35723570
symbolInfos.push_back({si["key"].GetUint64(),
35733571
{static_cast<BNSymbolType>(si["val1"].GetUint64()), si["val2"].GetString()}});

0 commit comments

Comments
 (0)