WWDC2019 - Optimizing App Launch

目錄

  • What is launch
  • How to properly measure your launch
  • Use Instrument to profile your launch
  • Track your launch over time

1揪阶、What is launch

Why Launch Is Important

  • First experience with your app should be delightful
  • Indicative of your codes overall performance
  • Impacts the system performance and battery

Example: Involves a lot of CPU work and a lot of memory work.

Launch Type

屏幕快照 2019-08-02 下午5.51.53.png

Cold launch

Cold launches occur after reboot, or when your app has not been launched for very long time. In order to launcher app, we need to bring it from disk into memory, startup system-side services that support your app, and then spawn your process.

Warm launch

Once it's happened once, you'll experience a warm launch. In this case, your app still needs to be spawned, but we've already brought your app into memory and started up some of those system-side services.

So, this will be a little bit faster and a little bit more consistent.

Resume

This occurs when a user reenters your app from either the home screen or the app switcher.

Phases of App Launch(App運行階段)

屏幕快照 2019-08-03 下午12.08.00.png

As you know, launch generally starts when the user taps your icon on your home screen.Then over the next 100 or so milliseconds, iOS will do the necessary system-side work in order to initialize your app.

That leaves you as developers about 300 milliseconds to create your views, load your content, and generate your first frame.

These six phases cover everything from system initialization to the app initialization to view creation and layout, and then depending on your app, potentially a asynchronous loading phase for your data, the extended phase.

屏幕快照 2019-08-02 下午5.53.20.png
  • System Interface
  • Runtime Init
  • UIKit Init
  • Application Init
  • Initial Frame Render
  • Extended

System Interface

DYLD3

  • Dynamic Linker loads shared libraries and frameworks
  • Introduces caching of runtime dependencies to improve warm launch

App Startup Time: Past, Present, and Future WWDC 2017

Some steps to optimizing for DYLD3

  • Avoid linking unused frameworks(this can have hidden costs)
  • Avoid dynamic library loading during launch(such as DLOpen or NSbundleLoad)
  • Hard link all your dependencies

For libSystem Init

  • Initializes the interfaces with low level system components
  • System side work with a fixed cost

Static Runtime Initialization

Steps to do:

  • Initializes the language runtime(Objective-C and Swift run times)
  • Invokes all class static load methods

Ways to optimizing:

  • Expose delicate initialization API in frameworks
  • Reduce impact to launch by avoiding + [Class load]
  • Use + [Class initialize] to lazily conduct static initialization

UIKit Initialization

Steps to do:

  • Instantiates the UIApplication And UIApplicationDelegate
  • Begins event processing and integration with the system

Ways to optimizing:

  • Minimize work in UIApplication subclass
  • Minimize work in UIApplicationDelegateinitialization
  • Application Initialization

Lifecycle Callbacks

Invokes UIApplicationDelegate app lifecycle callbacks

application:willFinishLaunchingWithOptions:
application:didFinishLaunchingWithOptions:

Invokes UIApplicaitonDelegate UI lifecycle callbacks

applicationDidBecomaActive:

Invokes UIScreenDelegate UI lifecycle callbacks for each scene

scene:willConnectToSession:options:
scene:willEnterForeground:
sceneDidBecomeActive:

Ways to optimizing:

  • Defer unrelated work

by either pushing it to the background queues or just doing it later entirely

  • share resources between scenes

First Frame Render

Creates, performs layout for, and draws views
Commits and renders first frame

loadView
viewDidLoad
layoutSubviews

Ways to optimizing:

  • Flatten view hierarchies (reducing the number of views in your hierarchy)and lazily load views
  • Optimize auto layout usage(reduce the number of constraints)

Extended

  • App-specific period after first frame
  • Displaya asynchronously loaded data
  • App should be interactive and responsive
  • Leverage os_signpost to messure work

Measuring Performance Using Logging WWDC 2018

2、How to properly measure your launch

Trading Representativeness for Consistency

  • Remove sources of variance to produce mode consistent results
  • May result in launch times that are not representative
  • Use consistent results to evaluate progress

Test in a Clean and Consistent Environment

  • Reboot then let system quiesce for 2-3 minutes
  • Enable Airplane Mode or mock the network
  • Use unchanging or no iCloud Account
  • Use release build of your app
  • Measure warm launches

Measuring Launch with XCTest

屏幕快照 2019-08-02 下午6.07.29.png

Improving Battery Life and Performance WWDC 2019

3障般、Use Instruments to profile your launch

Tips and Tricks

屏幕快照 2019-08-02 下午6.08.49.png

Minimize work

  • Defer work unrelated to first frame
  • Move blocking work off main thread
  • Reduce memory usage

Prioritize work

  • Identify the right QoS for your task
  • Utilize scheduler optimizations for app launch
  • Preserve the priority with the right primitives

Modernizing Grand Central Dispatch Usage WWDC 2017

Optimize work

  • Simplify or limit existing work
  • Optimize algorithms and data structures
  • Cache resources and computations

4、Track Your Launch Over Time

Make performance a development-time priority
Plot it and have a target

Monitor Customer Launches with Xcode Organizer

屏幕快照 2019-08-02 下午6.11.24.png

Adopt MetricKit for More Statistics

  • Collect custom power and performance metrics
  • Aggregated results delivered every 24 hours

Summary

  • Start understanding your launch today
  • Measure---don't estimate---performance
  • Track performance in all phases of development

Optimizing App Launch

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末写烤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拾徙,更是在濱河造成了極大的恐慌洲炊,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尼啡,死亡現(xiàn)場離奇詭異暂衡,居然都是意外死亡,警方通過查閱死者的電腦和手機崖瞭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進店門狂巢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人书聚,你說我怎么就攤上這事唧领。” “怎么了寺惫?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵疹吃,是天一觀的道長。 經(jīng)常有香客問我西雀,道長,這世上最難降的妖魔是什么歉摧? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任艇肴,我火速辦了婚禮,結(jié)果婚禮上叁温,老公的妹妹穿的比我還像新娘再悼。我一直安慰自己,他們只是感情好膝但,可當我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布冲九。 她就那樣靜靜地躺著,像睡著了一般跟束。 火紅的嫁衣襯著肌膚如雪莺奸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天冀宴,我揣著相機與錄音灭贷,去河邊找鬼。 笑死略贮,一個胖子當著我的面吹牛甚疟,可吹牛的內(nèi)容都是我干的仗岖。 我是一名探鬼主播,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼览妖,長吁一口氣:“原來是場噩夢啊……” “哼轧拄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起讽膏,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤檩电,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后桅打,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體是嗜,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年挺尾,在試婚紗的時候發(fā)現(xiàn)自己被綠了鹅搪。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡遭铺,死狀恐怖丽柿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情魂挂,我是刑警寧澤甫题,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站涂召,受9級特大地震影響坠非,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜果正,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一炎码、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秋泳,春花似錦潦闲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至卓起,卻和暖如春和敬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背既绩。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工概龄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人饲握。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓私杜,卻偏偏與公主長得像蚕键,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子衰粹,可洞房花燭夜當晚...
    茶點故事閱讀 42,834評論 2 345