Mobile Development 15 min read

Baidu App iOS Package Size Optimization: Code Optimization Techniques

The article explains how Baidu App reduces its iOS package size by analyzing Link Map files and applying six code‑optimization techniques—unused class and module slimming, unused method pruning with LLVM, duplicate‑code detection, utility‑method consolidation, and A/B‑test hardening—yielding up to 8 MB of savings.

Baidu App Technology
Baidu App Technology
Baidu App Technology
Baidu App iOS Package Size Optimization: Code Optimization Techniques

This article is part of the Baidu App iOS package size optimization series, focusing on code optimization techniques. It begins by explaining Link Map files, which are auxiliary files for Mach-O format binary files that describe the executable's structure, including information about compiled object files and their storage details in code and data segments. The article then details six code optimization strategies implemented in Baidu App:

1. Unused Class Optimization : This includes both static and dynamic detection methods. Static detection analyzes Link Map and Mach-O files to identify unused classes by comparing class lists and references. Dynamic detection uses runtime initialization flags in Objective-C meta-classes to determine if classes are actually used.

2. Unused Module Slimming : Identifies modules that are no longer used by calculating the ratio of unused classes to total classes in each module. Modules with high ratios can be removed or migrated.

3. Unused Method Slimming : Addresses the challenge of identifying unused methods by combining static analysis with LLVM plugin development to track method calls and eliminate false positives from special cases like category methods, inheritance chains, and reflection calls.

4. Code Duplication Reduction : Uses the PMD tool's CPD (Copy/Paste Detector) to identify duplicate code across the codebase. The tool supports multiple languages and can be customized with specific detection rules.

5. Tool Method Slimming : Identifies and consolidates duplicate utility methods, particularly focusing on redundant category implementations and standalone utility functions that have proliferated across different modules.

6. AB Test Hardening : Removes code from completed A/B experiments by identifying and deleting branches that are no longer active. This involves extracting experiment keys from source code and comparing them against completed experiments.

The article concludes by noting that while code optimization offers significant potential (20MB identified, 8MB implemented), it's more challenging than image or resource optimization due to the broader impact of code changes and the dynamic nature of Objective-C. The author promises future articles with more detailed technical implementations.

performance optimizationAB testingiOScode analysisMach-Opackage optimizationObjective-CLink MapPMDunused code
Baidu App Technology
Written by

Baidu App Technology

Official Baidu App Tech Account

0 followers
Reader feedback

How this landed with the community

login 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.