From b5e522e2002f1b616e689b5012c9a42bbc9321f4 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 5 Jul 2017 12:49:29 -0700 Subject: [PATCH] Skip AOT snapshot build if inputs are unchanged (#11084) Previously, the snapshot file was recomputed on every build. We now record checksums for all snapshot inputs (which are catalogued in the snapshot dependencies file output alongside the snapshot) and only rebuild if the checksum for any input file (or the previous output file) has changed. --- .../flutter_tools/lib/src/base/build.dart | 19 +++++ .../lib/src/commands/build_aot.dart | 77 +++++++++++++++---- packages/flutter_tools/lib/src/flx.dart | 22 +----- 3 files changed, 81 insertions(+), 37 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/build.dart b/packages/flutter_tools/lib/src/base/build.dart index 6b48ea6455..bc0a17f7aa 100644 --- a/packages/flutter_tools/lib/src/base/build.dart +++ b/packages/flutter_tools/lib/src/base/build.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:convert' show JSON; import 'package:crypto/crypto.dart' show md5; @@ -42,3 +43,21 @@ class Checksum { @override int get hashCode => _checksums.hashCode; } + +/// Parses a VM snapshot dependency file. +/// +/// Snapshot dependency files are a single line mapping the output snapshot to a +/// space-separated list of input files used to generate that output. e.g, +/// +/// outfile : file1.dart file2.dart file3.dart +Future> readDepfile(String depfilePath) async { + // Depfile format: + // outfile1 outfile2 : file1.dart file2.dart file3.dart + final String contents = await fs.file(depfilePath).readAsString(); + final String dependencies = contents.split(': ')[1]; + return dependencies + .split(' ') + .map((String path) => path.trim()) + .where((String path) => path.isNotEmpty) + .toSet(); +} diff --git a/packages/flutter_tools/lib/src/commands/build_aot.dart b/packages/flutter_tools/lib/src/commands/build_aot.dart index 22e0b2ca23..66284c2160 100644 --- a/packages/flutter_tools/lib/src/commands/build_aot.dart +++ b/packages/flutter_tools/lib/src/commands/build_aot.dart @@ -5,6 +5,7 @@ import 'dart:async'; import '../artifacts.dart'; +import '../base/build.dart'; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/logger.dart'; @@ -149,13 +150,15 @@ Future _buildAotSnapshot( final String uiPath = fs.path.join(skyEnginePkg, 'lib', 'ui', 'ui.dart'); final String vmServicePath = fs.path.join(skyEnginePkg, 'sdk_ext', 'vmservice_io.dart'); - final List filePaths = [ + final List inputPaths = [ vmEntryPoints, ioEntryPoints, uiPath, vmServicePath, ]; + final Set outputPaths = new Set(); + // These paths are used only on iOS. String snapshotDartIOS; String assembly; @@ -164,13 +167,15 @@ Future _buildAotSnapshot( case TargetPlatform.android_arm: case TargetPlatform.android_x64: case TargetPlatform.android_x86: + outputPaths.addAll([ + vmSnapshotData, + isolateSnapshotData, + ]); break; case TargetPlatform.ios: snapshotDartIOS = artifacts.getArtifactPath(Artifact.snapshotDart, platform, buildMode); assembly = fs.path.join(outputDir.path, 'snapshot_assembly.S'); - filePaths.addAll([ - snapshotDartIOS, - ]); + inputPaths.add(snapshotDartIOS); break; case TargetPlatform.darwin_x64: case TargetPlatform.linux_x64: @@ -179,9 +184,9 @@ Future _buildAotSnapshot( assert(false); } - final List missingFiles = filePaths.where((String p) => !fs.isFileSync(p)).toList(); - if (missingFiles.isNotEmpty) { - printError('Missing files: $missingFiles'); + final Iterable missingInputs = inputPaths.where((String p) => !fs.isFileSync(p)); + if (missingInputs.isNotEmpty) { + printError('Missing input files: $missingInputs'); return null; } if (!processManager.canRun(genSnapshot)) { @@ -207,6 +212,17 @@ Future _buildAotSnapshot( genSnapshotCmd.add('--embedder_entry_points_manifest=$ioEntryPoints'); } + // iOS symbols used to load snapshot data in the engine. + const String kVmSnapshotData = 'kDartVmSnapshotData'; + const String kIsolateSnapshotData = 'kDartIsolateSnapshotData'; + + // iOS snapshot generated files, compiled object files. + final String kVmSnapshotDataC = fs.path.join(outputDir.path, '$kVmSnapshotData.c'); + final String kIsolateSnapshotDataC = fs.path.join(outputDir.path, '$kIsolateSnapshotData.c'); + final String kVmSnapshotDataO = fs.path.join(outputDir.path, '$kVmSnapshotData.o'); + final String kIsolateSnapshotDataO = fs.path.join(outputDir.path, '$kIsolateSnapshotData.o'); + final String assemblyO = fs.path.join(outputDir.path, 'snapshot_assembly.o'); + switch (platform) { case TargetPlatform.android_arm: case TargetPlatform.android_x64: @@ -223,9 +239,14 @@ Future _buildAotSnapshot( if (interpreter) { genSnapshotCmd.add('--snapshot_kind=core'); genSnapshotCmd.add(snapshotDartIOS); + outputPaths.addAll([ + kVmSnapshotDataO, + kIsolateSnapshotDataO, + ]); } else { genSnapshotCmd.add('--snapshot_kind=app-aot-assembly'); genSnapshotCmd.add('--assembly=$assembly'); + outputPaths.add(assemblyO); } break; case TargetPlatform.darwin_x64: @@ -244,6 +265,27 @@ Future _buildAotSnapshot( genSnapshotCmd.add(mainPath); + final File checksumFile = fs.file('$dependencies.checksum'); + final List checksumFiles = [checksumFile, fs.file(dependencies)] + ..addAll(inputPaths.map(fs.file)) + ..addAll(outputPaths.map(fs.file)); + if (checksumFiles.every((File file) => file.existsSync())) { + try { + final String json = await checksumFile.readAsString(); + final Checksum oldChecksum = new Checksum.fromJson(json); + final Set snapshotInputPaths = await readDepfile(dependencies); + snapshotInputPaths.addAll(outputPaths); + final Checksum newChecksum = new Checksum.fromFiles(snapshotInputPaths); + if (oldChecksum == newChecksum) { + printTrace('Skipping AOT snapshot build. Checksums match.'); + return outputPath; + } + } catch (e, s) { + // Log exception and continue, this step is a performance improvement only. + printTrace('Error during AOT snapshot checksum check: $e\n$s'); + } + } + final RunResult results = await runAsync(genSnapshotCmd); if (results.exitCode != 0) { printError('Dart snapshot generator failed with exit code ${results.exitCode}'); @@ -260,16 +302,6 @@ Future _buildAotSnapshot( if (platform == TargetPlatform.ios) { printStatus('Building App.framework...'); - // These names are known to from the engine. - const String kVmSnapshotData = 'kDartVmSnapshotData'; - const String kIsolateSnapshotData = 'kDartIsolateSnapshotData'; - - final String kVmSnapshotDataC = fs.path.join(outputDir.path, '$kVmSnapshotData.c'); - final String kIsolateSnapshotDataC = fs.path.join(outputDir.path, '$kIsolateSnapshotData.c'); - final String kVmSnapshotDataO = fs.path.join(outputDir.path, '$kVmSnapshotData.o'); - final String kIsolateSnapshotDataO = fs.path.join(outputDir.path, '$kIsolateSnapshotData.o'); - final String assemblyO = fs.path.join(outputDir.path, 'snapshot_assembly.o'); - final List commonBuildOptions = ['-arch', 'arm64', '-miphoneos-version-min=8.0']; if (interpreter) { @@ -316,5 +348,16 @@ Future _buildAotSnapshot( await runCheckedAsync(linkCommand); } + // Compute and record checksums. + try { + final Set snapshotInputPaths = await readDepfile(dependencies); + snapshotInputPaths..addAll(outputPaths); + final Checksum checksum = new Checksum.fromFiles(snapshotInputPaths); + await checksumFile.writeAsString(checksum.toJson()); + } catch (e, s) { + // Log exception and continue, this step is a performance improvement only. + printTrace('Error during AOT snapshot checksum output: $e\n$s'); + } + return outputPath; } diff --git a/packages/flutter_tools/lib/src/flx.dart b/packages/flutter_tools/lib/src/flx.dart index 996b302340..b947be26c9 100644 --- a/packages/flutter_tools/lib/src/flx.dart +++ b/packages/flutter_tools/lib/src/flx.dart @@ -65,7 +65,7 @@ Future _createSnapshot({ try { final String json = await checksumFile.readAsString(); final Checksum oldChecksum = new Checksum.fromJson(json); - final Set inputPaths = await _readDepfile(depfilePath); + final Set inputPaths = await readDepfile(depfilePath); inputPaths.add(snapshotPath); final Checksum newChecksum = new Checksum.fromFiles(inputPaths); if (oldChecksum == newChecksum) { @@ -85,7 +85,7 @@ Future _createSnapshot({ // Compute and record input file checksums. try { - final Set inputPaths = await _readDepfile(depfilePath); + final Set inputPaths = await readDepfile(depfilePath); inputPaths.add(snapshotPath); final Checksum checksum = new Checksum.fromFiles(inputPaths); await checksumFile.writeAsString(checksum.toJson()); @@ -96,24 +96,6 @@ Future _createSnapshot({ return 0; } -/// Parses a VM snapshot dependency file. -/// -/// Snapshot dependency files are a single line mapping the output snapshot to a -/// space-separated list of input files used to generate that output. e.g, -/// -/// outfile : file1.dart file2.dart file3.dart -Future> _readDepfile(String depfilePath) async { - // Depfile format: - // outfile : file1.dart file2.dart file3.dart - final String contents = await fs.file(depfilePath).readAsString(); - final String dependencies = contents.split(': ')[1]; - return dependencies - .split(' ') - .map((String path) => path.trim()) - .where((String path) => path.isNotEmpty) - .toSet(); -} - Future build({ String mainPath: defaultMainPath, String manifestPath: defaultManifestPath,