As iOS projects get bigger and bigger, the startup time will become slower and slower. How can we optimize the startup time of app to give users a better experience?

Introduction to Application Startup

Application startup is divided into cold startup and hot startup

  • Cold boot means that no data related to applications is stored in the memory and the application data needs to be loaded to the memory. This process is determined by the system. Usually, we kill the application and start it immediately. At this time, the application-related data in memory will not be cleared immediately, so the startup at this time is not cold startup.
  • Hot boot indicates that data related to applications is stored in the memory. Data required for application running does not need to be loaded into the memory from the hard disk.

Monitoring startup time

We can monitor application startup time by setting environment variables. For setting environment variables, see Xcode Environment Variables

We set the environment variableDYLD_PRINT_STATISTICS, running project, printing application startup time consumption:

The application startup time is displayed as follows:

Using main function as the separation point, we divide the startup time optimization into before and after main function. The pre-main time printed here is the time before main function.

  • dylib loading time: Loading time of dynamic libraries. Apple suggests that the customized dynamic libraries in the project should not exceed 6 dynamic libraries. If more than 6 dynamic libraries should be combined;
  • rebase/binding time:The offset correction(rebase) andSymbol binding(Binding) time; Offset correction is a safety mechanismASLR, the principle is to generate a random value, add to the front of the application memory address, to play a method, function address random purpose, to solve the security problem.The memory addresses of all methods, functions, and data add this random value to the original real addressAnd the random value is different each time it is started, so that no one can get the real address of the method, function, or data; Symbolic binding is the binding of a method name to its implementation, SEL and IMP.
  • ObjC setup time: The registration time of object-c class; According to unauthoritative statistics, the time will increase by 800ms for every 20000 classes; Reducing the number of classes reduces startup time;
  • initializer time: Load method and constructor time. Try to minimize the need to rewrite the load method in the initialization.

Above is the description of startup time before main function and related optimization scheme;

For example, if you record a time in the main function and a time in the viewDidLoad method displayed on the first screen, the difference between the two times is the time taken after the main function. Since the time spent after the main function needs to be differentiated according to different projects, it cannot be generalized. Here are some suggestions for optimization

  • Reduce data loading, preferably using lazy loading, such as a third library load;
  • Remove classes and methods that are no longer used in the project;
  • Using multi-thread technology to load data, make full use of CPU resources;
  • Don’t use storyboard or XIB at startup time. Try to use pure code, because storyboard or XIB has one code-conversion operation, which is also time-consuming.

Binary rearrangement

Technical background

When the application program runs, it uses the method of combining virtual memory with physical memory to load data. Virtual memory is managed in pages. When data in a page of virtual memory is used, the corresponding real data must be loaded to the physical memory to form a mapping relationship between the virtual memory and physical memory. This operation is called page fault and takes time.

To view the loading order of iOS apps, we can set itWrite Link Map FileIs yes to view the order in which application data is loaded

Clean the project, then command+B to compile the project, go to Products xx.app directory

Go to the following directory

Find the following files

Open the file demo-linkmap-normal-x86_64. TXT

The order in which the application data is loaded is determined by two factors

File from top to bottom

Follow the order of methods in the file from top to bottom

Verify that the order of the file is the actual load order by modifying the order of the files or methods and then recompiling the file to see if the method order of the Link Map file has changed.

Binary rearrangement scheme

Since we are in the application at startup may only need some files in the method, and according to the actual load sequence, the application startup, page faults, you need to load the large amounts of data with less than, cause a lot of time consumption, therefore we only need in application startup only load startup method is ok, The actual data can be loaded later; We can do this by creating a.order file to change the loading order of methods or functions.

Open the hank.order file to see the order of the following methods

Configure this file into the corresponding Xcode project

Clean after the compilation, checkDemo-LinkMap-normal-x86_64file

In this way, we have successfully changed the loading order of methods or functions, so we only need to know which methods are executed when the application is started, and put them in a.order file, so that when the application is started, only the data it needs is loaded, reducing the number of page faults, thus reducing the startup time.

So how do you know which methods are executed when your application starts? Look at the ultimate weapon of Hook everything -Clang piling