[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.
This commit is contained in:
Daco Harkes 2025-05-15 07:17:23 +02:00 committed by GitHub
parent 540d19c148
commit 3cb2f5f6a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -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<Target> get dependencies => const <Target>[KernelSnapshot()];
List<Target> get dependencies => const <Target>[];
}
/// Installs the code assets from a [DartBuild] Flutter app.

View File

@ -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<KernelSnapshot>()));
});
testWithoutContext('NativeAssets throws error if missing target platform', () async {
iosEnvironment.defines.remove(kTargetPlatform);
expect(