25: 難道公司發大財了!?App Launch Time 加速面面觀

weak self - Podcast autorstwa 一三、波肥

Kategorie:

這集由波肥與喬喬聊聊:

  • 為什麼要加速 App Launch Time?
  • 什麼時候做?
  • 以及怎麼做?
  • 庫存已久的題目一次補完

{ 完整節目筆記請按我 }

什麼時候做 App Launch Time 加速?

公司有在賺錢的時候

App Launch 三階段

  • Pre-main: loading (MachO + dylib) / rebase & binding / Objc Setup / Initializer
    load dependency list from executable header
    rebase / binding : dylib 無法被改指令 (signed) ,load 完都是互相獨立存在某個 memory 區段(random),dylib 裡指向的 data 和實際位置不同,就要 rebase (+ offset); binding 則是處理 指向外部 symbol
    ObjC setup : register Objc class / Category / gurante uniquness of selector
    Initializer: C++ constructor / +(load)
  • main: create UIApplication, init AppDelegate
  • post-main: didFinishLaunching, first frame rendered, 自定義終點 (request location + fetch feed)
  • main.m 在 Swift 中去了哪裡? (@UIApplicationMain)

策略

  • pre-main
    dylib loading: app size 小;減少 dylib 數目 (merge pods); 全變 static
    rebase/binding: you can do nothing about it; less dylib
    Objc setup: less OC class, less cateogry
    Initializer: use +(initalize) rather than +(load)

    use iOS 13: dyld2 & dyld3
    dyld2: in-process load
    dyld3: out-process load (app download or install) => analyze header to get dylib depencies and prebinding/rebase
  • main: nothing
  • post-main: 少做事,盡量別擋 main thread;能 lazy 就 lazy; location : cache + 準確度低一點; feed: Cache; less IO
    SDK init: try to lazy
    Infra init: background thread
    DB: background
    File IO: avoid or lazy

量測、工具和用法

  • start type: cold, warm, hot
  • pre-main:
    Xcode scheme env variable: DYLD_PRINT_STATISTICS = 1 or DYLD_PRINT_STATISTICS_DETAILS = 1
    Instrument: Time profile / Thread state trace
    Script to scan unused OC methods/ Scan swift unused files/methods (13 joe 支援)
    merge pods
    static pods: pre_install , override pod.build_type : Pod::Target:BuildType.static_framework
  • main & post-main:
    signpost: interval, event
    CACurrentMediaTime() (ns), Date() (miu-s)
    Firebase Performanc: Bug bug bug bug
    MetricKit: XCTOSSingpostMetric.applicationLaunch, MXMetricPayload
    自己打點: Use process information to get when process created

高級技巧

  • binary 重排
  • Mach-O 結構組合

Reference code:

  • scan oc unused selectors
  • main.swift
  • get process create time

補充資料

更多 weak self