Skip to content

Commit 822a9dd

Browse files
bdashemesare
authored andcommitted
[SharedCache] Optimize parsing of slide info
Slide info is parsed and applied on the main thread, below the `SharedCache` constructor, when a shared cache is opened. Time spent applying the slide is time that the main thread is blocked. These changes eliminate some unnecessary overhead so what work remains is dominated by kernel work (paging in data, copying pages when the first modification is made). The changes are: 1. Read slide pointers via `ReadULong` rather than the variable-length `Read` method. The compiler isn't able to eliminate the call to `memcpy` in the variable-length `Read` function, and the function call overhead is noticeable given the small size of the read and number of times it is called. 2. Remove the file member from `MappingInfo`. Slide info is applied for a single file at a time so there's no reason to track the file it belongs to. This removes unnecesssary reference counting on the `std::shared_ptr` that holds the `MMappedFileAccessor`.
1 parent f68c0ef commit 822a9dd

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

view/sharedcache/core/SharedCache.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,6 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11571157
MappingInfo map;
11581158

11591159
file->Read(&map.mappingInfo, baseHeader.mappingOffset + sizeof(dyld_cache_mapping_info), sizeof(dyld_cache_mapping_info));
1160-
map.file = file;
11611160
map.slideInfoVersion = slideInfoVersion;
11621161
if (map.slideInfoVersion == 2)
11631162
file->Read(&map.slideInfoV2, slideInfoOff, sizeof(dyld_cache_slide_info_v2));
@@ -1183,7 +1182,6 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11831182
if (mappingAndSlideInfo.slideInfoFileOffset)
11841183
{
11851184
MappingInfo map;
1186-
map.file = file;
11871185
if (mappingAndSlideInfo.size == 0)
11881186
continue;
11891187
map.slideInfoVersion = file->ReadUInt32(mappingAndSlideInfo.slideInfoFileOffset);
@@ -1251,7 +1249,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
12511249
{
12521250
try
12531251
{
1254-
uint16_t start = mapping.file->ReadUShort(cursor);
1252+
uint16_t start = file->ReadUShort(cursor);
12551253
cursor += sizeof(uint16_t);
12561254
if (start == DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE)
12571255
continue;
@@ -1301,7 +1299,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13011299
uint64_t extraCursor = extrasOffset + (j * sizeof(uint16_t));
13021300
try
13031301
{
1304-
auto extra = mapping.file->ReadUShort(extraCursor);
1302+
auto extra = file->ReadUShort(extraCursor);
13051303
uint16_t aStart = extra;
13061304
uint64_t page = mapping.mappingInfo.fileOffset + (pageSize * i);
13071305
uint16_t pageStartOffset = (aStart & 0x3FFF)*4;
@@ -1340,7 +1338,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13401338
{
13411339
try
13421340
{
1343-
uint16_t delta = mapping.file->ReadUShort(cursor);
1341+
uint16_t delta = file->ReadUShort(cursor);
13441342
cursor += sizeof(uint16_t);
13451343
if (delta == DYLD_CACHE_SLIDE_V3_PAGE_ATTR_NO_REBASE)
13461344
continue;
@@ -1352,8 +1350,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13521350
loc += delta * sizeof(dyld_cache_slide_pointer3);
13531351
try
13541352
{
1355-
dyld_cache_slide_pointer3 slideInfo;
1356-
file->Read(&slideInfo, loc, sizeof(slideInfo));
1353+
dyld_cache_slide_pointer3 slideInfo = { file->ReadULong(loc) };
13571354
delta = slideInfo.plain.offsetToNextPointer;
13581355

13591356
if (slideInfo.auth.authenticated)
@@ -1395,7 +1392,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13951392
{
13961393
try
13971394
{
1398-
uint16_t delta = mapping.file->ReadUShort(cursor);
1395+
uint16_t delta = file->ReadUShort(cursor);
13991396
cursor += sizeof(uint16_t);
14001397
if (delta == DYLD_CACHE_SLIDE_V5_PAGE_ATTR_NO_REBASE)
14011398
continue;
@@ -1407,8 +1404,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
14071404
loc += delta * sizeof(dyld_cache_slide_pointer5);
14081405
try
14091406
{
1410-
dyld_cache_slide_pointer5 slideInfo;
1411-
file->Read(&slideInfo, loc, sizeof(slideInfo));
1407+
dyld_cache_slide_pointer5 slideInfo = { file->ReadULong(loc) };
14121408
delta = slideInfo.regular.next;
14131409
if (slideInfo.auth.auth)
14141410
{

view/sharedcache/core/SharedCache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ namespace SharedCacheCore {
511511

512512
struct MappingInfo
513513
{
514-
std::shared_ptr<MMappedFileAccessor> file;
515514
dyld_cache_mapping_info mappingInfo;
516515
uint32_t slideInfoVersion;
517516
dyld_cache_slide_info_v2 slideInfoV2;

0 commit comments

Comments
 (0)