|
32 | 32 |
|
33 | 33 | static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin"; |
34 | 34 |
|
35 | | -// Finds a bundle with the named `bundleID`. |
36 | | -// |
37 | | -// `+[NSBundle bundleWithIdentifier:]` is slow, and can take in the order of |
38 | | -// tens of milliseconds in a minimal flutter app, and closer to 100 milliseconds |
39 | | -// in a medium sized Flutter app. It is likely that the slowness comes from |
40 | | -// having to traverse and load all bundles known to the process. Using |
41 | | -// `+[NSBundle allframeworks]` and filtering also suffers from the same problem. |
| 35 | +// Finds a bundle with the named `bundleID` within `searchURL`. |
42 | 36 | // |
43 | | -// This implementation is an optimization to limit the search space with a hint. |
44 | | -// Callers can provide a `hintURL` for where the bundle is expected to be |
45 | | -// located in. If the desired bundle cannot be found here, the implementation |
46 | | -// falls back to `+[NSBundle bundleWithIdentifier:]`. |
47 | | -static NSBundle* FLTBundleWithIdentifier(NSString* bundleID, NSURL* hintURL) { |
48 | | - NSArray<NSURL*>* candidates = [[NSFileManager defaultManager] |
49 | | - contentsOfDirectoryAtURL:hintURL |
50 | | - includingPropertiesForKeys:@[] |
51 | | - options:0 |
52 | | - // Not interested in the error as there is a fallback. |
53 | | - error:nil]; |
54 | | - |
55 | | - for (NSURL* candidate in candidates) { |
| 37 | +// Returns `nil` if the bundle cannot be found or if errors are encountered. |
| 38 | +NSBundle* FLTFrameworkBundleInternal(NSString* bundleID, NSURL* searchURL) { |
| 39 | + NSDirectoryEnumerator<NSURL*>* frameworkEnumerator = [NSFileManager.defaultManager |
| 40 | + enumeratorAtURL:searchURL |
| 41 | + includingPropertiesForKeys:nil |
| 42 | + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | |
| 43 | + NSDirectoryEnumerationSkipsHiddenFiles |
| 44 | + // Skip directories where errors are encountered. |
| 45 | + errorHandler:nil]; |
| 46 | + |
| 47 | + for (NSURL* candidate in frameworkEnumerator) { |
56 | 48 | NSBundle* bundle = [NSBundle bundleWithURL:candidate]; |
57 | 49 | if ([bundle.bundleIdentifier isEqualToString:bundleID]) { |
58 | 50 | return bundle; |
59 | 51 | } |
60 | 52 | } |
| 53 | + return nil; |
| 54 | +} |
61 | 55 |
|
| 56 | +// Finds a bundle with the named `bundleID`. |
| 57 | +// |
| 58 | +// `+[NSBundle bundleWithIdentifier:]` is slow, and can take in the order of |
| 59 | +// tens of milliseconds in a minimal flutter app, and closer to 100 milliseconds |
| 60 | +// in a medium sized Flutter app on an iPhone 13. It is likely that the slowness |
| 61 | +// comes from having to traverse and load all bundles known to the process. |
| 62 | +// Using `+[NSBundle allframeworks]` and filtering also suffers from the same |
| 63 | +// problem. |
| 64 | +// |
| 65 | +// This implementation is an optimization to first limit the search space to |
| 66 | +// `+[NSBundle privateFrameworksURL]` of the main bundle, which is usually where |
| 67 | +// frameworks used by this file are placed. If the desired bundle cannot be |
| 68 | +// found here, the implementation falls back to |
| 69 | +// `+[NSBundle bundleWithIdentifier:]`. |
| 70 | +NS_INLINE NSBundle* FLTFrameworkBundleWithIdentifier(NSString* bundleID) { |
| 71 | + NSBundle* bundle = FLTFrameworkBundleInternal(bundleID, NSBundle.mainBundle.privateFrameworksURL); |
| 72 | + if (bundle != nil) { |
| 73 | + return bundle; |
| 74 | + } |
62 | 75 | // Fallback to slow implementation. |
63 | 76 | return [NSBundle bundleWithIdentifier:bundleID]; |
64 | 77 | } |
|
77 | 90 |
|
78 | 91 | bool hasExplicitBundle = bundle != nil; |
79 | 92 | if (bundle == nil) { |
80 | | - // The default build system for Flutter places the default bundle in the |
81 | | - // same directory as the engine bundle (as they are both frameworks). |
82 | | - NSURL* defaultBundleHintURL = [engineBundle.bundleURL URLByDeletingLastPathComponent]; |
83 | | - bundle = |
84 | | - FLTBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier], defaultBundleHintURL); |
| 93 | + bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]); |
85 | 94 | } |
86 | 95 | if (bundle == nil) { |
87 | 96 | bundle = mainBundle; |
@@ -344,7 +353,7 @@ - (instancetype)initWithSettings:(const flutter::Settings&)settings { |
344 | 353 |
|
345 | 354 | + (NSString*)flutterAssetsName:(NSBundle*)bundle { |
346 | 355 | if (bundle == nil) { |
347 | | - bundle = [NSBundle bundleWithIdentifier:[FlutterDartProject defaultBundleIdentifier]]; |
| 356 | + bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]); |
348 | 357 | } |
349 | 358 | if (bundle == nil) { |
350 | 359 | bundle = [NSBundle mainBundle]; |
|
0 commit comments