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

preface

For those of you who haven’t seen the first four installments, don’t worry, just post the current main demo code here

We mainly did the following things:

  • Declared ablock, a c functionfunc(), an OC function-(void)test, aSwiftWritten to contain class methodsswiftTestThe class of
  • intouchesBegan:withEvent:Method is called-(void)test.-(void)testNested calls to the above methods will eventually print a stringtest
  • Add methods for compilation – time staking__sanitizer_cov_trace_pc_guard_initand__sanitizer_cov_trace_pc_guard
  • Information such as method symbols can be obtained through related methods of pile-time, as shown in the code

Collect symbol

Focus 1 Multithreading

The related methods that start may be executed in different threads, and if we collect these symbols directly in an array, we have threading problems.

When I hear about multithreading, I immediately think of locking, but locking is not recommended because of its high performance cost. Atomic queues are recommended to solve this problem.

Atomic queue is stack structure, order is guaranteed by queue structure + atomicity.

Import header file

#import <libkern/OSAtomic.h>
Copy the code

When the method is started, the PC retrieved by __sanitizer_cov_trace_pc_guard is stored as a linked list of Node member variables.

When we click on the screen, touchesBegan:withEvent: It pulls out the node in reverse order, generates Dl_info from the member variable PC inside the node, and reads the method symbol from info. The method execution order of the final record is positive order by inserting it into the head of the method symbol array ARR.

Let’s run the code

-[ViewController touchesBegan:withEvent:]

Focus 2 handles the While loop

Breakpoint viewing assembly

__sanitizer_cov_trace_pc_guard is jumping repeatedly?

The documentation states that __sanitizer_cov_trace_pc_guard is inserted at every edge level, so each while loop should count as one edge!

Solution: Change to only hook function, Target -> Build Setting -> Custom Complier Flags -> Other C Flags change to -fsanitize-coverage=func,trace-pc-guard

Run the code again

At this point, we’ve got the correct method notation.

Focus 3 handles the load function

After the load method is added to the current class, I look at the output and find that the load is not printed.

When the load method is called, the __sanitizer_cov_trace_pc_guard parameter is inserted with guard of 0. The default implementation of the function will return directly, resulting in the failure to capture the load.

Disables if (! In __sanitizer_cov_trace_pc_guard. *guard) return; Can be

All method symbols related to startup were successfully collected.

Focus 3 missing symbol processing

We see that the function symbols in the output are missing the previously declared functions (test, func, swiftTest) and block because I didn’t call them.

Only called functions are caught by __sanitizer_cov_trace_pc_guard.

Let’s call it in viewDidLoad

Focus 4 processingC functionsandblockThe symbol of

C functions and blocks also need special processing before generating Order files.

You can see in objC-750’s Order file that both symbols begin with an underscore “_”. And what we get is not underlined, so we have to concatenate it.

To generate the Order File

Generating an Order File is to concatenate the above processed collection of method symbols into a string and write it to a File.

Remember to remove the ‘touchesBegan:withEvent’ symbol triggered by the click.

Take a look at our Order File

Perfect ~ just put the Order File into the project directory and set the path, then complete the implementation of boot optimization ~

~ 🎉🎉🎉🎉 doctor


Do not love the principle, directly open dry fat friends

See optimization effect comparison of fat friends

Please move to iOS App launch optimization (VI) : Utility party directly see here