IOS startup process

Time:2021-12-4

background

How does the app work?
What happens when you click the app icon to see the home page?
How does the app start quickly?
……

Launching app to display the front page quickly will bring great experience to users. The products of tiktok and jitter series are all well done, and the author is also a deep user. This is because they have a systematic methodology and in the constant evolution, this article will elaborate the whole starting process and the core idea of quick optimization.

Loading process

Why mount?
The CPU can only process instructions and data loaded into memory

The app program we have written is compiled into an executable binary file, which contains operation instructions and data sets. It is a physical file actually stored on the mobile phone. At the beginning of application startup,The operating system creates a process for it, an IOS application has only one process. The early loading process is very violent. It directly copies the executable binary from disk to memory. With the development of hardware, the multi-user and virtual storage operating system has been invented, and the loading process has been greatly optimized. Modern operating systemPage mappingIt is widely used as a dynamic loading scheme, and the memory has been further liberated

Page mappingIs a part of the virtual storage mechanism, which stores data and instructions in memory and all disks according topageThe unit is divided into multiple pages. The unit of all loading and operation is page. Common processors generally use pages with the size of 4096 bytes. The core idea isLoad memory pages when in use, and write physical pages when not in use. Release memory pages that have not been operated for a long time when memory is tight

The execution of the program can be simply summarized into the following three steps:The executors are operating systems

  • Create a process and open up an independent virtual address space
  • Read executable binary header(IOS compilation processThis article describes the structure and function of the file header) and establishes the mapping relationship between the virtual address and the executable binary
  • Set the instruction register of the CPU to the entry address of the executable binary file and give control to the process

At the same time, the stack is switched from the kernel state to the user state, and the CPU operation permission is switched from the operating system to the process. The above is the loading process. Since then, the program starts to run. First, the dynamic linker is loaded, and then the dynamic linker is responsible for linking all dynamic libraries and executable files

Dynamic link process

Why do you need dynamic links?
The above loading process has loaded the executable binary into memory. We know that in the final stage of compilation, the static linker will link the static library and the executable machine code into a mach-o format file. It can be said that if there is no dynamic library, there is no need for the dynamic linker, and there is no dynamic linking process, So what are the shortcomings of static library? Must we design dynamic library?

  • Waste of space, during static linking, the referenced part of the static library will be copied to the executable file. If it is referenced by multiple executable files, there will be multiple redundant copies
  • Updating the deployment release is cumbersome, the dynamic library can be updated and published separately, while the static library needs to be linked into a file together with the executable program to be published

Of course, in IOS, except for system libraries such as UIKit, there is no real dynamic library. Each package needs a copy of the self-developed dynamic library. The system dynamic library and shared cache can share a dynamic library among multiple processes. The dynamic linker developed by apple is named dynamic (full name: dynamic link editor) and is integrated into the operating system

dynamic linknamelyThe process of linking dynamic libraries and executable files in memory at runtime

DyldThe source code of is open source. If you are interested, you can download it. The main process is as follows:

Rebase:
During the loading process, the executable binary has been loaded into memory, and then dyld is responsible for initializing the executable binary, recursively loading all dynamic libraries, and then rebase,The operating system will randomly open up an independent virtual address space for the process at the beginning of creation. In this way, the addresses of functions and variables need to add an offset to the starting position of the virtual address to get their real address. This process is rebase

Bind:
Variables and functions are called by the compilerSymbolIts value is the corresponding address,The symbol defined in dynamic library a cannot get the real address in dynamic library B. during compilation, the value of the symbol is filled with 0. When the dynamic link is reached, the linker will correct it in memory. This process is called relocation. The process of recursively correcting all symbol values is the bind process, which strictly depends on the symbol table

Objc setup:
Initialize objc, including registering objc class, detecting the uniqueness of selector, inserting classification method and other runtime initialization

Initializers:
All classes registered in the previous step execute the + load method, call the C + + constructor (a function decorated with attribute ((constructor)), create C + + static and global variables of non basic types, etc. (static and global variables of basic types are allocated in the executable binary during compilation, and memory is allocated directly during loading)

last:
Dyld will call the main function, main will call uiapplicationmain, uiapplicationmain will call the didfinishlaunchingwithoptions function, followed by the business process

Start optimization

Dynamic link phase

The number of dynamic libraries, objc classes, classifications, C + + constructors, C + + static and global objects. The more they are, the more time it takes to startTherefore, the reduction that can be reduced and the combination that cannot be reduced is the direction of optimization. After the main function, the following sentence:Reduce the logic of synchronous execution in the didfinishlaunchingwithoptions function

The above isdynamic linkPhase optimization. If you have done all the above things, you can try optimizationloadAt this stage, there will be a lot of benefits for large apps

Loading phase

We knowThe binary loading process of app and dynamic library will be executed in strict accordance with their static link order, the loading process will apply for physical memory through page fault, and the more interrupts, the more time-consuming. Therefore, we can optimize the startup time by combining page fault. The specific way is to obtain all symbols at startup through clang stake insertion, and then perform binary rearrangement to put the functions to be called at startup together, For details, please refer toTiktok teamandThis Taoist friendArticles

Recommended Today

Hive built-in function summary

1. Related help operation functions View built-in functions: Show functions; Display function details: desc function ABS; Display function extension information: desc function extended concat; 2. Learn the ultimate mental method of built-in function Step 1: carefully read all the functions of the show functions command to establish an overall understanding and impression Step 2: use […]