Mobile Development 21 min read

How Baidu’s iOS App Cut 30 MB with Compiler and Architecture Optimizations

This article details Baidu APP’s iOS package‑size optimization series, focusing on compiler tweaks (GCC, Swift, LTO, symbol stripping, dead‑code removal, asset catalog settings, C++ virtual‑function reduction, third‑party SDK slimming), instruction‑set pruning, Xcode‑14 improvements, and Swift dynamic‑library handling, together with concrete Xcode configuration steps and measured size gains.

Baidu App Technology
Baidu App Technology
Baidu App Technology
How Baidu’s iOS App Cut 30 MB with Compiler and Architecture Optimizations

1. Introduction

The Baidu APP iOS package‑size optimization series consists of six articles covering overall size‑reduction strategy, image optimization, resource optimization, code optimization, unused‑class removal, HEIC image handling, and unused‑method cleanup. This article concentrates on compiler‑related optimizations, which affect Objective‑C, C, and C++ code.

2. Compiler Optimizations

2.1 Overview

Compiler optimizations are applied through Xcode build settings and affect all source languages used in the project.

2.2 GCC Language Compilation Optimization

Enabling -Oz in GCC produces smaller binaries for Objective‑C, C, and C++. In Xcode the path is Build Settings → Apple Clang → Code Generation → Optimize Level , where -Oz is selected instead of the default -Os. WWDC 2019’s “What’s New in Clang and LLVM” explains that -Oz outlines identical code sequences across functions, replacing them with a shared out‑lined function, which reduces binary size at the cost of a modest increase in call‑stack depth. In practice Baidu APP observed a 25 % size reduction for Objective‑C++ and up to 30 % for pure C/C++ code.

GCC optimization diagram
GCC optimization diagram

2.3 Swift Compilation Optimization

Swift optimizations combine Optimization Level and Compilation Mode under Build Settings → Swift Compiler → Code Generation . The -Osize level mirrors GCC’s -Oz by outlining repeated machine instructions. Enabling Whole Module Optimization together with -Osize yields roughly a 10 % reduction in Swift binary size. The available options are shown in the WWDC 2019 video and the Xcode UI screenshots.

Swift optimization levels
Swift optimization levels

2.4 LTO (Link‑Time Optimization)

LTO performs whole‑program analysis during the link phase, allowing function inlining, dead‑code elimination, and global optimizations. Enable it via Build Settings → Apple Clang → Code Generation → Link‑Time Optimization and set the value to Incremental for both the main target and any framework that should be slimmed.

LTO configuration
LTO configuration

LTO benefits include function inlining, removal of unused symbols, and global branch pruning. Drawbacks are reduced Link Map readability and longer compile/link times.

2.5 Strip Debug Symbols

Set Build Settings → Strip Linked Product to YES. This hides symbols by default, making them private extern and slightly shrinking the binary. For debug builds keep it NO to retain symbol information.

Strip Debug Symbols
Strip Debug Symbols

2.6 Strip Symbol Table

Enable Strip Linked Product (same setting as above) and change Debug Information Format to “DWARF with dSYM file” so that debugging can still be performed via the generated dSYM.

Strip Symbol Table
Strip Symbol Table

2.7 Dead Code Stripping

Activate Build Settings → Dead Code Stripping and set it to YES. This removes unused static code at link time for C, C++, and Swift. It does not affect Objective‑C because of its dynamic runtime.

Dead Code Stripping = YES

2.8 Asset Catalog Optimization

Configure Build Settings → Asset Catalog Compiler → Optimization . Selecting “Space” reduces the size of compiled asset catalogs, though the gain is modest.

Asset optimization options
Asset optimization options

2.9 Reducing C++ Virtual Functions

Fewer virtual functions shrink the virtual‑function table, decreasing binary size. Replacing virtual calls with static ones where possible yields measurable size reductions.

2.10 Third‑Party SDK Compiler Slimming

Each framework must inherit the same compiler settings as the main target. Baidu APP applied the above flags to its top‑15 third‑party SDKs, achieving an additional 30 MB reduction across the app bundle.

3. Instruction‑Set Architecture Optimization

3.1 iPhone Common Architectures

iPhones use low‑power ARM cores (armv6, armv7, armv7s, arm64). Modern devices support arm64, and simulators run x86_64 (or i386 for 32‑bit). Supporting only arm64 (and x86_64 for simulators) is sufficient for current hardware.

iPhone architecture matrix
iPhone architecture matrix

3.2 Architecture Settings

Architectures: Standard architectures - $(ARCHS_STANDARD) (armv7 + arm64 on device, x86_64 + i386 + arm64 on simulator).

Build Active Architectures Only: YES compiles only the current device’s architecture (typically arm64 on device, x86_64 on simulator).

Excluded Architectures: specify architectures to omit, e.g., exclude arm64 to produce a binary without that slice.

Architecture build settings
Architecture build settings

3.3 Removing Unused Architectures

Use the lipo tool to extract required slices and recombine them, thereby discarding unnecessary architectures. lipo -info AbcSDK.framework/AbcSDK Example output shows a fat binary containing x86_64, i386, arm64, and armv7.

lipo -thin arm64 -output AbcArm64 AbcSDK.framework/AbcSDK</code>
<code>lipo -thin x86_64 -output AbcArmX86_64 AbcSDK.framework/AbcSDK
lipo -create AbcArm64 AbcArmX86_64 -output newAbc
lipo -info newAbc

Replace the original binary with the newly‑created thin binary to keep only arm64 and x86_64 slices.

4. Xcode Upgrade Optimization

Xcode 14, released in October 2022, introduces stronger parallel compilation and several size‑saving codegen improvements: message‑send calls shrink from 12 bytes to 8 bytes, retain/release calls from 8 bytes to 4 bytes, and autorelease‑related mov instructions are removed, saving 4 bytes each. These changes are documented in the WWDC 2022 session “Improve App Size and Runtime Performance”.

5. Swift Built‑in Dynamic Library Optimization

For iOS versions prior to 12.2 the system lacks built‑in Swift libraries, so Xcode bundles the required Swift frameworks into the IPA. Raising the minimum deployment target to iOS 12.2 eliminates these bundled libraries. In Baidu APP this reduced the IPA size by over 30 MB, with a 20 MB reduction on iPhone X and older devices, but the benefit vanished on newer devices. Because the low‑end device share is < 5 % and raising the deployment target would cause user churn, the team decided not to adopt this optimization.

6. Summary

Among code, resource, and image optimizations, compiler‑level tweaks deliver the highest ROI for package‑size reduction, but they also affect every compiled module, so strict quality control is essential. Baidu APP’s compiler optimizations alone saved 30 MB, and the same approach was applied to its top‑15 third‑party SDKs, achieving comparable gains.

7. References

GCC optimization options: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

LTO documentation: https://llvm.org/docs/LinkTimeOptimization.html

Xcode build setting reference: https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW102

What’s New in Clang and LLVM (WWDC 2019): https://developer.apple.com/videos/play/wwdc2019/409/

Xcode 14 release notes: https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

Improve app size and runtime performance (WWDC 2022): https://developer.apple.com/videos/play/wwdc2022/110363/#

architectureiOSSwiftXcodeCompiler OptimizationPackage Size OptimizationLTO
Baidu App Technology
Written by

Baidu App Technology

Official Baidu App Tech Account

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.