From 5ccd5a1e756f1527adff3ba988ad3211bce642c3 Mon Sep 17 00:00:00 2001 From: jensjoha Date: Wed, 31 Jan 2018 16:39:58 +0100 Subject: [PATCH] [kernel/flutter] Improve speed of first hot reload (#13699) * [kernel/flutter] Improve speed of first hot reload * [kernel/flutter] Improve speed of first hot reload * Revert "[kernel/flutter] Improve speed of first hot reload" This reverts commit f3dc133878ec7405a8efb5715f1cfe720f4ea05f. * Revert "[kernel/flutter] Improve speed of first hot reload" This reverts commit f7bcb082b01cf9cf48f02c6aad9e5c79705def8f. * Revert "Revert "[kernel/flutter] Improve speed of first hot reload"" This reverts commit 73c7a1a8e85a4c0397984d8b65a98b2f71bf84f3. * [kernel/flutter] Improve speed of first hot reload Do so by not evicting unnecessary paths (while still evicting paths that was changed between the generation of the package file and the initial upload). * Fix grammar in method name * Fix bad merge * Add method to super class --- packages/flutter_tools/lib/src/asset.dart | 5 ++++ packages/flutter_tools/lib/src/devfs.dart | 25 +++++++++++++++++-- .../lib/src/resident_runner.dart | 4 +++ packages/flutter_tools/lib/src/run_hot.dart | 8 +++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/asset.dart b/packages/flutter_tools/lib/src/asset.dart index a49c084d18..8d9a751c78 100644 --- a/packages/flutter_tools/lib/src/asset.dart +++ b/packages/flutter_tools/lib/src/asset.dart @@ -35,6 +35,8 @@ abstract class AssetBundle { Map get entries; + bool wasBuiltOnce(); + bool needsBuild({String manifestPath: _ManifestAssetBundle.defaultManifestPath}); /// Returns 0 for success; non-zero for failure. @@ -89,6 +91,9 @@ class _ManifestAssetBundle implements AssetBundle { } } + @override + bool wasBuiltOnce() => _lastBuildTimestamp != null; + @override bool needsBuild({String manifestPath: defaultManifestPath}) { if (_fixed) diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index 96711f61ff..6882208764 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -34,6 +34,11 @@ abstract class DevFSContent { /// or if the entry has been modified since this method was last called. bool get isModified; + /// Return true if this is the first time this method is called + /// or if the entry has been modified after the given time + /// or if the given time is null. + bool isModifiedAfter(DateTime time); + int get size; Future> contentsAsBytes(); @@ -103,6 +108,13 @@ class DevFSFileContent extends DevFSContent { return _oldFileStat == null || _fileStat.modified.isAfter(_oldFileStat.modified); } + @override + bool isModifiedAfter(DateTime time) { + final FileStat _oldFileStat = _fileStat; + _stat(); + return _oldFileStat == null || time == null || _fileStat.modified.isAfter(time); + } + @override int get size { if (_fileStat == null) @@ -124,12 +136,14 @@ class DevFSByteContent extends DevFSContent { List _bytes; bool _isModified = true; + DateTime _modificationTime = new DateTime.now(); List get bytes => _bytes; set bytes(List value) { _bytes = value; _isModified = true; + _modificationTime = new DateTime.now(); } /// Return true only once so that the content is written to the device only once. @@ -140,6 +154,11 @@ class DevFSByteContent extends DevFSContent { return modified; } + @override + bool isModifiedAfter(DateTime time) { + return time == null || _modificationTime.isAfter(time); + } + @override int get size => _bytes.length; @@ -383,6 +402,8 @@ class DevFS { String mainPath, String target, AssetBundle bundle, + DateTime firstBuildTime, + bool bundleFirstUpload: false, bool bundleDirty: false, Set fileFilter, ResidentCompiler generator, @@ -445,10 +466,10 @@ class DevFS { // that isModified does not reset last check timestamp because we // want to report all modified files to incremental compiler next time // user does hot reload. - if (content.isModified || (bundleDirty && archivePath != null)) { + if (content.isModified || ((bundleDirty || bundleFirstUpload) && archivePath != null)) { dirtyEntries[deviceUri] = content; numBytes += content.size; - if (archivePath != null) + if (archivePath != null && (!bundleFirstUpload || content.isModifiedAfter(firstBuildTime))) assetPathsToEvict.add(archivePath); } }); diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 3f42d0be6d..a7c520ae27 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -365,6 +365,8 @@ class FlutterDevice { String mainPath, String target, AssetBundle bundle, + DateTime firstBuildTime, + bool bundleFirstUpload: false, bool bundleDirty: false, Set fileFilter, bool fullRestart: false @@ -379,6 +381,8 @@ class FlutterDevice { mainPath: mainPath, target: target, bundle: bundle, + firstBuildTime: firstBuildTime, + bundleFirstUpload: bundleFirstUpload, bundleDirty: bundleDirty, fileFilter: fileFilter, generator: generator, diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index a4000ada32..039f440063 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -66,6 +66,7 @@ class HotRunner extends ResidentRunner { // The initial launch is from a snapshot. bool _runningFromSnapshot = true; bool previewDart2 = false; + DateTime firstBuildTime; bool strongMode = false; void _addBenchmarkData(String name, int value) { @@ -213,6 +214,8 @@ class HotRunner extends ResidentRunner { return 1; } + firstBuildTime = new DateTime.now(); + for (FlutterDevice device in flutterDevices) { final int result = await device.runHot( hotRunner: this, @@ -262,6 +265,7 @@ class HotRunner extends ResidentRunner { // Did not update DevFS because of a Dart source error. return false; } + final bool isFirstUpload = assetBundle.wasBuiltOnce() == false; final bool rebuildBundle = assetBundle.needsBuild(); if (rebuildBundle) { printTrace('Updating assets'); @@ -275,7 +279,9 @@ class HotRunner extends ResidentRunner { mainPath: mainPath, target: target, bundle: assetBundle, - bundleDirty: rebuildBundle, + firstBuildTime: firstBuildTime, + bundleFirstUpload: isFirstUpload, + bundleDirty: isFirstUpload == false && rebuildBundle, fileFilter: _dartDependencies, fullRestart: fullRestart );