Write in front: iOS underlying principle exploration is my usual development and learning in the accumulation of a section of advanced road. Record my continuous exploration of the journey, I hope to be helpful to all readers.Copy the code
The directory is as follows:
- IOS underlying principles of alloc exploration
- The underlying principles of iOS are explored
- The underlying principles of iOS explore the nature of objects & isa’s underlying implementation
- Isa-basic Principles of iOS (Part 1)
- Isa-basic Principles of iOS (Middle)
- Isa-class Basic Principles of iOS Exploration (2)
- IOS fundamentals explore the nature of Runtime Runtime & methods
- Objc_msgSend: Exploring the underlying principles of iOS
- Slow lookups in iOS Runtime
- A dynamic approach to iOS fundamentals
- The underlying principles of iOS explore the message forwarding process
- Dyld (part 1)
- IOS Basic Principles of application loading principle dyld (ii)
- IOS basic principles explore the loading of classes
- The underlying principles of iOS explore the loading of categories
- IOS underlying principles to explore the associated object
- IOS underlying principle of the wizard KVC exploration
- Exploring the underlying principles of iOS: KVO Principles | More challenges in August
- Exploring the underlying principles of iOS: Rewritten KVO | More challenges in August
- The underlying principles of iOS: Multi-threading | More challenges in August
- GCD functions and queues in iOS
- GCD principles of iOS (Part 1)
- IOS Low-level – What do you know about deadlocks?
- IOS Low-level – Singleton destruction is possible?
- IOS Low-level – Dispatch Source
- IOS bottom – a fence letter blocks the number
- IOS low-level – Be there or be Square semaphore
- IOS underlying GCD – In and out into a scheduling group
- Basic principles of iOS – Basic use of locks
- IOS underlying – @synchronized Flow analysis
- IOS low-level – The principle of lock exploration
- IOS Low-level – allows you to implement a read/write lock
- Implementation of Objective-C Block
- Implementation of Objective-C Block
- IOS bottom – Block, comprehensive resolution!
- # iOS Low-level – Startup Optimization (part 1)
Summary of the above column
- Summary of iOS underlying principles of exploration
Sort out the details
- Summary of iOS development details
preface
In the last article, we analyzed the knowledge point of starting optimization theoretically. Summarizes why binary reordering can be optimized at startup time (by grouping functions called at startup time, reducing the number of page missed interrupts); So the challenge here is how do we know which methods are called at startup time. Ok, let’s sort out the idea for today. One: locate the method in the project that is called at the time of APP launch; Second: generate order file; Third: configure binary rearrangement.
Record before optimization
finishing
The page-missing interruption was 520.31ms for 2578 times;
A page – missing interruption takes about 0.2ms
Cold start time 642.26ms
Clang peg HOOK everything
Here is a Clang13 document with a section called Tracing PCs
With Clang pegging we can track the execution of all functions, including those called at APP startup.
Add Clang peg markers
Implement the following functions:
Start and stop put the number of symbols
So stop minus 4
By calling back the data information, we can know:
- Our project has 3 * 16+5+6 * 16 * 16+5 * 16 * 16 * 16 = 22069 symbols;
In the __sanitizer_cov_trace_pc_guard callback we can get the symbol, we can save the method called at start time here, and then extract it and regenerate it into an order file. Next, we can start collecting the methods called at start time
void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
if(! *guard)return;
// The address of the previous function
void *PC = __builtin_return_address(0);
Dl_info info;
dladdr(PC, &info);
printf("%s\n", info.dli_sname);
// printf("fname-%s\nfbase-%p\nsname-%s\nsaddr%p\n\n\n\n",info.dli_fname, info.dli_fbase, info.dli_sname, info.dli_saddr);
}
Copy the code
Collect call methods
Custom storage structure
Define a symbol structure to store the symbols we want to collect:
Store node data in the __sanitizer_cov_trace_pc_guard callback:
Take out all the node data and print it out where the startup is complete
Note that there are the following problems that need to be fixed:
- The order is reversed
- The order is repeated
- The function needs to be underlined manually
Generate order file
After solving the three problems in the previous step, we can start to process and generate the order file we need.
Solve the three problems existing in the previous step:
Configure binary rearrangement
Here you can remove the Clang peg mark
Configuring the Order file
Place the order file we generated in the root path of the project, and then configure the Order File
After configuration, open link Map to build the project verification:
Perfect, same order as our order file.
The optimization effect
File Backed Page In reduced from 2578 to 688 to 145.08ms (nearly 74% less Page missing interruption)
Startup time reduced from 642.26ms to 519.45ms (cold startup increased by 19%)
The effect is still quite good!!
supplement
Configure the Clang peg callback function only within the method:
-fsanitize-coverage=func,trace-pc-guard