flutter
flutter copied to clipboard
NSBundle bundleWithIdentifier causes slowdown when creating FlutterEngine
The addition of traversing the bundles by searching for an identifier added in https://github.com/flutter/engine/pull/5986/files#diff-8e54aca7e9d4fd69a57fb86d6ec3dee2R36 adds a 100ms latency when instantiating a FlutterEngine (7ms -> 106ms on an iPod).
It's hard to figure out the original feature request from the PR. Can we revise the approach?
Internally we wanted to support having multiple bundles and not rely on the main bundle when looking up Flutter assets. (I'm still trying to refresh my memory about exactly why... I think we considered it a burden for add2app?)
The +[NSBundle bundleForClass]:
call to initialize engineBundle
could be moved to where it's used. I'm not sure what to do about the +[NSBundle bundleWithIdentifier:]
call. Possibly we could replace it with +[NSBundle bundleWithPath:]
(although since the point of finding the bundle is to extract paths from it, probably not). Maybe another possibility would be to change the ordering so that the expensive lookup is done only after checking cheaper sources.
Anyway, I'm happy to ponder this and assist however I can, but I currently am no longer set up to make and test changes, so this probably would be better owned by someone else.
For context, the original feature request was b/112004455

FWIW [FlutterDartProject init] takes 4ms on iPhone SE. Debug build at dd77b73d7e03fcbbd49536a3b58395f086b91d8d
Edit: This was recorded on a full Flutter app, not Add-to-app.
Could have been fixed already. Feel free to close this.
Previous observations with https://github.com/flutter/engine/pull/5986/files
and without
I measured with the same previous settings, but an Add-to-app scenario and didn't notice any problem either.

The slowdown is plausible. I just don't know what conditions lead to [NSBundle bundleWithIdentifier:]
being so slow. Any ideas?
The [discussion on [NSBundle bundleWithIdentifier:]](https://developer.apple.com/documentation/foundation/nsbundle/1411929-bundlewithidentifier?language=objc)
has a section on performance implications of this call and suggests alternatives. Specifically:
As an optimization, you can use the bundleWithPath: or bundleWithURL: method instead to avoid file system traversal.
hi,the problem caused watchdog crash.Is there a repair plan?
@gaaclarke Our project use engineGroup, and there are some watchdog crash when create engine.
Could u please kindly check this?
btw: Flutter version is 2.8.1.
It seems like this could be proportionate to the size of the app. For example, customer: chalk
is more impacted by it than the raw hello flutter app. Googlers - see http://shortn/_WcifhO48X6 for details.
Tested with build_engine=1 internally - as a quick experiment, removing the call to +[NSBundle bundleWithIdentifier]
doesn't break the app, and improves startup of customer: chalk
by 30 - 50%. It also seems like the runtime of this method scales to the size of the app bundle.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v
and a minimal reproduction of the issue.