IOS App startup optimization (I) : Detect startup time

IOS App startup Optimization (2) : Physical memory and virtual memory

IOS App startup optimization (3) : binary rearrangement

IOS App startup optimization (4) : pile && get method symbol at compile time

IOS App startup optimization (5) : Collect symbols && generate Order File

IOS App launch optimization (vi) : Utility party directly see here

Cold start and hot start

  • Cold start: indicatesAPPBy the backgroundkillPost rebootAPP, which is called cold start.
  • Thermal activation:APPThe status of therunningSwitch to asuspend.APPHas not beenkillIt’s still running in the background. Again putAPPSwitch to the foreground. This boot mode is called hot boot.

Composition of startup time

  • The partition of startup time can be putmain()The function is split in two as a key point
  • t1Stage,main()The previous processing time is calledpre-main
  • t2Stage,main()andmain()Time required for subsequent processing

T1 phase:pre-main

T2 stage

Phase T2 takes most of the time for business code

The recommended tool is BLStopwatch, which can collect statistics on service time

This part of optimization is handled according to their own business requirements

Xcode measurementpre-maintime

You can get the time of the pre-main phase by adding environment variables

DYLD_PRINT_STATISTICS

Edit scheme -> Run -> uments Add the environment variable DYLD_PRINT_STATISTICS, value set to YES.

After startup, you can see the startup duration

loadingdylib

Analyze each dylib (mostly system), find its Mach-O file, open it and read it for validation; Find the code signature and register it with the kernel, and finally call mmap() for each segment in dylib.

ASLR (Address Space Layout Randomization) technology and code signature are introduced in the loading process of dylib for safety.

ASLR: Images, executables, dylib, and bundles will add a slide in front of the preferred_address to prevent the internal address from being located.

rebase/bind

After dylib is loaded, they are in an independent state and need to be bound together.

Rebase reads the image into memory and modifies the pointer inside the image. The performance cost is mainly I/O.

Bind is used to query the symbol table and set Pointers to the outside of the image. The performance cost is mainly calculated by CPU.

Objc setup

The Runtime maintains a global table of class names and a list of class methods.

  • Read all classes and register class objects into the global table (class registration)
  • Read all categories and load the categories into the class object (category registration)
  • checkselectorUniqueness of (selector uniquing)

initalizer time

This is actually how long the load method takes

DYLD_PRINT_STATISTICS_DETAILS

You can also get a more detailed time by adding the environment variable DYLD_PRINT_STATISTICS_DETAILS with value set to YES.

Optimization idea

  • Remove dynamic libraries that you don’t need and use system libraries as much as possible, and Apple recommends keeping the number under six
  • Remove classes that are not needed Merge classes and extensions with similar functionality; Testing 20,000 classes adds about 800 milliseconds
  • Try to do lazy loading, try to avoid inload()Method to perform the action, postponing the action toinitialize()methods

IOS App startup optimization (2) : physical memory and virtual memory