APP startup can be divided into two types

  1. Cold Launch: Start an APP from scratch;
  2. Warm Launch: The APP is already in memory and alive in the background. Click the icon again to Launch the APP.

The optimization of APP startup time is mainly for cold startup

  • DYLD_PRINT_STATISTICS = 1; DYLD_PRINT_STATISTICS = 1;
  • If you need more detailed information, set DYLD_PRINT_STATISTICS_DETAILS to 1.

The cold start of APP can be summarized into three stages

  1. dyld(dynamic link editor):
  • Apple dynamic linker, which can be used to load Mach-O files (executables, dynamic libraries, etc.)
  1. runtime;
  2. The main.

1. Dyld stage

  1. Load the executable (Mach-O) file of the APP and recursively load all dependent dynamic libraries;
  2. When dyLD finishes loading the executable and dynamic libraries, it notifies Runtime for further processing.

2. Runtime phase:

  1. callmap_imagesParsing and processing the contents of executable files:
 _dyld_objc_notify_register(&map_images, load_images, unmap_image);
Copy the code
  1. inload_imagesIn the callcall_load_methodsCall the +load method for all classes and categories;
  // Call +load methods (without runtimeLock - re-entrant)
  call_load_methods();
Copy the code
  1. Initialize various objC structures (register objC classes, initialize class objects, and so on)
  2. Call the C++ static initializer and__attribute__((constructor))Modified function
  • Up to this point, all symbols (Class, Protocol, Selector, IMP,…) in executables and dynamic libraries All have been successfully loaded into memory in the format managed by the Runtime.

3. Start the main function

  1. When all initialization is complete, Dyld calls main;
  • Next up is the UIApplicationMain function, the AppDelegateapplication:didFinishLaunchingWithOptions:methods

Cold start optimization:

1. Dyld phase

  1. Reduce dynamic libraries, merge some dynamic libraries (periodically clean up unnecessary dynamic libraries);
  2. Reduce the number of Objc classes and classes, and reduce the number of selectors (periodically cleaning up unnecessary classes and classes);
  3. Reduce the number of C++ virtual functions (The existence of virtual functions generates a virtual table.)
  4. Swift uses structs as much as possible.

2. The runtime phase

  1. Replace all with the +initialize method and dispatch_once__attribute__((constructor)), C++ static constructor, ObjC+load.
+ (void)initialize {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
    });
}
Copy the code

3. main

  1. Delay as much as possible without affecting the user experience, and don’t put everything in the finishLaunching method;
  2. Load on demand.

IOS Performance optimization:

  • Caton optimization;
  • Power consumption optimization;
  • Startup optimization;