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:

  1. IOS underlying principles of alloc exploration
  2. The underlying principles of iOS are explored
  3. The underlying principles of iOS explore the nature of objects & isa’s underlying implementation
  4. Isa-basic Principles of iOS (Part 1)
  5. Isa-basic Principles of iOS (Middle)
  6. Isa-class Basic Principles of iOS Exploration (2)
  7. IOS fundamentals explore the nature of Runtime Runtime & methods
  8. Objc_msgSend: Exploring the underlying principles of iOS
  9. Slow lookups in iOS Runtime
  10. A dynamic approach to iOS fundamentals
  11. The underlying principles of iOS explore the message forwarding process
  12. Dyld (part 1)
  13. IOS Basic Principles of application loading principle dyld (ii)
  14. IOS basic principles explore the loading of classes
  15. The underlying principles of iOS explore the loading of categories
  16. IOS underlying principles to explore the associated object
  17. IOS underlying principle of the wizard KVC exploration
  18. Exploring the underlying principles of iOS: KVO Principles | More challenges in August
  19. Exploring the underlying principles of iOS: Rewritten KVO | More challenges in August
  20. The underlying principles of iOS: Multi-threading | More challenges in August
  21. GCD functions and queues in iOS
  22. GCD principles of iOS (Part 1)
  23. IOS Low-level – What do you know about deadlocks?
  24. IOS Low-level – Singleton destruction is possible?
  25. IOS Low-level – Dispatch Source
  26. IOS bottom – a fence letter blocks the number
  27. IOS low-level – Be there or be Square semaphore
  28. IOS underlying GCD – In and out into a scheduling group
  29. Basic principles of iOS – Basic use of locks
  30. IOS underlying – @synchronized Flow analysis
  31. IOS low-level – The principle of lock exploration
  32. IOS Low-level – allows you to implement a read/write lock
  33. Implementation of Objective-C Block
  34. Implementation of Objective-C Block
  35. IOS bottom – Block, comprehensive resolution!
  36. # 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