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:

  1. iOS App Launch Performance Optimization (1) - premain
  2. iOS App Launch Performance Optimization (2) - main
  3. iOS App Launch Performance Optimization (3) - Tools
  4. iOS App Launch Performance Optimization (4) - Principles
  5. 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:

  1. premain stage: Code executed before main function.
  2. main to first page UIViewController’s viewDidAppear.

Among them, premain stage is divided into three parts:

  1. Load automatically linked dynamic libraries, and execute 2, 3 items in dynamic libraries sequentially.
  2. Execute +load methods
  3. Execute C++ static initializers and C/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:

  1. Various Hook code (or called Swizzle Method).
  2. NSNotificationCenter, common in multi-person collaborative development code.
  3. 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