Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mergeable Libraryで 高速なアプリ起動を実現しよう!

giginet
August 23, 2024

Mergeable Libraryで 高速なアプリ起動を実現しよう!

giginet

August 23, 2024
Tweet

More Decks by giginet

Other Decks in Programming

Transcript

  1. Static Frameworkͷಛ௃ Ϗϧυ࣌ؒ Ϗϧυ࣌ʹϦϯΫ͕ඞཁͳͨΊ௕͘ͳΓ͕ͪ Ωϟογϡ ϦϯΫ͠௚͢ඞཁ͕͋Γޮ͖ͮΒ͍ Xcode Preview ར༻ෆՄ ʢͨͩ͠Xcode

    16͔Β͸αϙʔτʣ ىಈ଎౓΁ͷӨڹ Өڹ͕ͳ͍ ΞϓϦͷαΠζ embed͕ෆཁɺෆཁͳ࣮૷ͷ࠷దԽ͕ޮ͖΍͘͢খ͘͞͠΍͍͢
  2. Dynamic Framework Static Framework Ϗϧυ࣌ؒ ୹Ί Ϗϧυ࣌ʹϦϯΫ͕ෆཁ Ωϟογϡ͠΍͍͢ ௕Ί Ϗϧυ࣌ʹϦϯΫ͕ඞཁ

    Ωϟογϡͮ͠Β͍ Ωϟογϡ ͠΍͍͢ ͮ͠Β͍ Xcode Preview Մೳ ෆՄ ʢXcode 16Ҏ߱͸Մೳʣ ىಈ଎౓ Өڹ͕͋Δ ʢىಈ࣌ͷϦϯΫ͕ൃੜʣ Өڹ͕ͳ͍ ΞϓϦͷαΠζ େ͖͘ͳΓ͕ͪ embed͕ඞཁ ࠷దԽͮ͠Β͍ খ͞Ί embed͕ෆཁ ࠷దԽ͠΍͍͢ ϦϦʔε࣌ʹ༗ར ։ൃ࣌ʹѻ͍΍͍͢
  3. Resource Bundle Framework Bundle MyApp.app/Frameworks/ MyUI.framework Dynamic Framework Static Framework

    Main Bundle MyApp.app/Frameworks/ MyUI.framework Static Framework಺ͷ Bundle͸ࢀরͰ͖ͳ͍ let bundle = Bundle(for: MyClass.self)
  4. Mergeable Library • ϝλσʔλΛ࣋ͬͨ৽͍͠Dynamic Library • Build Con fi gurationʹԠͯ͡ϦϯΫํࣜΛ੾Γସ͑ΒΕΔ

    • DebugϏϧυ࣌͸Dynamic Frameworkͱͯ͠ৼΔ෣͏ • ReleaseϏϧυ࣌͸Static Frameworkͱͯ͠ৼΔ෣͏ ਤͷҾ༻ɿhttps://developer.apple.com/documentation/xcode/con fi guring-your-project-to-use-mergeable-libraries
  5. Ϛʔδ͞ΕͨόΠφϦΛݟͯΈΑ͏ $ nm -gU GiginyanPay.app/GiginyanPay 0000000100014070 S _$s10NyanPayKit7TopViewV7SwiftUI0E0AAMc 0000000100008f78 T

    _$s11NyanPayCore6WalletC6amountACSi_tcfC 0000000100014268 S _$s9NyanPayUI10QRCodeViewV05SwiftC00E0AAMc 00000001000141f0 S _$s9NyanPayUI9ImageViewV05SwiftC00E0AAMc 0000000100014150 S _NyanPayCoreVersionNumber 0000000100014120 S _NyanPayCoreVersionString 0000000100014068 S _NyanPayKitVersionNumber 0000000100014038 S _NyanPayKitVersionString 00000001000141e8 S _NyanPayUIVersionNumber 00000001000141b8 S _NyanPayUIVersionString 0000000100000000 T __mh_execute_header 00000001000054ac T _main • ΞϓϦόΠφϦʹଞͷϑϨʔϜϫʔΫͷγϯϘϧʢ࣮૷ʣ͕ຒΊࠐ·Ε͍ͯ Δ͜ͱ͕Θ͔ΔʢStatic Link)
  6. embed͞ΕͨStatic Framework $ nm -gU GiginyanPay.app/Frameworks/NyanPayUI.framework/NyanPayUI GiginyanPay.app/Frameworks/NyanPayUI.framework/NyanPayUI: no symbols •

    embed͞ΕͨStatic FrameworkͷόΠφϦʹ͸γϯϘϧؚ͕·Εͳ͍ • ϦιʔεόϯυϧͷΈembedͰ͖Δ ϦιʔεͷΈͷ ϑϨʔϜϫʔΫ
  7. ϕϯνϚʔΫ Dynamic Framework Mergeable (Debug/Re-export) Mergeable (Release) Ϗϧυ࣌ؒ ΫϦʔϯϏϧυ 185s

    151.6s 84.3s Ϗϧυ࣌ؒ ࠩ෼Ϗϧυ 7s 79.3s 10.16s ΞϓϦαΠζ 81.9MB 82MB 6.8MB
  8. %ZOBNJD-JCSBSZ΁ͷϚʔδ • MAKE_MERGEABLE=YESΛࢦఆ͢Δͱɺ*.debug.dylibʹDynamic FrameworkͷόΠφϦ͕Ϛʔδ͞ΕΔ • ୅ΘΓʹReexportedBinaries͸ग़ྗ͞Εͳ͘ͳΓɺίϐʔίετ͕ͳ͘ͳΔ DebugϏϧυ MAKE_MERGEABLE =YES ґଘͷ࣮૷͸dylibʹ౷߹͞ΕΔ

    Merged Dynamic Binary 239KB -> 417KB $ nm -gU GiginyanPay.debug.dylib 0000000000008c88 T _$s10NyanPayKit7TopViewV4bodyQrvg 000000000001d3b0 S _$s10NyanPayKit7TopViewV4bodyQrvpMV 000000000001da4c S _$s10NyanPayKit7TopViewV4bodyQrvpQOMQ 0000000000008938 T _$s10NyanPayKit7TopViewV6wallet0aB4Core6WalletCvM 00000000000085fc T _$s10NyanPayKit7TopViewV6wallet0aB4Core6WalletCvg 0000000000020910 S _$s10NyanPayKit7TopViewV6wallet0aB4Core6WalletCvpMV 0000000000008870 T
  9. ϕϯνϚʔΫ Dynamic Framework Mergeable (Debug/Merge) Mergeable (Release) Ϗϧυ࣌ؒ ΫϦʔϯϏϧυ 185s

    149.3s 84.3s Ϗϧυ࣌ؒ ࠩ෼Ϗϧυ 7s 7.6s 10.16s ΞϓϦαΠζ 81.9MB 8.4MB 6.8MB
  10. ߟ࡯ɿཧ૝஋͕ग़͍ͯͳ͍݅ • ΫϦʔϯϏϧυ࣌ͷDynamic Frameworkߏஙʹ͔͔ΔॳظίετΛࠩ෼Ϗϧυ ͰϖΠͰ͖͍ͯͳ͍ • Ծઆ1: μϛʔϓϩδΣΫτͷͨΊ࣮૷͕΄΅ͳ͍ • Static

    LinkͰ͋ͬͨͱͯ͠΋ϦϯΫ͕͔࣌ؒͳΓ୹͍ • Ծઆ2: ࠶Ϗϧυ࣌ͷมߋ͕ͳ͍ • Static FrameworkͰ͋ͬͯ΋Ωϟογϡ͕شൃͮ͠Β͘ɺDynamic FrameworkͷΩϟογϡͷར఺Λੜ͔͍ͤͯͳ͍
  11. Ϛʔδ͢Δଆͷઃఆ • Ϛʔδ͢ΔଆʢΞϓϦଆʣ • Create Merged Library: Automatic/Manual • Automatic:

    ࢠґଘΛϚʔδՄೳʹ | Manual: ࢦఆͨ͠΋ͷͷΈ • Ϛʔδ͢Δ΋ͷΛFrameworks, Libraries, and Embedded Contentʹ௥Ճ • ໌ࣔతͳґଘ(௚઀ґଘ)ͷΈ͕Ϛʔδ͞ΕΔ • Ϧιʔε͕͋Δ΋ͷͷΈEmbed & Sign Create Merged Binary Frameworks, Libraries, and Embedded Content 1 2 1 2
  12. Ϛʔδ͞ΕΔଆͷઃఆ • Ϛʔδ͞ΕΔଆʢϑϨʔϜϫʔΫଆʣ • Build Mergeable Library • ϑϨʔϜϫʔΫΛϚʔδՄೳʹ͢Δ͔ •

    ManualͳΒBuild Mergeable LibraryΛYES • MAKE_MERGEABLE • ϑϨʔϜϫʔΫΛϚʔδ͢Δ৔߹͸YES • ࢦఆ͠ͳ͍ͱσόοά࣌͸࠶ΤΫεϙʔτ͢Δ Build Mergeable Library MAKE_MERGEABLE 2 2 1 1
  13. XCFrameworkͷMergeableԽ • ϏϧυࡁΈόΠφϦ(XCFramework)΋Mergeable Libraryͱͯ͠഑෍Ͱ͖Δ • ഑෍αΠζ͸େ͖͘ͳΔ͕ɺϥΠϒϥϦͷར༻ऀ͕Dynamic/Static྆ํΛ࢖ ͍Θ͚Δ͜ͱ͕Ͱ͖Δ • Framework͕Mergeable Library͔Ͳ͏͔͸ɺmetadataͷ༗ແͰ൑ผͰ͖Δ

    • LC_ATOM_INFOΛϩʔυ͢Δ͔Ͳ͏͔ $ otool -l MyFramework.xcframework/ios-arm64/MyFramework.framework/MyFramework Load command 49 cmd LC_ATOM_INFO cmdsize 16 dataoff 133328 datasize 63984
  14. Swift PackageͷMergeable XCFrameworkԽ • giginet/Scipio͸͢ͰʹMergeable LibraryαϙʔτࡁΈ • ଟ͘ͷSwift Package͸͜Ε͚ͩͰMergeable XCFrameworkԽͰ͖Δ

    • https://giginet.github.io/Scipio/documentation/scipio/mergeable-library Swift PackageΛ࢖ͬͨ ڊେͳґଘάϥϑͷΩϟογϡઓུ iOSDC2023 $ scipio create path/to/MyPackage --framework-type mergeable --enable-library- evolution
  15. ·ͱΊ • Mergeable Libraryͷ࢓૊Έͱಋೖํ๏ • ࠓ͙͢Create Merged Binary > Automatic

    • MAKE_MERGEABLE=YESΛ๨Εͣʹ • Prebuilt Framework͸ࠓ͙͢Mergeable FrameworkͰ഑෍ʂ • Ϧιʔεؚ͕·ΕΔ৔߹ͷରॲํ๏
  16. ࢀߟࢿྉ - Mergeable Library - • Meet mergeable libraries -

    WWDC23 - Videos - Apple Developer • https://developer.apple.com/videos/play/wwdc2023/10268/ • Con fi guring your project to use mergeable libraries | Apple Developer Documentation • https://developer.apple.com/documentation/xcode/con fi guring-your- project-to-use-mergeable-libraries • https://github.com/kateinoigakukun/MergeableLibraryInternals • Understanding mergeable libraries • https://www.polpiella.dev/understanding-mergeable-libraries
  17. ࢀߟࢿྉ - Metrics- • Reducing your app’s launch time |

    Apple Developer Documentation • https://developer.apple.com/documentation/xcode/reducing-your-app-s- launch-time • Ultimate application performance survival guide - WWDC21 - Videos - Apple Developer • https://developer.apple.com/videos/play/wwdc2021/10181/ • What’s new in Xcode 16 - WWDC24 - Videos - Apple Developer • https://developer.apple.com/videos/play/wwdc2024/10135/
  18. Mergeable Libraryઃఆ஋ xccon fi g Xcode GUI આ໌ MERGED_BINARY_TYPE NO/automatic/manual

    Create Merged Binary ϦϯΫઌͷλʔήοτΛϚʔδ͢Δ͔ AutomaticͩͱࣗಈతʹϚʔδՄೳʹ͢Δ Manualͩͱ໌ࣔతʹࢦఆͯ͠Δ΋ͷͷΈ MERGEABLE_LIBRARY YES/NO Build Mergeable Library ϥΠϒϥϦΛϚʔδՄೳʹ͢Δ͔ɻManual ͷࡍ͸໌ࣔతʹࢦఆ͕ඞཁ MAKE_MERGEABLE YES/NO ઃఆෆՄ σόοάϏϧυ࣌ʹϥΠϒϥϦΛϦϯΫ࣌ʹ Ϛʔδ͢Δ͔ɺ࠶ΤΫεϙʔτ͢Δ͔
  19. ىಈ࣌ؒܭଌͷ4ͭͷํ๏ Instruments Performance Tests MetricKit Xcode Organizer ར༻͢Δ΋ͷ App Launch

    Template XCTApplicationLaunch Metric MXAppRunTimeMetric Xcode Organizer ར༻ͷ؆୯͞ ˕ ◦ ˚ ˕ ࣮ߦ λΠϛϯά ։ൃதͷ೚ҙ UI Tests࣮ߦ࣌(CIͳͲ) ΞϓϦ࣮ߦ࣌ɺϦϦʔεޙ ϦϦʔεޙ ࣮૷ ෆཁ UIςετέʔεΛ࣮૷ ϝτϦΫεऔಘ࣌ͷίʔϧ όοΫΛ࣮૷ ෆཁ ༻్ ։ൃ࣌ͷσόοά σάϨʔγϣϯͷݕ஌ ΤϯυϢʔβʔͷϩάΛ ࡉ͔͘औΔ ʢϩάج൫΁ͷૹ৴ͳͲʣ ΤϯυϢʔβʔͷ ܏޲Λόʔδϣϯຖʹ ஌Δ