1. Page missing interrupt time before optimization (first installation)
If yes, delete the -fsanitize-coverage=func, trace-PC-guard configuration first.
Delete app and clear Xcode cache;
Xcode menu bar > Product > Profile (shortcut key command+I), wait for running to complete;
When the Instruments screen appears, select System Trace;
Start the project, stop when the first screen appears, wait for analysis,
Search for Main Thread, select Virtu Memory, and check the number and time of interrupted File Backed Page.
2. Get all function symbols loaded at startup
Delete the APP in Xcode Build Settings > Apple Clang-custom Compiler Flags > Other C Flags
Add – fsanitize – coverage = func, trace – PC – guard,
Add the following code to the first screen that appears after startup:
#import <dlfcn.h>
#import <libkern/OSAtomic.h>
// Atomic queue
static OSQueueHead symboList = OS_ATOMIC_QUEUE_INIT;
// Define the symbolic structure
typedef struct {
void * pc;
void * next;
} SymbolNode;
void __sanitizer_cov_trace_pc_guard_init(uint32_t *start, uint32_t *stop) {
static uint64_t N; // Counter for the guards.
if (start == stop || *start) return; // Initialize only once.
printf("[clang] INIT: %p %p\n", start, stop);
for (uint32_t *x = start; x < stop; x++)
*x = ++N; // Guards should start from 1.
}
void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
//if (!*guard) return; // Duplicate the guard check.
void *PC = __builtin_return_address(0);
SymbolNode * node = malloc(sizeof(SymbolNode));
*node = (SymbolNode){PC,NULL};
/ / team
// offsetof is used here to add the next node to the queue and find the position of the next pointer to the previous node
OSAtomicEnqueue(&symboList, node, offsetof(SymbolNode, next));
}
void tmpOrderFile() {
NSMutableArray<NSString *> * symbolNames = [NSMutableArray array];
while (true) {
//offsetof is to find the offsetof a property relative to a structure
SymbolNode * node = OSAtomicDequeue(&symboList, offsetof(SymbolNode, next));
if (node == NULL) break;
Dl_info info;
dladdr(node->pc, &info);
NSString * name = @(info.dli_sname);
/ / add _
BOOL isObjc = [name hasPrefix:@ "+ ["] || [name hasPrefix:@"-["];
NSString * symbolName = isObjc ? name : [@ "_" stringByAppendingString:name];
/ / to heavy
if (![symbolNames containsObject:symbolName]) {
[symbolNames addObject:symbolName];
}
}
// Kill yourself
NSString * thisFunc = [NSString stringWithFormat:@"_%s",__FUNCTION__];
if ([symbolNames containsObject:thisFunc]) {
[symbolNames removeObject:thisFunc];
}
/ / the not
NSMutableArray * symbolAry = [NSMutableArray arrayWithArray:[[symbolNames reverseObjectEnumerator] allObjects]];
NSLog(@"[clang] %@",symbolAry);
// Write the result to a file
NSString * funcString = [symbolAry componentsJoinedByString:@"\n"];
NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"lb.order"];
NSData * fileContents = [funcString dataUsingEncoding:NSUTF8StringEncoding];
BOOL result = [[NSFileManager defaultManager] createFileAtPath:filePath contents:fileContents attributes:nil];
if (result) {
NSLog(@"[clang] %@",filePath);
}else{
NSLog(Error writing to [clang] file); }}Copy the code
Then call tmpOrderFile() in viewDidAppear (or in other event methods).
After the first screen appears (or another event method fires and completes), download the file.
Click “Show package contents” to find the lb.order file in TMP. All bullet points will be loaded.
3. Rearrange the startup symbols
Copy the ld.order file to the project directory;
Search for order file in Build Setting and write ld.order file path:
Perform Step 1 again to view the optimized result.
View symbol reorder
Search for link map in Build Setting, change the Write Link map File to YES, and run it in the Intermediates. Noindex directory at the level of Products to find… # Symbols: = ld.order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order = lD. order
4. Interruption time of page missing after optimization
The interruption speed of missing pages increased by 32.44%; The overall speed was increased 46.34ms and 29.32%.