iOS App Launch Performance Optimization (1) - premain
Been working on App launch performance, also read many articles about “optimizing App launch performance”. This article summarizes articles read, plus some own understanding, nothing new. Work recently also encountered some difficulties and bottlenecks, summarize here and organize thoughts.
Planned to have five short articles:
- iOS App Launch Performance Optimization (1) - premain
- iOS App Launch Performance Optimization (2) - main
- iOS App Launch Performance Optimization (3) - Tools
- iOS App Launch Performance Optimization (4) - Principles
- iOS App Launch Performance Optimization (5) - Summary
Articles are all very short, this is the first article, don’t have high expectations
Introduction
App’s startup process can be simply divided into 2 major stages:
- premain stage: Code executed before main function.
- main to first page UIViewController’s viewDidAppear.
Among them, premain stage is divided into three parts:
- Load automatically linked dynamic libraries, and execute 2, 3 items in dynamic libraries sequentially.
- Execute
+loadmethods - Execute
C++ static initializersandC/C++ __attribute__(constructor) functions.
Since this part of code executes completely on main thread, must write carefully.
+load
Objective C’s +load method: dyld will in pre-main stage, call all Objective C classes’ +load methods in current image one by one (call order related to link-time order).
+load code generally has several types:
- Various Hook code (or called Swizzle Method).
- NSNotificationCenter, common in multi-person collaborative development code.
- Various singleton initialization code.
For +load methods, can “delete” or move to +initialize method. For NSNotificationCenter need framework to provide unified mechanism for other code to integrate, avoid everyone listening separately.
How to see all +load methods, manual method can filter Labels through hopper. As below:

How to count +load method time consumption, can reference article https://everettjf.github.io/2017/01/06/a-method-of-hook-objective-c-load/
static initializers
Precisely: C++ static initializers and C/C++ __attribute__(constructor) functions
This type of code executes after +load methods, before main method.
C++ static initializers : Easily produced in code written using C++ (or Objective C++), reference this article’s “What methods can produce initializer?”.
C/C++ __attribute__(constructor) functions : Reference code below,
__attribute__((constructor)) void calledFirst(){
// todo
}
Reference https://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work
How to see all initializers, in hopper can:

How to count this type of initializers’ time consumption, can reference article https://everettjf.github.io/2017/02/06/a-method-of-hook-static-initializers/
pre-main Alternative Solution
To achieve “auditable time consumption” for this type of code, there’s a perhaps feasible alternative solution, reference article https://everettjf.github.io/2017/03/06/a-method-of-delay-premain-code/
References
- Taobao iOS Performance Optimization Exploration
- [Toutiao iOS Client Startup Speed Optimization](https://techblog.toutiao.com/2017/01/17/iosspeed/>