This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

The last article talked about monitoring and measuring startup time: iOS Basics Learning-1 – Startup time Monitoring. This article talked about optimizing the startup process. As mentioned in the previous article, the initiation process can be divided into three stages:

  1. The pre – the main stage
  2. After main
  3. After rendering the first screen

Now let’s look at how to optimize time in each stage

The pre – the main stage

This phase does the following:

  1. Load executable files (collection of.o files for app)
  2. Load dynamic connection library – dylb loading
  3. Rebase pointer adjustment and bind – rebase/binding
  4. OC runtime initial processing (OC related class registration, category registration, selector uniqueness check, etc.) – Objc Setup
  5. Initialize the(to perform+load()Method,The attribute (constructor)Modifies function calls, creates C++ static global variables) -initializer

Points that can be optimized include:

  1. In dylb loading phase, dynamic library is loaded
    • This phase loads the dynamic libraries used by the app, and each dynamic library has its own dependencies, so it takes time to find and read. The system dynamic library provided by Apple has been highly optimized. For developers to define imported dynamic libraries, it takes more time. Apple’s official advice is to use custom dynamic libraries as little as possible, or to consider merging multiple dynamic libraries. One suggestion is to consider merging more than six dynamic libraries.
  2. Rebase/binding phase,
    • This is the stage where you adjust the Rebase pointer and bind the bind symbol, and this is the stage where you reduce the number of Objective-C classes, classes, and selectors in your App. This is mainly to speed up the whole dynamic link of the program, reduce the use of pointer correction in the process of dynamic library Rebase/binding, and speed up the generation of the program machine code.
  3. Objc setup phase
    • Most of the ObjC initialization is done in the Rebase/Bind phase, where DyLD registers all declared ObjC classes, inserts classes into the class’s list of methods, and checks for the uniqueness of each selector. The optimization in this stage is the same as the optimization in the last stage, the optimization in the last stage is good, the optimization in this stage is not necessary
  4. Initializers phase
    • At this stage, DyLD starts running the program’s initialization functions, calling the +load method for each Objc class and class, calling the C/C++ constructor functions (functions modified with attributes), and creating C++ static global variables of non-primitive types. After the Initializers phase is complete, Dyld starts calling main().
    • Optimizations in this phase are: 1. Reduce doing things in +load methods and postpone implementing them in Initializers. 2. 3. Reduce the number of C++ static global variables

Optimization scheme of pre-main stage

  1. Use custom dynamic libraries as little as possible, or consider merging multiple dynamic libraries. One recommendation is to consider merging more than six dynamic libraries.
  2. Reduce the number of Objective-C classes, classes, and selectors in your App
  3. Reduce doing things in +load methods and put them off in initializers
  4. Reduce the number of constructor functions and do fewer things in constructor functions
  5. Reduce the number of C++ static global variables

After the main

Optimization scheme

  1. Check method calls on the startup path for possible lags
  2. Binary rearrangement to reduce page in startup process
  3. Dynamic library lazy loading, reduce the startup process code load
  4. Background Fetch, reduce the number of cold starts

Optimize implementation details

Reduce and merge dynamic libraries

Reduce the number of classes, classes, and selectors

Reduce the number of classes

It can be divided into static scanning and online statistics.

The simplest static scan is based on AppCode, but AppCode is very slow to index when the project is large. Another type of static scan is based on Mach-O:

  • _objc_selrefs_objc_classrefsThe sel and class referenced are stored
  • __objc_classlistAll sel and class are stored

A set of differences between the two will tell you which classes /sel are not needed, but ObjC supports runtime calls that require a second check before deletion.

Another way to count useless code is to count it online. There are three main methods:

  • Viewconterler permeability can be counted by the declaration period method corresponding to HOOK
  • Class permeability, which traverses all classes at Runtime and determines whether a Class is accessed by the Objective C Runtime flag
  • Line-level permeability, which requires compile-time staking, is detrimental to packet size and execution speed.

The first two are high ROI options, and the Class level penetration is sufficient most of the time.

+ Load things into initializers

Put things in the +load method into the initializers method and add dispatch_one to prevent multiple executions

Reduce the number of constructor functions and do fewer things in constructor functions

Reduce the number of C++ static global variables

Check method calls on the startup path for possible lags

Binary rearrangement to reduce page in startup process

Get which symbols are loaded at startup. 2. Generate sort file. 3. validation

Juejin. Cn/post / 695528…

Dynamic library lazy loading, reduce the startup process code load

Juejin. Cn/post / 692150…

Background Fetch, reduce the number of cold starts

1. Set Background Fetch 2. Set suggested call frequency 3. Set the callback

Juejin. Cn/post / 684490…

Before optimization


Reference: juejin. Cn/post / 695159… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490… Mp.weixin.qq.com/s/3-Sbqe9gx… Juejin. Cn/post / 692150… Juejin. Cn/post / 695528… Juejin. Cn/post / 695528… Juejin. Cn/post / 691865… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490…