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

LINE TECHPULSE 2022 - Reduce iOS app size By analyzing source code

LINE TECHPULSE 2022 - Reduce iOS app size By analyzing source code

Reduce iOS app size By analyzing source code by QC Li / iOS Dev @ LINE TECHPULSE 2022 https://techpulse.line.me/

LINE Developers Taiwan

January 21, 2022
Tweet

More Decks by LINE Developers Taiwan

Other Decks in Programming

Transcript

  1. QC Li / iOS Dev Reduce iOS app size By

    analyzing source code 
  2. › 📦 The space an app takes on the disk

    of user’s cellphone. › ⏳ It takes longer time to download or update an app with bigger binary size. App Size
  3. App … 🗄 Database 📐 Storyboard / UI file 📚

    Translation Text 🖼 Image 📃 Code
  4. How to find redundant code? › Like losing weight ›

    We should have a scale first. › Can compare the weight change before and after. › May need to analyze body composition. › By removing redundant code, it can reduce app size.
  5. Link Map › An intermediate file generated by compiler. ›

    Recorded the memory layout of an app by combining binary from other object file.
  6. By default Xcode doesn’t generate link map but it is

    easy to turn this build option on. Generate it! Turn this on
  7. Link Map Format › Path and Arch › Object files

    › Sections › Symbols › Dead Stripped Symbols
  8. Object Files › Each object files (.o) is generated from

    source code by compiler. › There is an ID number for each object file in link map. Filename ID
  9. › Memory layout › Address › Size › Summarize all

    sizes will get total size of the app. Sections
  10. › Memory layout details › Address › Size › File

    — Object file › Name Symbols Symbol Object file ID Size
  11. › Unique name for compiler usage. › Help to address

    corresponding code. › Use tool to get meaning. Mangling
  12. What we learned from Link Map › Compare the size

    between components. › Observe the size change before and after. › Knowing the size of each component. › Down to source file level. › Combining object file list and symbol list from Link Map file.
  13. Redundant Code › Unreferenced declarations › How to find those

    unreferenced declaration? › Variables or functions that are not in use
  14. Find Unreferenced Declaration › Use “Find Call Hierarchy” in IDE.

    › Compiler have those information. › We could analyze compiler intermediate steps › Is there a systematic, lightweight way?
  15. SourceKit › Open source by Apple › Swift language tool

    › IDE supports › Indexing › Syntax highlight › Code completion
  16. SourceKit C API › SourceKit provides a set of APIs

    that you can write your program to call SourceKit. › There are some 3rd open source softwares help you communicate with SourceKit, such as SourceKitten.
  17. Use SourceKit for parsing 📃 📋 💻 🔬 Source Code

    Parsed Result SourceKit Parsing Program XPC
  18. Find Declarations There is a public declaration of structure called

    MilkTea Declaration Name Type Accessibility
  19. Find References Inside MilkTea, there is a public declaration of

    variable sugar which is a SugarAmount Reference to another type Type Name
  20. Unreferenced Declaration › They are not in use because they

    are not reached › We could consider to remove them from source code › Unreached nodes are unreferenced declaration
  21. Practice in LINE Internal Modules Applied on 3 Binary size

    Reduced 44% For LINE app Saved 2.1MB
  22. Recap › To find out redundant code, we can use

    language tool such as SourceKit to analyze source code and build dependency graph. › Remove redundant code reduces app binary size. › Link Map helps us have a better understanding of binary size for each components and source files.