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

More Decks by LINE Developers Taiwan

Other Decks in Programming


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

    analyzing source code 
  2. By Analyzing source code… Internal Modules Analyzed 3 Binary size

    Reduced 44% For LINE app Saved 2.1MB
  3. Agenda › App Size › Link Map › Find Redundant

  4. App Size Overview

  5. › 📦 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
  6. App … 🗄 Database 📐 Storyboard / UI file 📚

    Translation Text 🖼 Image 📃 Code
  7. More code, larger binary size 📃 📦 📦 📦📦 📃

  8. When app size getting bigger… › Redundant code increases in

    app › More features ➡ Bigger size
  9. 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.
  10. Link Map Using link map as scale to analyze iOS

    app size
  11. Link Map › An intermediate file generated by compiler. ›

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

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

    › Sections › Symbols › Dead Stripped Symbols
  14. 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
  15. › Memory layout › Address › Size › Summarize all

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

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

    corresponding code. › Use tool to get meaning. Mangling
  18. 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.
  19. Find Redundant Code By using SourceKit to analyze source code

  20. Redundant Code Sample 1 Compiler warns about non-used variable

  21. Redundant Code Sample 2 Non-used interface (var or function)

  22. Redundant Code Sample 3 Referenced but not really in use

  23. Redundant Code › Unreferenced declarations › How to find those

    unreferenced declaration? › Variables or functions that are not in use
  24. 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?
  25. SourceKit › Open source by Apple › Swift language tool

    › IDE supports › Indexing › Syntax highlight › Code completion
  26. sourcekitd › SourceKitService › XPC › Xcode send request ›

    sourcekitd response
  27. 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.
  28. Use SourceKit for parsing 📃 📋 💻 🔬 Source Code

    Parsed Result SourceKit Parsing Program XPC
  29. Example Source Code

  30. Example Parsing Result

  31. Find Declarations There is a public declaration of structure called

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

    variable sugar which is a SugarAmount Reference to another type Type Name
  33. Build Dependency MilkTea SugarAmount IceAmount Double sugar volume ice init()

  34. Build Dependency (Simplified) MilkTea SugarAmount IceAmount Double

  35. Build Dependency Graph Module A

  36. Build Dependency Graph across Modules Module A Module B

  37. Traverse Dependency Graph Module A Module B

  38. Find Unreached Nodes Module A Module B

  39. 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
  40. Practice in LINE Internal Modules Applied on 3 Binary size

    Reduced 44% For LINE app Saved 2.1MB
  41. 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.
  42. Thank you