App startup time with Dynamic/Static frameworks

App startup time with Dynamic/Static frameworks

2f8b61ad31dd9c6ada4a90af746290fd?s=128

Shota Kashihara

March 19, 2019
Tweet

Transcript

  1. ©2019 Wantedly, Inc. App startup time with Dynamic/Static frameworks try!

    Swift Pre Talk 2019 Mar 19, 2019 - @ShotaKashihara
  2. ©2019 Wantedly, Inc. Shota Kashihara ɾWantedly People ɾiOS
 ɾSwift/Double base

    ɾTwitter: @oakland76 ɾGitHub: @ShotaKashihara
  3. ©2019 Wantedly, Inc. ɾXcode ͱ Frameworks ͷྺ࢙ ɾΞϓϦͷىಈͱ Frameworks ͷؔ܎

    ɾFrameworks ͷߏ੒Λߟ͑Δ ࠓ೔࿩͢͜ͱ
  4. ©2019 Wantedly, Inc. Xcode ͱ Frameworks ͷྺ࢙

  5. ©2019 Wantedly, Inc. Xcode ͱ Frameworks ͷྺ࢙ Dynamic/Static Framework Λ

    ͓͞Β͍
  6. ©2019 Wantedly, Inc. Xcode ͱ Frameworks ͷྺ࢙ Xcode 5 #2013೥

    ɹɾOnly Static Library (Obj-C) Dynamic/Static Framework Λ ͓͞Β͍
  7. ©2019 Wantedly, Inc. Xcode ͱ Frameworks ͷྺ࢙ Xcode 5 #2013೥

    ɹɾOnly Static Library (Obj-C) Xcode 6 #2014೥ ɹɾSupport embedded frameworks (Dynamic Library) Dynamic/Static Framework Λ ͓͞Β͍
  8. ©2019 Wantedly, Inc. Xcode ͱ Frameworks ͷྺ࢙ Xcode 5 #2013೥

    ɹɾOnly Static Library (Obj-C) Xcode 6 #2014೥ ɹɾSupport embedded frameworks (Dynamic Library) Xcode 9 #2017೥ ɹɾSupport static library with Swift ɹɹɾCocoa Pods (v1.5.0), Carthage (v0.30.1) ʹͯ ରԠࡁ Dynamic/Static Framework Λ ͓͞Β͍
  9. ©2019 Wantedly, Inc. Dynamic/Static Framework

  10. ©2019 Wantedly, Inc. Framework ֦ுࢠ .framework ɾϥΠϒϥϦ ɾ(όϯυϧ) ɹɾNib ɹɾը૾

    ɹɾϔομʔϑΝΠϧ ͳͲΛ·ͱΊͨϑΥϧμ Dynamic/Static Framework
  11. ©2019 Wantedly, Inc. Dynamic Framework .framework ɾಈతϥΠϒϥϦ (.dylib, or ֦ுࢠͳ͠)

    ɹΛؚΜͩ Framework Dynamic/Static Framework
  12. ©2019 Wantedly, Inc. Dynamic Framework .framework ɾಈతϥΠϒϥϦ (.dylib, or ֦ுࢠͳ͠)

    ɹΛؚΜͩ Framework ɾArchive ɹɾ<YourApp>/Frameworks/ ഑Լʹ ɹɹ.framework ͷ··ίϐʔ Dynamic/Static Framework
  13. ©2019 Wantedly, Inc. Static Framework .framework ɾ੩తϥΠϒϥϦ (.a, or ֦ுࢠͳ͠)

    ɹΛؚΜͩ Framework Dynamic/Static Framework
  14. ©2019 Wantedly, Inc. Static Framework .framework ɹ ɾArchive ɹɾ੩తϥΠϒϥϦ͸ίϯύΠϧ࣌ʹ੩తϦϯΧʹΑͬͯɺ ɹɹ࣮ߦՄೳϑΝΠϧʹؚ·ΕΔ(୯ҰͷϑΝΠϧʹͳΔ)

    ɹɾϦιʔεϑΝΠϧ͸ΞϓϦέʔγϣϯ഑Լʹίϐʔͯ͠ɺ ɹɹ࣮ߦՄೳϑΝΠϧ͔Βݟ͑Δঢ়ଶʹ͢Δඞཁ͋Γ Dynamic/Static Framework ← Static Library ͸ίϯύΠϧͰ ࣮ߦՄೳϑΝΠϧͷҰ෦ʹͳΔ ɾ੩తϥΠϒϥϦ (.a, or ֦ுࢠͳ͠) ɹΛؚΜͩ Framework
  15. ©2019 Wantedly, Inc. Dynamic Library ͷ 2ͭͷಛ௃ ɾDynamic Linking ɾDynamic

    Loading Dynamic/Static Framework
  16. ©2019 Wantedly, Inc. Dynamic Linking ɾΞϓϦ࣮ߦ࣌ʹϦϯΫ͕ߦΘΕΔ͜ͱ ɾ࣮ߦՄೳϑΝΠϧΛখ͘͞อͭ͜ͱ͕Ͱ͖Δ Dynamic/Static Framework

  17. ©2019 Wantedly, Inc. Dynamic Loading ɾ`dlopen()` ɾΞϓϦىಈ࣌Ͱ͸ͳ͘ɺඞཁͳͱ͖ʹϩʔυΛ࢝ΊΔ͜ͱ͕Ͱ͖Δ ɾiOS Ͱ͸࢖͍ͬͯΔ৔໘Λ͋·Γݟͨ͜ͱ͕ͳ͍ Dynamic/Static

    Framework
  18. ©2019 Wantedly, Inc. ΞϓϦͷىಈʹ͸Ͳ͏͍͏ϑϩʔ͕͋Δ͔

  19. ©2019 Wantedly, Inc. exec() to main()

  20. ©2019 Wantedly, Inc. exec() to main() ΞϓϦ্ཱ͕͕ͪΔ·ͰʹཪଆͰى͖͍ͯΔ͜ͱ ※ WWDC 2016

    (Optimizing App Startup Time) ʹͯݴٴ͞ΕͨςʔϚ exec() to main()
  21. ©2019 Wantedly, Inc. exec() 1. Kernel maps your application into

    new address space 2. Kernel loads helper program 3. Dyld runs in-process exec() to main()
  22. ©2019 Wantedly, Inc. exec() 1. Kernel maps your application into

    new address space 2. Kernel loads helper program 3. Dyld runs in-process exec() to main()
  23. ©2019 Wantedly, Inc. exec() 1. Kernel maps your application into

    new address space 2. Kernel loads helper program 3. Dyld runs in-process exec() to main()
  24. ©2019 Wantedly, Inc. exec() 1. Kernel maps your application into

    new address space 2. Kernel loads helper program 3. Dyld runs in-process exec() to main()
  25. ©2019 Wantedly, Inc. exec() 1. Kernel maps your application into

    new address space 2. Kernel loads helper program 3. Dyld runs in-process Dyld: ࣮ߦ࣌ʹ dynamic library ΛಡΈࠐΉϓϩηεͷ͜ͱ exec() to main()
  26. ©2019 Wantedly, Inc. Dyld Steps 1. Map all dependent dylibs,

    recurse 2. Rebase all images 3. Bind all images 4. ObjC prepare images 5. Run initializers exec() to main()
  27. ©2019 Wantedly, Inc. Dyld Steps 1. Map all dependent dylibs,

    recurse 2. Rebase all images 3. Bind all images 4. ObjC prepare images 5. Run initializers exec() to main()
  28. ©2019 Wantedly, Inc. Dyld Steps 1. Map all dependent dylibs,

    recurse 2. Rebase all images 3. Bind all images 4. ObjC prepare images 5. Run initializers exec() to main()
  29. ©2019 Wantedly, Inc. Dyld Steps 1. Map all dependent dylibs,

    recurse 2. Rebase all images 3. Bind all images 4. ObjC prepare images 5. Run initializers exec() to main() ͜ΕΒ͕ Dynamic Framework ͷ਺͚ͩ࠶ؼ͢Δ
  30. ©2019 Wantedly, Inc. Dyld Steps ͜͜ʹͲΕ͘Β͍ͷ͕͔͔͍࣌ؒͬͯΔ͔ΛݟΔ exec() to main()

  31. ©2019 Wantedly, Inc. DYLD_PRINT_STATISTICS exec() to main()

  32. ©2019 Wantedly, Inc. DYLD_PRINT_STATISTICS exec() to main() ΞϓϦΛ্ཱͪ͛Δͱ main() ·Ͱʹ

    Dyld ʹ͔͔͕ͬͨ࣌ؒදࣔ
  33. ©2019 Wantedly, Inc. exec() to main() ɾΞϓϦͷىಈ࣌ʹ Dyld ͕࠶ؼతʹ Framework

    ΛϝϞϦʹ഑ஔ͢Δ ɾDynamic Framework ͷ਺͚ͩΦʔόʔϔου͕૿͑Δ ɹ exec() to main()
  34. ©2019 Wantedly, Inc. exec() to main() ɾΞϓϦͷىಈ࣌ʹ Dyld ͕࠶ؼతʹ Framework

    ΛϝϞϦʹ഑ஔ͢Δ ɾDynamic Framework ͷ਺͚ͩΦʔόʔϔου͕૿͑Δ ɹ ɹˠ ෳ਺ͷ Dynamic Frameworks Λ Static Library ͱͯ͠Ϗϧυͯ͠ɺ ɹɹ ͦΕΒΛ·ͱΊͨ Dynamic Frameworks Λ࡞Δ exec() to main()
  35. ©2019 Wantedly, Inc. ΞϓϦ ͷߏ੒ Domain.framework Main.app Watch.app C.framework B.framework

    (3rd party) A.framework
  36. ©2019 Wantedly, Inc. ΞϓϦ ͷߏ੒ Domain.framework Main.app Watch.app Umbrella frameworks

    C.framework B.framework (3rd party) A.framework
  37. ©2019 Wantedly, Inc. ΞϓϦ ͷߏ੒ Domain.framework Main.app Watch.app Umbrella frameworks

    C.framework B.framework (3rd party) A.framework Static linking
  38. ©2019 Wantedly, Inc. ·ͱΊ

  39. ©2019 Wantedly, Inc. Summary ɾDynamic frameworks ͕૿͑Δͱ Dyld ͷίετ͕͔͔Δ ɾΞϓϦͷىಈ଎౓͕ؾʹͳͬͨΒ

    DYLD_PRINT_STATISTICS ΦϓγϣϯͰ ɹDynamic Library ͷϩʔυʹ͔͔͍ͬͯΔ࣌ؒΛݟͯΈΑ͏ ɾDynamic frameworks Λ Static Frameworks ʹஔ͖׵͑ͯ ɹDyld ͷίετΛݮΒ͢͜ͱ΋બ୒ࢶͷҰͭ App startup time with Dynamic/Static frameworks
  40. ©2019 Wantedly, Inc. Have a nice try! Swift day