Skip to content

Commit 8ebb8d4

Browse files
authored
Speed up native assets target (#134523)
Speeds up the native assets target in the backend by 1. changing other targets `gen_dart_plugin_registrant` and `release_unpack_ios` to do async I/O, 2. not reparsing the package config, and 3. not calling `dart pub deps --json` for 0 or 1 packages (fixed package:native_assets_builder). * flutter/flutter#134427 ``` [ +2 ms] native_assets: Starting due to {} [ +2 ms] Skipping target: gen_localizations [ +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset} [ +33 ms] gen_dart_plugin_registrant: Complete [ +107 ms] release_unpack_ios: Complete [ +60 ms] Writing native_assets.yaml. [ +7 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/be2692bbfbc0b9a27fcd2422d52354c6/native_assets.yaml done. [ ] native_assets: Complete ``` -> ``` [ +4 ms] native_assets: Starting due to {} [ ] Skipping target: gen_localizations [ +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset} [ +31 ms] Writing native_assets.yaml. [ +8 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/f9451a65a465bfab70d004e21d6cc1d6/native_assets.yaml done. [ +1 ms] native_assets: Complete ``` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent 72b69f9 commit 8ebb8d4

File tree

10 files changed

+145
-52
lines changed

10 files changed

+145
-52
lines changed

packages/flutter_tools/lib/src/build_system/targets/ios.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,21 @@ abstract class UnpackIOS extends Target {
287287
if (archs == null) {
288288
throw MissingDefineException(kIosArchs, name);
289289
}
290-
_copyFramework(environment, sdkRoot);
290+
await _copyFramework(environment, sdkRoot);
291291

292292
final File frameworkBinary = environment.outputDir.childDirectory('Flutter.framework').childFile('Flutter');
293293
final String frameworkBinaryPath = frameworkBinary.path;
294-
if (!frameworkBinary.existsSync()) {
294+
if (!await frameworkBinary.exists()) {
295295
throw Exception('Binary $frameworkBinaryPath does not exist, cannot thin');
296296
}
297-
_thinFramework(environment, frameworkBinaryPath, archs);
297+
await _thinFramework(environment, frameworkBinaryPath, archs);
298298
if (buildMode == BuildMode.release) {
299-
_bitcodeStripFramework(environment, frameworkBinaryPath);
299+
await _bitcodeStripFramework(environment, frameworkBinaryPath);
300300
}
301301
await _signFramework(environment, frameworkBinary, buildMode);
302302
}
303303

304-
void _copyFramework(Environment environment, String sdkRoot) {
304+
Future<void> _copyFramework(Environment environment, String sdkRoot) async {
305305
final EnvironmentType? environmentType = environmentTypeFromSdkroot(sdkRoot, environment.fileSystem);
306306
final String basePath = environment.artifacts.getArtifactPath(
307307
Artifact.flutterFramework,
@@ -310,7 +310,7 @@ abstract class UnpackIOS extends Target {
310310
environmentType: environmentType,
311311
);
312312

313-
final ProcessResult result = environment.processManager.runSync(<String>[
313+
final ProcessResult result = await environment.processManager.run(<String>[
314314
'rsync',
315315
'-av',
316316
'--delete',
@@ -328,16 +328,20 @@ abstract class UnpackIOS extends Target {
328328
}
329329

330330
/// Destructively thin Flutter.framework to include only the specified architectures.
331-
void _thinFramework(Environment environment, String frameworkBinaryPath, String archs) {
331+
Future<void> _thinFramework(
332+
Environment environment,
333+
String frameworkBinaryPath,
334+
String archs,
335+
) async {
332336
final List<String> archList = archs.split(' ').toList();
333-
final ProcessResult infoResult = environment.processManager.runSync(<String>[
337+
final ProcessResult infoResult = await environment.processManager.run(<String>[
334338
'lipo',
335339
'-info',
336340
frameworkBinaryPath,
337341
]);
338342
final String lipoInfo = infoResult.stdout as String;
339343

340-
final ProcessResult verifyResult = environment.processManager.runSync(<String>[
344+
final ProcessResult verifyResult = await environment.processManager.run(<String>[
341345
'lipo',
342346
frameworkBinaryPath,
343347
'-verify_arch',
@@ -355,7 +359,7 @@ abstract class UnpackIOS extends Target {
355359
}
356360

357361
// Thin in-place.
358-
final ProcessResult extractResult = environment.processManager.runSync(<String>[
362+
final ProcessResult extractResult = await environment.processManager.run(<String>[
359363
'lipo',
360364
'-output',
361365
frameworkBinaryPath,
@@ -374,8 +378,11 @@ abstract class UnpackIOS extends Target {
374378

375379
/// Destructively strip bitcode from the framework. This can be removed
376380
/// when the framework is no longer built with bitcode.
377-
void _bitcodeStripFramework(Environment environment, String frameworkBinaryPath) {
378-
final ProcessResult stripResult = environment.processManager.runSync(<String>[
381+
Future<void> _bitcodeStripFramework(
382+
Environment environment,
383+
String frameworkBinaryPath,
384+
) async {
385+
final ProcessResult stripResult = await environment.processManager.run(<String>[
379386
'xcrun',
380387
'bitcode_strip',
381388
frameworkBinaryPath,

packages/flutter_tools/lib/src/build_system/targets/macos.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ abstract class UnpackMacOS extends Target {
8080
if (!frameworkBinary.existsSync()) {
8181
throw Exception('Binary $frameworkBinaryPath does not exist, cannot thin');
8282
}
83-
_thinFramework(environment, frameworkBinaryPath);
83+
await _thinFramework(environment, frameworkBinaryPath);
8484
}
8585

8686
static const List<String> _copyDenylist = <String>['entitlements.txt', 'without_entitlements.txt'];
@@ -96,17 +96,21 @@ abstract class UnpackMacOS extends Target {
9696
}
9797
}
9898

99-
void _thinFramework(Environment environment, String frameworkBinaryPath) {
99+
Future<void> _thinFramework(
100+
Environment environment,
101+
String frameworkBinaryPath,
102+
) async {
100103
final String archs = environment.defines[kDarwinArchs] ?? 'x86_64 arm64';
101104
final List<String> archList = archs.split(' ').toList();
102-
final ProcessResult infoResult = environment.processManager.runSync(<String>[
105+
final ProcessResult infoResult =
106+
await environment.processManager.run(<String>[
103107
'lipo',
104108
'-info',
105109
frameworkBinaryPath,
106110
]);
107111
final String lipoInfo = infoResult.stdout as String;
108112

109-
final ProcessResult verifyResult = environment.processManager.runSync(<String>[
113+
final ProcessResult verifyResult = await environment.processManager.run(<String>[
110114
'lipo',
111115
frameworkBinaryPath,
112116
'-verify_arch',

packages/flutter_tools/lib/src/build_system/targets/native_assets.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import 'package:meta/meta.dart';
66
import 'package:native_assets_cli/native_assets_cli.dart' show Asset;
7+
import 'package:package_config/package_config_types.dart';
78

89
import '../../base/common.dart';
910
import '../../base/file_system.dart';
1011
import '../../base/platform.dart';
1112
import '../../build_info.dart';
13+
import '../../dart/package_map.dart';
1214
import '../../ios/native_assets.dart';
1315
import '../../macos/native_assets.dart';
1416
import '../../macos/xcode.dart';
@@ -52,7 +54,21 @@ class NativeAssets extends Target {
5254

5355
final Uri projectUri = environment.projectDir.uri;
5456
final FileSystem fileSystem = environment.fileSystem;
55-
final NativeAssetsBuildRunner buildRunner = _buildRunner ?? NativeAssetsBuildRunnerImpl(projectUri, fileSystem, environment.logger);
57+
final File packagesFile = fileSystem
58+
.directory(projectUri)
59+
.childDirectory('.dart_tool')
60+
.childFile('package_config.json');
61+
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
62+
packagesFile,
63+
logger: environment.logger,
64+
);
65+
final NativeAssetsBuildRunner buildRunner = _buildRunner ??
66+
NativeAssetsBuildRunnerImpl(
67+
projectUri,
68+
packageConfig,
69+
fileSystem,
70+
environment.logger,
71+
);
5672

5773
final List<Uri> dependencies;
5874
switch (targetPlatform) {

packages/flutter_tools/lib/src/flutter_plugins.dart

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ import 'platform_plugins.dart';
2626
import 'plugins.dart';
2727
import 'project.dart';
2828

29-
void _renderTemplateToFile(String template, Object? context, File file, TemplateRenderer templateRenderer) {
29+
Future<void> _renderTemplateToFile(
30+
String template,
31+
Object? context,
32+
File file,
33+
TemplateRenderer templateRenderer,
34+
) async {
3035
final String renderedTemplate = templateRenderer
3136
.renderString(template, context);
32-
file.createSync(recursive: true);
33-
file.writeAsStringSync(renderedTemplate);
37+
await file.create(recursive: true);
38+
await file.writeAsString(renderedTemplate);
3439
}
3540

36-
Plugin? _pluginFromPackage(String name, Uri packageRoot, Set<String> appDependencies, {FileSystem? fileSystem}) {
41+
Future<Plugin?> _pluginFromPackage(String name, Uri packageRoot, Set<String> appDependencies,
42+
{FileSystem? fileSystem}) async {
3743
final FileSystem fs = fileSystem ?? globals.fs;
3844
final File pubspecFile = fs.file(packageRoot.resolve('pubspec.yaml'));
3945
if (!pubspecFile.existsSync()) {
@@ -42,7 +48,7 @@ Plugin? _pluginFromPackage(String name, Uri packageRoot, Set<String> appDependen
4248
Object? pubspec;
4349

4450
try {
45-
pubspec = loadYaml(pubspecFile.readAsStringSync());
51+
pubspec = loadYaml(await pubspecFile.readAsString());
4652
} on YamlException catch (err) {
4753
globals.printTrace('Failed to parse plugin manifest for $name: $err');
4854
// Do nothing, potentially not a plugin.
@@ -85,7 +91,7 @@ Future<List<Plugin>> findPlugins(FlutterProject project, { bool throwOnError = t
8591
);
8692
for (final Package package in packageConfig.packages) {
8793
final Uri packageRoot = package.packageUriRoot.resolve('..');
88-
final Plugin? plugin = _pluginFromPackage(
94+
final Plugin? plugin = await _pluginFromPackage(
8995
package.name,
9096
packageRoot,
9197
project.manifest.dependencies,
@@ -445,7 +451,7 @@ Future<void> _writeAndroidPluginRegistrant(FlutterProject project, List<Plugin>
445451
templateContent = _androidPluginRegistryTemplateOldEmbedding;
446452
}
447453
globals.printTrace('Generating $registryPath');
448-
_renderTemplateToFile(
454+
await _renderTemplateToFile(
449455
templateContent,
450456
templateContext,
451457
globals.fs.file(registryPath),
@@ -774,20 +780,20 @@ Future<void> _writeIOSPluginRegistrant(FlutterProject project, List<Plugin> plug
774780
};
775781
if (project.isModule) {
776782
final Directory registryDirectory = project.ios.pluginRegistrantHost;
777-
_renderTemplateToFile(
783+
await _renderTemplateToFile(
778784
_pluginRegistrantPodspecTemplate,
779785
context,
780786
registryDirectory.childFile('FlutterPluginRegistrant.podspec'),
781787
globals.templateRenderer,
782788
);
783789
}
784-
_renderTemplateToFile(
790+
await _renderTemplateToFile(
785791
_objcPluginRegistryHeaderTemplate,
786792
context,
787793
project.ios.pluginRegistrantHeader,
788794
globals.templateRenderer,
789795
);
790-
_renderTemplateToFile(
796+
await _renderTemplateToFile(
791797
_objcPluginRegistryImplementationTemplate,
792798
context,
793799
project.ios.pluginRegistrantImplementation,
@@ -829,13 +835,13 @@ Future<void> _writeLinuxPluginFiles(FlutterProject project, List<Plugin> plugins
829835
}
830836

831837
Future<void> _writeLinuxPluginRegistrant(Directory destination, Map<String, Object> templateContext) async {
832-
_renderTemplateToFile(
838+
await _renderTemplateToFile(
833839
_linuxPluginRegistryHeaderTemplate,
834840
templateContext,
835841
destination.childFile('generated_plugin_registrant.h'),
836842
globals.templateRenderer,
837843
);
838-
_renderTemplateToFile(
844+
await _renderTemplateToFile(
839845
_linuxPluginRegistryImplementationTemplate,
840846
templateContext,
841847
destination.childFile('generated_plugin_registrant.cc'),
@@ -844,7 +850,7 @@ Future<void> _writeLinuxPluginRegistrant(Directory destination, Map<String, Obje
844850
}
845851

846852
Future<void> _writePluginCmakefile(File destinationFile, Map<String, Object> templateContext, TemplateRenderer templateRenderer) async {
847-
_renderTemplateToFile(
853+
await _renderTemplateToFile(
848854
_pluginCmakefileTemplate,
849855
templateContext,
850856
destinationFile,
@@ -860,7 +866,7 @@ Future<void> _writeMacOSPluginRegistrant(FlutterProject project, List<Plugin> pl
860866
'framework': 'FlutterMacOS',
861867
'methodChannelPlugins': macosMethodChannelPlugins,
862868
};
863-
_renderTemplateToFile(
869+
await _renderTemplateToFile(
864870
_swiftPluginRegistryTemplate,
865871
context,
866872
project.macos.managedDirectory.childFile('GeneratedPluginRegistrant.swift'),
@@ -931,13 +937,13 @@ Future<void> writeWindowsPluginFiles(FlutterProject project, List<Plugin> plugin
931937
}
932938

933939
Future<void> _writeCppPluginRegistrant(Directory destination, Map<String, Object> templateContext, TemplateRenderer templateRenderer) async {
934-
_renderTemplateToFile(
940+
await _renderTemplateToFile(
935941
_cppPluginRegistryHeaderTemplate,
936942
templateContext,
937943
destination.childFile('generated_plugin_registrant.h'),
938944
templateRenderer,
939945
);
940-
_renderTemplateToFile(
946+
await _renderTemplateToFile(
941947
_cppPluginRegistryImplementationTemplate,
942948
templateContext,
943949
destination.childFile('generated_plugin_registrant.cc'),
@@ -955,7 +961,7 @@ Future<void> _writeWebPluginRegistrant(FlutterProject project, List<Plugin> plug
955961

956962
final String template = webPlugins.isEmpty ? _noopDartPluginRegistryTemplate : _dartPluginRegistryTemplate;
957963

958-
_renderTemplateToFile(
964+
await _renderTemplateToFile(
959965
template,
960966
context,
961967
pluginFile,
@@ -1411,8 +1417,8 @@ Future<void> generateMainDartWithPluginRegistrant(
14111417
final File newMainDart = rootProject.dartPluginRegistrant;
14121418
if (resolutions.isEmpty) {
14131419
try {
1414-
if (newMainDart.existsSync()) {
1415-
newMainDart.deleteSync();
1420+
if (await newMainDart.exists()) {
1421+
await newMainDart.delete();
14161422
}
14171423
} on FileSystemException catch (error) {
14181424
globals.printWarning(
@@ -1428,7 +1434,7 @@ Future<void> generateMainDartWithPluginRegistrant(
14281434
(templateContext[resolution.platform] as List<Object?>?)?.add(resolution.toMap());
14291435
}
14301436
try {
1431-
_renderTemplateToFile(
1437+
await _renderTemplateToFile(
14321438
_dartPluginRegistryForNonWebTemplate,
14331439
templateContext,
14341440
newMainDart,

0 commit comments

Comments
 (0)