Mobile Development 25 min read

Startup Optimization Practices for 58 Tongcheng iOS App: Measurement, Method Timing Detection, Binary Reordering, and Lazy Loading

This article details how 58 Tongcheng improved iOS app startup speed by defining launch time, building automated measurement tools, detecting slow methods via hooking, applying binary reordering with Clang sanitizers, and implementing dynamic library lazy loading, achieving significant reductions in launch latency and page faults.

58 Tech
58 Tech
58 Tech
Startup Optimization Practices for 58 Tongcheng iOS App: Measurement, Method Timing Detection, Binary Reordering, and Lazy Loading

Startup speed is critical for user experience; the 58 Tongcheng app faces challenges due to complex business logic and many SDK initializations.

Key challenges include accurately measuring launch time, locating slow methods, and monitoring version regressions, leading to a systematic governance approach.

Launch time is defined as the interval from icon tap to first screen display, comprising pre‑main (dynamic library loading, rebase, bind, ObjC initialization, initializers) and post‑main phases (SDK registration and business initialization).

A custom launch‑time statistics tool reads iOS system logs (log‑power‑xxx.session), extracts app_bundleid and launch sessions, automates data collection, and enables cross‑app launch time comparison.

Online user launch time can be obtained via Xcode Organizer, process start time code, or a custom dynamic library with a +load hook placed first in the linking order.

/**获取进程创建时间*/
+ (NSTimeInterval)processStartTime {
struct kinfo_proc kinfo;
if ([self processInfoForPID:[[NSProcessInfo processInfo] processIdentifier] procInfo:&kinfo]) {
return kinfo.kp_proc.p_un.__p_starttime.tv_sec * 1000.0 + kinfo.kp_proc.p_un.__p_starttime.tv_usec / 1000.0;
} else {
return 0;
}
}
+ (BOOL)processInfoForPID:(int)pid procInfo:(struct kinfo_proc*)procInfo {
int cmd[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
size_t size = sizeof(*procInfo);
return sysctl(cmd, sizeof(cmd)/sizeof(*cmd), procInfo, &size, NULL, 0) == 0;
}

The method‑time detection tool monitors both pre‑main ( +load , C/C++ constructors) and post‑main methods, records execution times, and exports reports to Excel.

IMP originLoadIMP = origin_load_method->imp;
IMP hookLoadIMP = imp_implementationWithBlock(^(__unsafe_unretained id self, SEL cmd){
uint64_t starttime = currentTime();
((void (*)(id, SEL))originLoadIMP)(self, cmd);
uint64_t endtime = currentTime();
recordLoadTrace(array, invokeMethodName, endtime - starttime);
});
origin_load_method->imp = hookLoadIMP;

Load‑method optimizations include replacing heavy +load with +initialize , deferring work after launch, or using Clang attribute sections to control execution order.

For Objective‑C method timing, objc_msgSend is hooked via fishhook; call records are stored with depth information to reconstruct call stacks.

typedef struct {
__unsafe_unretained Class cls;
SEL sel;
uint64_t time;
int depth;
} LTCallRecord; // one method call record
typedef struct {
LTCallRecord *record;
int allocated_length;
int index;
} LTMainThreadCallRecord; // call queue

Binary reordering is achieved by collecting function symbols via Clang SanitizerCoverage, generating an order file for the linker, and thus reducing page‑faults; experiments show ~1626 fewer page faults and a 162 ms launch time reduction on iPhone 6S.

Dynamic‑library lazy loading removes selected libraries from link flags and loads them at runtime with dlopen , decreasing startup overhead; 12 libraries now lazy‑loaded, saving ~817 ms on iPhone 6P (iOS 12).

In summary, combining launch‑time measurement, method‑time detection, binary reordering, and lazy loading yields substantial startup performance gains, and future work will focus on further pre‑main optimizations.

performance optimizationiOSlazy-loadingapp startupBinary ReorderingMethod Timing
58 Tech
Written by

58 Tech

Official tech channel of 58, a platform for tech innovation, sharing, and communication.

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.