iOS slow startup time

2020-06-24 01:24发布

问题:

I have project in Swift and when I measure with DYLD_PRINT_STATISTICS I can see 1.0 second pre-init time, where 70% is dynamic libraries linking.

Are there any clean and safe ways of dealing with this problem?

回答1:

According to Apple's WWDC 2016 Session on Optimizing App Startup Time, regardless of their size, having a large number of dynamically linked libraries slows down app launch time dramatically.

To fix this, several dynamic libraries can be merged into a single library. If they are already static libraries, then libtool can be used to combine them, using the command from this SO answer. However, if they are not static, then to combine them, one must have access to their source code. If the source code is accessible, then literally copying the code from one library into another, and using the resulting library, will suffice.

Of course, merging disparate libraries into a single one is definitely inconvenient from the developer's perspective. To combat this, Xcode allows for different libraries to be linked when different flags (i.e. RELEASE and DEBUG) are set, as described in this forum.

When possible, it's better to merge static libraries, as the merging process is far less error prone. CocoaPods allows users to use static libraries in their projects.