This article as far as possible to simplify the content, write the main start to optimize the answer ideas, some relevant knowledge as far as possible to write other articles to explain.
Optimization idea
- Sorting out the startup process
- Monitor the time spent in each phase
- Solve the time consuming problem in each phase
IOS Startup Process
An overview of the
There are two stages, premain() and main()
premain()
- Load dylibs
- Rebase, Rebind (link)
- Objc (handles some OC dynamic features, including but not limited to the following)
- Oc class registration,
- Adds classification information to the relevant host class
- Selector uniqueness check
- Intializers (various operations requiring initialization, including but not limited to the following)
- The C++ compiler generates initializers to initialize those abstract DATA.
- Load method for non-lazily loaded classes in ObjC (parent class before subclass, main class before class)
- All dylib initializers
- Create c++ global static variables (I don’t know much about c++ memory layout, and I will continue to study after the review of iOS)
main()
If SceneDelegate has been applied to your project, the process will naturally change accordingly. The following is a general process description:
- Dyld call main ()
- UIApplicationMain()
- applicationWillFinishLaunching
- didFinishLaunchingWithOptions
Monitor the time-consuming
The theory is basically the above, if you want to practice the project, find the problem is the first step.
The significance of time detection
- The timing of premain and main() is mainly for debugging and fixing problems.
- Overall startup time is monitored online for real-time detection and the impact of each iteration feature update on startup time.
Personally, in 2021, it is not difficult to find information online, learn from the experience of predecessors, and solve problems oriented to optimization points. Then it is necessary to think more about how to put an end to this kind of problem, and really solve the problem for the project.
Detect premain time
DYLD_PRINT_STATISTICS was added when the Scheme environment becomes coldThe output is
Detect main() time
- The configuration project outputs the symbol table in debug mode
- Select App Launch in Instruments. Running for about 5 seconds will close the program and display the detailed startup process
Can see the stages of start-up process, can choose box didFinishLaunchingWithOptions (phase), and analyze the business function invocation chain.
Online monitoring
Online monitoring is a very important part of startup optimization. There is no monitoring of online data, just the drudgery of repeated problem identification. The online monitoring systems we know so far include Metrics (Meituan) and the App bytedance is currently responsible for, which is not comparable to Dachang Super App. For the time being, simple embedded code scheme is used. To be continued
The solution
Based on the above, list some points that can be optimized
Solution (Premain)
- Load Dylibs: Reduce the number of Dylibs that can be converted to static libraries to reduce the number of Bundler resources
- Rebase/Bind: Reduces useless classes, functions, and classes.
- Objc setup :
- Initializers: reduce load behavior time and c++ static global variables
Solution (Main)
- Find a time-consuming method with Instruments and select the background thread or wait for the first frame page to appear.
- Binary rearrangement
To be continued
The product level
To be continued