1
Keeping Swift Apps Small
Why and how
Bruno Rocha - iOS Engineer @ Spotify
App Size
2
App Size
2
•Why do we care?
•How do we track it?
•How do we improve it?
App Size
2
Should I care about my
app’s size?
3
4
Programming in the 90s
~300 KB
•Storage is extremely expensive
•Slow / weak hardware
•Little to no access to decent internet (if at all)
Focus: High optimization
5
349 MB
Programming today
Focus: Great experiences
•Storage is cheap (Base flagship iPhones have 128GB!)
•Fast / strong hardware
•High-speed internet is widely available
6
So why bother about app
size in 2023?
So why bother about app size in
2023?
7
So why bother about app size in
2023?
7
Developed World
🇩🇪🇺🇸🇸🇪🇫🇷🇬🇧, etc.
So why bother about app size in
2023?
7
Developed World
🇩🇪🇺🇸🇸🇪🇫🇷🇬🇧, etc.
Emerging Markets
🇧🇷🇮🇳🇨🇳🇲🇽🇹🇷, etc.
8
Devices and data usage in emerging markets
🇧🇷🇮🇳🇨🇳🇲🇽🇹🇷, etc.
8
Devices and data usage in emerging markets
🇧🇷🇮🇳🇨🇳🇲🇽🇹🇷, etc.
•Devices are usually older gen ones (16GB)
8
Devices and data usage in emerging markets
🇧🇷🇮🇳🇨🇳🇲🇽🇹🇷, etc.
•Devices are usually older gen ones (16GB)
•Data can be prohibitively expensive and constrained
8
Devices and data usage in emerging markets
🇧🇷🇮🇳🇨🇳🇲🇽🇹🇷, etc.
•Devices are usually older gen ones (16GB)
•Data can be prohibitively expensive and constrained
•Little to no access to decent internet (if at all)
9
9
hmmmmmmm
10
in 2017:
10
70% of people consider the size of
an app before downloading it
in 2017:
(Emerging markets)
10
70% of people consider the size of
an app before downloading it
in 2017:
(85% of the world’s population)
10
70% of people consider the size of
an app before downloading it
in 2017:
For every 6MB increase, install conversion drops by 1%
(Android)
(85% of the world’s population)
…which means
11
Might be even worse for iOS!
11
Might be even worse for iOS!
•Apps larger than 200mb have extra friction
Should I care about my
app’s size?
12
Should I care about my
app’s size?
12
85% of the population says yes!
App Size @ Spotify
13
14
App Size @ Spotify
14
App Size @ Spotify
Team Attribution
CI Checks
Tracking Alerting
Charts
Policies
Linters Team Pages
… …
… …
15
App Size CI Check
15
App Size CI Check
Push PR
15
App Size CI Check
Push PR CI compiles the app
15
App Size CI Check
Push PR CI compiles the app CI checks the binary
15
App Size CI Check
Push PR CI compiles the app CI checks the binary
15
App Size CI Check
Push PR CI compiles the app CI checks the binary
16
App Size CI Check
Push PR CI compiles the app CI checks the binary
16
App Size CI Check
Push PR CI compiles the app CI checks the binary
Difference is too big = PR gets blocked
Important sidenote!
17
Important sidenote!
17
18
Install Size != Download Size
18
Install Size != Download Size
Install Size
18
Install Size != Download Size
Install Size
•Actual downloaded package may be smaller
18
Install Size != Download Size
Install Size
•Actual downloaded package may be smaller
•Both can be predicted by generating an App
Thinning Size Report through Xcode
18
Install Size != Download Size
Install Size
•Actual downloaded package may be smaller
•Both can be predicted by generating an App
Thinning Size Report through Xcode
•Companies usually focus on Install Size
specifically
19
CI Checks
Push PR CI compiles the app CI checks the binary
19
CI Checks
Push PR CI compiles the app CI checks the binary
Install size!
19
CI Checks
Push PR CI compiles the app CI checks the binary
What if you can’t optimize it?
20
App Size Policy
20
App Size Policy
20
App Size Policy
Do the benefits outweighs the impact on app size?
20
App Size Policy
Do the benefits outweighs the impact on app size?
(This is checked recurrently!)
21
App Size @ Spotify
Policies
Alerting
Team Attribution
CI Checks
Tracking
Charts
Linters Team Pages
… …
… …
Team Attribution
CI Checks
Tracking Alerting
Charts
Linters Team Pages
… …
… …
21
App Size @ Spotify
Policies
Alerting
Team Attribution
CI Checks
Tracking
Charts
Linters Team Pages
… …
… …
21
App Size @ Spotify
Policies
Alerting
Team Attribution
CI Checks
Tracking
Charts
Linters Team Pages
… …
… …
CI Checks
Optimizing iOS apps for
size
22
23
Delete everything
Size optimization tip 1:
23
Delete everything
Size optimization tip 1:
no
24
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
Optimizing apps for size
24
Remove overhead from the binary
Optimizing apps for size
25
Optimizing apps for size
25
Optimizing apps for size
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
Overhead
26
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
Optimizing apps for size
26
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Reality: Complex problem, complex solutions
Optimizing apps for size
27
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Group 1
Group 2
Group 3
Optimizing apps for size
27
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Group 1
Group 2
Group 3
Objective: Overview
•What does it mean?
•Examples & Tips
•What else?
Optimizing apps for size
Group 1
Group 2
Group 3
28
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Optimizing apps for size
Group 1
Group 2
Group 3
28
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Swift Overhead
Optimizing apps for size
Group 1
Group 2
Group 3
28
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Swift Overhead
Resources Overhead
Optimizing apps for size
Group 1
Group 2
Group 3
28
App Package
Swift
iOS
Hardware
Compiler
The iPhone Stack
Swift Overhead
Resources Overhead
Project/OS
Overhead
Optimizing apps for size
29
Resources Overhead
29
•R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
Resources Overhead
29
•R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
R
•U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
U
•N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
Resources Overhead
29
Resources Overhead
29
Resources Overhead
Problem: Usingthewrong file formats
30
Usingthewrong file formats
Resources Overhead
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
•Text content, like JSONs: Sanitize them in order to remove
things you don’t need
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
•Text content, like JSONs: Sanitize them in order to remove
things you don’t need
•Lottie animation JSONs have many fields that can be safely
stripped away
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
•Audio: CAF (format) + AAC (codec) for short effects (Lossy)
•Text content, like JSONs: Sanitize them in order to remove
things you don’t need
•Lottie animation JSONs have many fields that can be safely
stripped away
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
•Audio: CAF (format) + AAC (codec) for short effects (Lossy)
•For regular audio: try MP3 codecs with lower bit-rates
•Text content, like JSONs: Sanitize them in order to remove
things you don’t need
•Lottie animation JSONs have many fields that can be safely
stripped away
30
Usingthewrong file formats
Resources Overhead
•Images: SVG for small icons, HEIC for everything else
•Audio: CAF (format) + AAC (codec) for short effects (Lossy)
•⚠ Warning: Audio is complicated!
•For regular audio: try MP3 codecs with lower bit-rates
•Text content, like JSONs: Sanitize them in order to remove
things you don’t need
•Lottie animation JSONs have many fields that can be safely
stripped away
31
Resources Overhead
31
Resources Overhead
Problem: Placing resources inthe
wrong place
32
Placing resources inthewrong place
Resources Overhead
32
Placing resources inthewrong place
Resources Overhead
•Always use Asset Catalogs
32
Placing resources inthewrong place
Resources Overhead
•Always use Asset Catalogs
•Reduces code signature overhead
32
Placing resources inthewrong place
Resources Overhead
•Always use Asset Catalogs
•Reduces code signature overhead
•Makes your app compress better
32
Placing resources inthewrong place
Resources Overhead
•Always use Asset Catalogs
•Use lossy compression for short-lived images
•Reduces code signature overhead
•Makes your app compress better
33
•Looking for more?
Resources Overhead
33
•Looking for more?
Resources Overhead
33
•Looking for more?
•WWDC sessions about resources
Resources Overhead
34
The iPhone Stack
App Package
Swift
iOS
Hardware
Compiler
Swift Overhead
Project/OS
Overhead
Resources Overhead
Optimizing apps for size
35
The iPhone Stack
Swift Overhead
Resources Overhead
App Package
Swift
iOS
Hardware
Compiler
Project/OS
Overhead
Optimizing apps for size
36
Swift Overhead
36
•Certain Swift patterns can be very expensive for app size
Swift Overhead
36
•Certain Swift patterns can be very expensive for app size
Swift Overhead
•Optimize for Size (-Osize)
•Whole Module Optimization
•Important: Make sure these flags are enabled in Xcode!
36
•Certain Swift patterns can be very expensive for app size
Swift Overhead
•Optimize for Size (-Osize)
•Whole Module Optimization
•Important: Make sure these flags are enabled in Xcode!
•⚠ Be aware that this can affect performance
37
Swift Overhead
37
Swift Overhead
Problem: Improperusage ofstructs
38
Swift Overhead Improperusage ofstructs
•Struct composition is expensive
38
Swift Overhead Improperusage ofstructs
•Struct composition is expensive
38
Swift Overhead Improperusage ofstructs
•Struct composition is expensive
38
Swift Overhead Improperusage ofstructs
•Struct composition is expensive
This can be really bad!
Potential impact: Dozens of megabytes!
39
Swift Overhead Improperusage ofstructs
•Other potentially expensive patterns:
39
Swift Overhead Improperusage ofstructs
•Other potentially expensive patterns:
Reference types in structs Mutability in structs
40
Swift Overhead Improperusage ofstructs
40
Swift Overhead Improperusage ofstructs
This is a compiler issue. You should probably ignore this.
40
Swift Overhead Improperusage ofstructs
This is a compiler issue. You should probably ignore this.
Optimization Tip (1):
Don’t forget about classes!
Still…
40
Swift Overhead Improperusage ofstructs
This is a compiler issue. You should probably ignore this.
Optimization Tip (1):
Don’t forget about classes!
Optimization Tip (2):
Using SwiftUI? Be extra careful.
Still…
41
Swift Overhead
41
Problem: Unused code
Swift Overhead
42
Swift Overhead Unused code
42
Swift Overhead Unused code
43
Swift Overhead Unused code
43
Swift Overhead Unused code
43
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
44
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
44
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
Equatable Hashable Codable
(Protocols with compiler-generated conformances)
45
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
45
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
46
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
46
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
•Potential impact: Several megabytes
46
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
Optimization Tip (1):
Only add protocols if you truly need them
•Potential impact: Several megabytes
46
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
Optimization Tip (1):
Only add protocols if you truly need them
•Potential impact: Several megabytes
46
Swift Overhead Unused code
•Unnecessary Dynamic Dispatching
•Potential impact: Several megabytes
Optimization Tip (2):
Be familiar with Dynamic Dispatching in Swift
47
Swift Overhead
•Looking for more?
47
•Official Swift Forums
Swift Overhead
•Looking for more?
47
•Official Swift Forums
Swift Overhead
•Looking for more?
•WWDC sessions about Swift
48
The iPhone Stack
Swift Overhead
Resources Overhead
App Package
Swift
iOS
Hardware
Compiler
Project/OS
Overhead
Optimizing apps for size
49
The iPhone Stack
Swift Overhead
Resources Overhead
App Package
Swift
iOS
Hardware
Compiler
Project/OS
Overhead
Optimizing apps for size
50
Project/OS Overhead
50
•⚠ Requires dedicated infrastructure teams
Project/OS Overhead
50
•⚠ Requires dedicated infrastructure teams
•⚠ Often requires deep knowledge of Swift and iOS
Project/OS Overhead
50
•⚠ Requires dedicated infrastructure teams
•⚠ Often requires deep knowledge of Swift and iOS
•⚠ Not something you would do for “individual" PRs
Project/OS Overhead
50
•⚠ Requires dedicated infrastructure teams
•⚠ Often requires deep knowledge of Swift and iOS
•⚠ Not something you would do for “individual" PRs
Don’ttrythis at home!
Project/OS Overhead
51
Project/OS Overhead
51
Problem:Xcode’s native localization
is inefficient
Project/OS Overhead
52
Project/OS Overhead Xcode’s native localization is inefficient
52
// Comment for string A
“stringA" = “Button”
// Comment for string B
“stringB" = “Another Button”
// Comment for string C
“stringC" = “Cool Button”
// Comment for string D
“stringD” = “Bad Button"
en.strings
// Comment for string A
“stringA" = “Botão”
// Comment for string B
“stringB" = “Outro Button”
// Comment for string C
“stringC" = “Botão Legal”
// Comment for string D
“stringD” = “Botão Ruim”
pt-BR.strings
Project/OS Overhead Xcode’s native localization is inefficient
53
en.values
[“Botão,"Outro Botão”,“Botão
Legal”,“Botão Ruim”]
pt-BR.values
["Button,"Another Button”,“Cool
Button”,“Bad Button”]
•Improve efficiency by storing them yourself!
keys
[“stringA,"stringB”,“stringC”,
“stringD”]
Project/OS Overhead Xcode’s native localization is inefficient
53
en.values
[“Botão,"Outro Botão”,“Botão
Legal”,“Botão Ruim”]
pt-BR.values
["Button,"Another Button”,“Cool
Button”,“Bad Button”]
•Improve efficiency by storing them yourself!
keys
[“stringA,"stringB”,“stringC”,
“stringD”]
Improvement @ Spotify: -20MB
Project/OS Overhead Xcode’s native localization is inefficient
54
Project/OS Overhead
54
Project/OS Overhead
•Previously, on this talk…
55
Project/OS Overhead
55
Project/OS Overhead
•LLVM has optimization flags not exposed to Xcode
55
Project/OS Overhead
•Machine Outlining
-mllvm —enable-machine-outliner=always -mllvm —machine-outliner-reruns=NO_OF_RERUNS
•LLVM has optimization flags not exposed to Xcode
55
Project/OS Overhead
•Machine Outlining
-mllvm —enable-machine-outliner=always -mllvm —machine-outliner-reruns=NO_OF_RERUNS
•ThinLTO (Cross Module Optimization)
-
fl
to=thin
•LLVM has optimization flags not exposed to Xcode
55
Project/OS Overhead
•Machine Outlining
-mllvm —enable-machine-outliner=always -mllvm —machine-outliner-reruns=NO_OF_RERUNS
•ThinLTO (Cross Module Optimization)
-
fl
to=thin
•LLVM has optimization flags not exposed to Xcode
Don’ttrythis at home!
56
Swift Overhead
•Looking for more?
Project/OS Overhead
56
•Official Swift Forums
Swift Overhead
•Looking for more?
Project/OS Overhead
56
•Official Swift Forums
Swift Overhead
•Looking for more?
•Official LLVM Forums
Project/OS Overhead
56
•Official Swift Forums
Swift Overhead
•Looking for more?
•Official LLVM Forums
Project/OS Overhead
•Large companies’ developer blogs
56
•Official Swift Forums
Swift Overhead
•Looking for more?
•Official LLVM Forums
Project/OS Overhead
•Large companies’ developer blogs
•MobileNativeFoundation
57
57
😫
58
Overwhelmed?
Start simple!
58
Overwhelmed?
Start simple!
•Start by simply being aware of the problem
58
Overwhelmed?
Start simple!
•Start by simply being aware of the problem
•Don’t worry about the tech until you actually need it
59
@rockbruno_
https:/
/swiftrocks.com

Keeping Swift Apps Small