From fcbe8f44eadc21f09cb3690d98e4de22e7200c93 Mon Sep 17 00:00:00 2001 From: "James D. Lin" Date: Fri, 4 Oct 2019 17:22:35 -0700 Subject: [PATCH] Restructure ProjectFileInvalidator.findInvalidated a bit (#42008) Restructure ProjectFileInvalidator.findInvalidated a bit I plan to modify `ProjectFileInvalidator.findInvalidated` to allow it to use `FileStat.stat` instead of `FileStat.statSync`. Restructure `findInvalidated` a bit so that `FileStat.statSync` is called in only one place. Note that this change now always counts the `.packages` file as one of the files scanned, even if it does not exist. I think that this is okay since it more accurately reflects the number of times that we hit the filesystem with `stat()`, and it is consistent with how other files are counted. --- packages/flutter_tools/lib/src/run_hot.dart | 36 ++++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index aec03e94c67..bbea5990d14 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -1049,33 +1049,37 @@ class ProjectFileInvalidator { assert(packagesPath != null); if (lastCompiled == null) { + // Initial load. assert(urisToMonitor.isEmpty); return []; } - final List invalidatedFiles = []; - int scanned = 0; final Stopwatch stopwatch = Stopwatch()..start(); - for (Uri uri in urisToMonitor) { - if ((platform.isWindows && uri.path.contains(_pubCachePathWindows)) - || uri.path.contains(_pubCachePathLinuxAndMac)) { - // Don't watch pub cache directories to speed things up a little. - continue; - } + + final List urisToScan = [ + // Don't watch pub cache directories to speed things up a little. + ...urisToMonitor.where(_isNotInPubCache), + + // We need to check the .packages file too since it is not used in compilation. + fs.file(packagesPath).uri, + ]; + final List invalidatedFiles = []; + for (final Uri uri in urisToScan) { final DateTime updatedAt = fs.statSync( uri.toFilePath(windows: platform.isWindows)).modified; - scanned++; if (updatedAt != null && updatedAt.isAfter(lastCompiled)) { invalidatedFiles.add(uri); } } - // We need to check the .packages file too since it is not used in compilation. - final DateTime packagesUpdatedAt = fs.statSync(packagesPath).modified; - if (packagesUpdatedAt != null && packagesUpdatedAt.isAfter(lastCompiled)) { - invalidatedFiles.add(fs.file(packagesPath).uri); - scanned++; - } - printTrace('Scanned through $scanned files in ${stopwatch.elapsedMilliseconds}ms'); + printTrace( + 'Scanned through ${urisToScan.length} files in ' + '${stopwatch.elapsedMilliseconds}ms', + ); return invalidatedFiles; } + + static bool _isNotInPubCache(Uri uri) { + return !(platform.isWindows && uri.path.contains(_pubCachePathWindows)) + && !uri.path.contains(_pubCachePathLinuxAndMac); + } }