Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10755] Application initialization time noticably higher with code compiled with Swift 5 vs Swift 4 toolchain on iOS device #53145

Open
swift-ci opened this issue May 23, 2019 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10755
Radar rdar://problem/51154245
Original Reporter wernera77 (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 10.2.1 (Swift 5.0.1) vs Xcode 10.1 (Swift 4.2.1)
iPhone 6 Plus iOS 12.1

Additional Detail from JIRA
Votes 3
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5bd354a24a1dabff011cbd391f18d3cc

Issue Description:

We had problems with the app launch time deteriorating quite dramatically when compiling our code base with the Swift 5 toolchain.

Attached is a profile trace for the exact code base, using the same set of dynamic Swift libraries. The only difference is the compiler version: Xcode 10.2.1 with Swift 5.0.1 toolchain vs Xcode 10.1 with the Swift 4.2.1 toolchain.

As you can see in the trace (focus on the initialization part) the swift 5.0.1 version has a 100% CPU block lasting for a few seconds, which is not there with swift 4.2.1.

Testing has been done with an iPhone 6 plus on iOS 12.1, but same issue exist with newer devices (the total time will be reduced there though) and with newer iOS versions (12.2 and 12.3).

Looking at the trace a culprit seems to be calls to realpath (dyld)

Edit: iOS 11 does not seem to exhibit the same problem.

@belkadan
Copy link
Contributor

Can you share your project, even just with Apple at https://bugreport.apple.com? If not, can you share a built IPA for your app?

@belkadan
Copy link
Contributor

Suspicion: Xcode 10.3 is somehow adding a lot more Runtime Search Paths, or this app has so many dynamic libraries that adding one more Runtime Search Path (/usr/lib/swift) caused…well, that seems unrealistic.

Most of the time within dyld is spent on realpath, as noted by Werner on the forums; most of the time within that is spent on getattrlist.

@swift-ci
Copy link
Collaborator Author

Comment by Artem Loenko (JIRA)

It seems that we have the same issue in Badoo. And, yeah, we have a lot of dynamic libraries. I will try to gather the data for our applications today and attach it to the ticket.

@swift-ci
Copy link
Collaborator Author

Comment by Werner Altewischer (JIRA)

I created an associated bug at bugreport.apple.com: https://bugreport.apple.com/web/?problemID=51154245

I uploaded the two IPAs used for testing there: one compiled with Swift 5.0 and one compiled with Swift 4.2.1.

@belkadan
Copy link
Contributor

Thanks!

@swift-ci
Copy link
Collaborator Author

Comment by Artem Loenko (JIRA)

Sorry for the delay, took some time to investigate the problem.
It seems that we have a different issue. In our case, the regression during the startup caused not by Swift Toolchain but by the recent iOS releases.

We tested the same code-base with Swift 4.2.1/5.0.1 and on iOS 12.1.4/12.3.1. You can check the results on the screenshot below:

The strangest thing is that the regression is not "visible" when you ask dyld to print statistics during the launch. But it is visible with dtrace.

Right now, we have the following conclusions:

  • the regression is related to dyld behaviour that probably was changed with iOS 12.2 release (ABI or shared Swift standard libraries perhaps?);

  • the main degradations in the performance are within ImageLoader::recursiveBindWithAccounting and ImageLoader::recursiveInitialization;

  • the issue is not related directly to Swift Toolchain ❓.

So, I created a radar (51257011) for Apple Xcode/build system team.
Let me know if you need more information or have questions.

P.S. the main reason is the same as in the initial description but it is not reproducible on iOS < 12.2 for us:

  • dyld behaviour has changed between iOS 12.1.4 and iOS 12.3 (12.2?)

    • From Xcode 10.2 Release Notes: Swift libraries are now found in the dyld cache when the main project isn’t written in Swift. (48385698);
  • the main differences are within ImageLoader::recursiveBindWithAccounting call when dyld calls ImageLoaderMegaDylib::findImageIndex:

    • on iOS < 12.3 the next call is ImageLoaderMegaDylib::hasDylib which tries to passthrough the tree with ImageLoader::trieWalk and it looks like a cheap operation;

    • on iOS >= 12.3 the next call is ImageLoaderMachO::getRPaths which tries to call realpath$DARWIN_EXTSN and realpath is reading attributes with getattrlist function;

  • the second regression is related to ImageLoaderMegaDylib::findExportedSymbolAddress:

    • on iOS < 12.3 it calls ImageLoaderMachOCompressed::findShallowExportedSymbol;

    • on iOS >= 12.3 it calls ImageLoaderMegaDylib::findImageIndex which calls ImageLoaderMachO::getRPaths as in the first scenario;

  • it looks like the getattrlist is very expensive because CPU usage on these calls is around 100% and IO is affected as well.

@swift-ci
Copy link
Collaborator Author

Comment by Artem Loenko (JIRA)

wernera77 (JIRA User), hi. Do you have any news about the issue? We found the main reason (in the comments above) but it seems that it is impossible to solve it on our side. So, we are trying to switch to static linking to reduce the regression.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants