IOS App startup optimization (I) : Detect startup time
IOS App startup Optimization (2) : Physical memory and virtual memory
IOS App startup optimization (3) : binary rearrangement
IOS App startup optimization (4) : pile && get method symbol at compile time
IOS App startup optimization (5) : Collect symbols && generate Order File
IOS App launch optimization (vi) : Utility party directly see here
Effect of contrast
Before optimization
The optimized
Step 1: Add the Build Setting
Target -> Build Setting -> Custom Complier Flags ->
Other C Flags Added
-fsanitize-coverage=func,trace-pc-guard
Other Swift Flags Added
-sanitize-coverage=func
-sanitize=undefined
Step 2: Add code
Add it to the ViewController that started the first time.
#import "dlfcn.h"
#import <libkern/OSAtomic.h>
Copy the code
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("INIT: %p %p\n", start, stop);
for(uint32_t *x = start; x < stop; x++) *x = ++N; Guards should start from 1.} static OSQueueHead list = OS_ATOMIC_QUEUE_INIT; Typedef struct {void * PC; // save the obtained PC void *next; // point to the next Node} Node; void __sanitizer_cov_trace_pc_guard(uint32_t *guard) { void *PC = __builtin_return_address(0); Node *node = malloc(sizeof(Node)); *node = (Node){PC, NULL}; OSAtomicEnqueue(&list, node, offsetof(node, next)); } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSMutableArray *arr = [NSMutableArray array];whileOSAtomicEnqueue(&list, offsetof(Node, next)); // Exit mechanismif (node == NULL) {
break; } // get function information Dl_info info; dladdr(node->pc, &info); NSString *sname = [NSString stringWithCString:info.dli_sname encoding:NSUTF8StringEncoding];printf("%s \n", info.dli_sname); BOOL isObjc = [sname hasPrefix:@"+ ["] || [sname hasPrefix:@"-"]; Sname = isObjc? sname: [@"_"stringByAppendingString:sname]; / / to heavyif(! [arr insersobject :sname]) {arr insertObject:sname atIndex:0]; [arr insertObject:sname atIndex:0]; [arr removeObject:[NSString stringWithFormat:@]"%s",__FUNCTION__]]; / / string array synthesis nsstrings * funcStr = [arr componentsJoinedByString: @"\n"]; / / write file nsstrings * filePath = [NSTemporaryDirectory () stringByAppendingPathComponent: @"link.order"];
NSData * fileContents = [funcStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"% @", filePath);
[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileContents attributes:nil];
}
Copy the code
Step 3: Fetch the Order File
- The code in Step two
NSLog(@"%@", filePath);
The breakpoint - If the page doesn’t trigger a click,
viewDidLoad
It callstouchesBegan:withEvent:
Can also be - Record after running the code
link.order
The path of the Finder
Access pathorder file
Step 4: Set the order file
- the
link.order
To the project root directory Target -> Build Setting -> Linking -> Order File
Set the path
Step 5: Compile the code
- The first step
order file
Setting restore - Delete the code added in step 2
clean
Later compiled code