From 3cb2f5f6a5588beee504be86eaa751994a408d42 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 15 May 2025 07:17:23 +0200 Subject: [PATCH] [native assets] Remove `KernelSnapshot` dependency in build (#168742) The link hooks (`hook/link.dart`) in Flutter have no access to the resource identifier experiment for tree-shaking yet. That means we can start running hooks earlier in the flutter build. When we do get around to adding tree-shaking information, we should only have a dependency on `KernelSnapshot` for AOT builds in which the Dart tree-shaker runs. Likely achieved by splitting the `DartBuildForNative` into two targets, one for Debug and one for Release. (Note, the names for these targets are somewhat weird, so we should address that in another PR as well. It should probably be called `RunDartHooks` or something.) I don't expect this PR to have any performance effect for fast hooks. Other targets already run concurrently with this target: ``` $ flutter build -d macos --verbose [ ] [ +278 ms] compile_macos_framework: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/flutter/examples/hello_world/.dart_tool/flutter_build/ce2ba8b9e7204bce6beba11f20d6df36/app.dill,/Users/dacoharkes/flt/flutter/packages/flutter_tools/lib/src/build_system/targets/macos.dart,/Users/dacoharkes/flt/flutter/bin/cache/engine.stamp} [...] [ ] [ ] dart_build: Starting due to {} [ ] [ +20 ms] No packages with native assets. Skipping native assets compilation. [ ] [ +1 ms] dart_build: Complete [ ] [ +1 ms] install_code_assets: Starting due to {} [ ] [ +1 ms] Writing native assets json to file:///Users/dacoharkes/flt/flutter/examples/hello_world/.dart_tool/flutter_build/ce2ba8b9e7204bce6beba11f20d6df36/native_assets.json. [ ] [ +1 ms] Writing /Users/dacoharkes/flt/flutter/examples/hello_world/.dart_tool/flutter_build/ce2ba8b9e7204bce6beba11f20d6df36/native_assets.json done. [ ] [ ] install_code_assets: Complete [ ] [+1376 ms] Building App.framework for x86_64... [...] [ ] [ +41 ms] compile_macos_framework: Complete ``` However, this PR makes the hooks already run concurrently with kernel compilation, which always takes quite a while. ``` [ ] [ +2 ms] kernel_snapshot_program: Starting due to {} [...] [ ] [+2515 ms] release_unpack_macos: Complete [ ] [+2425 ms] kernel_snapshot_program: Complete ``` This means that if the hooks do actual work, and enough cores on the host are available for Dart compilation and hook execution, the result will be quicker than before. ### Context * https://github.com/dart-lang/native/issues/2236 ### Testing Existing native assets integration tests ### Benchmarks Currently _not_ benchmarked. This will be benchmarked by the `flutter build` and flutter-time-to-first-frame benchmarks once the experiment flag is removed. --- .../lib/src/build_system/targets/native_assets.dart | 4 ++-- .../isolated/build_system/targets/native_assets_test.dart | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_system/targets/native_assets.dart b/packages/flutter_tools/lib/src/build_system/targets/native_assets.dart index 87c58721259..792e4e066e9 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/native_assets.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/native_assets.dart @@ -14,7 +14,6 @@ import '../../isolated/native_assets/native_assets.dart'; import '../build_system.dart'; import '../depfile.dart'; import '../exceptions.dart'; -import 'common.dart'; /// Runs the dart build of the app. abstract class DartBuild extends Target { @@ -121,8 +120,9 @@ abstract class DartBuild extends Target { class DartBuildForNative extends DartBuild { const DartBuildForNative({@visibleForTesting super.buildRunner}); + // TODO(dcharkes): Add `KernelSnapshot()` for AOT builds only when adding tree-shaking information. https://github.com/dart-lang/native/issues/153 @override - List get dependencies => const [KernelSnapshot()]; + List get dependencies => const []; } /// Installs the code assets from a [DartBuild] Flutter app. diff --git a/packages/flutter_tools/test/general.shard/isolated/build_system/targets/native_assets_test.dart b/packages/flutter_tools/test/general.shard/isolated/build_system/targets/native_assets_test.dart index d187182f5ec..9ed8f9d7c51 100644 --- a/packages/flutter_tools/test/general.shard/isolated/build_system/targets/native_assets_test.dart +++ b/packages/flutter_tools/test/general.shard/isolated/build_system/targets/native_assets_test.dart @@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_system/build_system.dart'; import 'package:flutter_tools/src/build_system/exceptions.dart'; +import 'package:flutter_tools/src/build_system/targets/common.dart'; import 'package:flutter_tools/src/build_system/targets/native_assets.dart'; import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/isolated/native_assets/native_assets.dart'; @@ -64,6 +65,11 @@ void main() { androidEnvironment.buildDir.createSync(recursive: true); }); + testWithoutContext('no dependency on KernelSnapshot', () async { + const DartBuildForNative target = DartBuildForNative(); + expect(target.dependencies, isNot(isA())); + }); + testWithoutContext('NativeAssets throws error if missing target platform', () async { iosEnvironment.defines.remove(kTargetPlatform); expect(