Skip to content

Commit b31e755

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 50c5dc4 commit b31e755

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
@@ -1156,7 +1156,6 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11561156
MappingInfo map;
11571157

11581158
file->Read(&map.mappingInfo, baseHeader.mappingOffset + sizeof(dyld_cache_mapping_info), sizeof(dyld_cache_mapping_info));
1159-
map.file = file;
11601159
map.slideInfoVersion = slideInfoVersion;
11611160
if (map.slideInfoVersion == 2)
11621161
file->Read(&map.slideInfoV2, slideInfoOff, sizeof(dyld_cache_slide_info_v2));
@@ -1182,7 +1181,6 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
11821181
if (mappingAndSlideInfo.slideInfoFileOffset)
11831182
{
11841183
MappingInfo map;
1185-
map.file = file;
11861184
if (mappingAndSlideInfo.size == 0)
11871185
continue;
11881186
map.slideInfoVersion = file->ReadUInt32(mappingAndSlideInfo.slideInfoFileOffset);
@@ -1250,7 +1248,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
12501248
{
12511249
try
12521250
{
1253-
uint16_t start = mapping.file->ReadUShort(cursor);
1251+
uint16_t start = file->ReadUShort(cursor);
12541252
cursor += sizeof(uint16_t);
12551253
if (start == DYLD_CACHE_SLIDE_PAGE_ATTR_NO_REBASE)
12561254
continue;
@@ -1300,7 +1298,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13001298
uint64_t extraCursor = extrasOffset + (j * sizeof(uint16_t));
13011299
try
13021300
{
1303-
auto extra = mapping.file->ReadUShort(extraCursor);
1301+
auto extra = file->ReadUShort(extraCursor);
13041302
uint16_t aStart = extra;
13051303
uint64_t page = mapping.mappingInfo.fileOffset + (pageSize * i);
13061304
uint16_t pageStartOffset = (aStart & 0x3FFF)*4;
@@ -1339,7 +1337,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13391337
{
13401338
try
13411339
{
1342-
uint16_t delta = mapping.file->ReadUShort(cursor);
1340+
uint16_t delta = file->ReadUShort(cursor);
13431341
cursor += sizeof(uint16_t);
13441342
if (delta == DYLD_CACHE_SLIDE_V3_PAGE_ATTR_NO_REBASE)
13451343
continue;
@@ -1351,8 +1349,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13511349
loc += delta * sizeof(dyld_cache_slide_pointer3);
13521350
try
13531351
{
1354-
dyld_cache_slide_pointer3 slideInfo;
1355-
file->Read(&slideInfo, loc, sizeof(slideInfo));
1352+
dyld_cache_slide_pointer3 slideInfo = { file->ReadULong(loc) };
13561353
delta = slideInfo.plain.offsetToNextPointer;
13571354

13581355
if (slideInfo.auth.authenticated)
@@ -1394,7 +1391,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
13941391
{
13951392
try
13961393
{
1397-
uint16_t delta = mapping.file->ReadUShort(cursor);
1394+
uint16_t delta = file->ReadUShort(cursor);
13981395
cursor += sizeof(uint16_t);
13991396
if (delta == DYLD_CACHE_SLIDE_V5_PAGE_ATTR_NO_REBASE)
14001397
continue;
@@ -1406,8 +1403,7 @@ void SharedCache::ParseAndApplySlideInfoForFile(std::shared_ptr<MMappedFileAcces
14061403
loc += delta * sizeof(dyld_cache_slide_pointer5);
14071404
try
14081405
{
1409-
dyld_cache_slide_pointer5 slideInfo;
1410-
file->Read(&slideInfo, loc, sizeof(slideInfo));
1406+
dyld_cache_slide_pointer5 slideInfo = { file->ReadULong(loc) };
14111407
delta = slideInfo.regular.next;
14121408
if (slideInfo.auth.auth)
14131409
{

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)