[レポート] Improving Application Startup Performance #AltConf2018 #WWDC18

こちらはAltConfで行われたセッション「9 Lessons Learned from Scaling a New App Business to $1M Recurring Revenue in 6 Months」のレポートです。
2018.06.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは。モバイルアプリサービス部の平屋です。

こちらはAltConfで行われたセッション「Improving Application Startup Performance」のレポートです。

スピーカーはtumblrのEngineerのKen Ackerson(@pearapps)さんです。

概要

  • アプリの起動時間を最適化する方法を複数の視点から見ていく

For the last year at Tumblr we have been extensively working on improving our application's start up time. Learn about many different aspects of optimizing your application's startup time as well as some interesting things we have learned along the way.

Table of Contents

  • Introduction / Backstory
  • Problems and solutions
  • General performance tips

Introduction / Backstory

Application Startup Timeとは

  • Pre-Main time + Post-main time = Application Startup Time
  • Time for the user to be able to interact with your application after pressing the Icon

アプリの起動時間の遅さはなぜ問題か?

  • 86% of consumers have deleted applications because of performance issues.
  • Users hate to wait

Backstory

  • 2016
    • Tumblr application started in approx. 12-15 seconds
    • No production performance metrics

アプリが遅いと評価が下がる?

Problems and solutions

Measuring

時間計測の実装例

Instruments

  • File Activity
  • System Usage
  • Time Profiler
  • Network

Pre-main

  • Before UIApplication callbacks
  • View metrics with the DYLD_PRINT_STATISTICS env variable

SchemeのArgumentsのEnvironment Variablesに DYLD_PRINT_STATISTICS を追加する

DYLD_PRINT_STATISTICS

ログの出力例

dylib loading time

  • Loading of dynamic libraries
  • Apple recommends only 6 non-Apple dynamic libraries

Tumblrアプリのdynamic frameworkの数の変遷

  • 2015-2016
    • 66 third party (non-Apple) dynamic frameworks
  • 2017
    • Mixed static libraries and dynamic frameworks
  • 2018
    • Almost exclusively static libraries where possible

static librariesへの移行

  • Cocoapods 1.5.x
    • Supports static linking for Swift
    • Turn off use_frameworks!
  • Hybrid solutions if needed

結果

Rebase/binding time and ObjC setup time

  • This is the time it takes to setup all of your Objective-C classes and selectors
  • Remove unused Objective-C code
  • Use Swift

Post-main

Clean up leaks

  • Use the Leaks Instrument and the Memory Graph Debugger to clean up leaks

Third party librariesに関する注意点

  • Be careful about adding third party libraries
  • We have seen seconds (!) taken from our launch time due to a few third party libraries
  • Be careful about adding trendy libraries

Asset loading

  • We found issues with UIImage(named: "")
    • Costs between 15-50+ ms depending on device per image
  • Similar issues with UIFont - especially bundled fonts.
UIImage(named: "") solution 1

UIImage(named: "") solution 2

Other use cases

  • UIFont
  • CoreData NSMangedobjectModel allocations
  • Other I/O blocking application startup

Optimizing View Controllers

  • Lazily load VCs with VC containment

General performance tips

Concurrent Programming using GCD groups

Compiler settings we've enabled

  • WMO (Whole Module Optimization)
  • LTO (Link time Optimization)

Other compiler settings to investigate

  • Unroll loops
  • Repeated use of weak references

TL/DR

  • Profile, Profile, Profile!
  • Use less dynamic libraries
  • Use Swift and/or remove dead Objective-C code
  • Measure and optimize assets and UIViewController initializations during launch
  • Make sure your project settings are optimal for the tradeoffs you want

More Resources

Thats all

さいごに

ちょうど開発中のアプリの起動時間が気になっていたので、起動時間の計測やライブラリの見直しなどから始めてみようかと思いました!

今年のAltConfは6/7(木)まで開催されます。セッションスケジュールは以下のページで確認できます。気になるセッションがある方はぜひ足を運んでみてください。