[flutter_tools] skip copying 1GB of data from chrome cache dirs (#81170)

* [flutter_tools] skip copying 1GB of data from chrome cache dirs

* Update chrome.dart
This commit is contained in:
Jonah Williams
2021-04-26 13:02:42 -07:00
committed by GitHub
parent b74df388e1
commit 52661bc713
4 changed files with 75 additions and 6 deletions

View File

@@ -107,10 +107,12 @@ String getDisplayPath(String fullPath, FileSystem fileSystem) {
/// source/destination file pair.
///
/// Skips files if [shouldCopyFile] returns `false`.
/// Does not recurse over directories if [shouldCopyDirectory] returns `false`.
void copyDirectory(
Directory srcDir,
Directory destDir, {
bool Function(File srcFile, File destFile)? shouldCopyFile,
bool Function(Directory)? shouldCopyDirectory,
void Function(File srcFile, File destFile)? onFileCopied,
}) {
if (!srcDir.existsSync()) {
@@ -134,6 +136,9 @@ void copyDirectory(
newFile.writeAsBytesSync(entity.readAsBytesSync());
onFileCopied?.call(entity, newFile);
} else if (entity is Directory) {
if (shouldCopyDirectory != null && !shouldCopyDirectory(entity)) {
continue;
}
copyDirectory(
entity,
destDir.fileSystem.directory(newPath),

View File

@@ -318,13 +318,24 @@ class ChromiumLauncher {
///
/// Note: more detailed docs of the Chrome user preferences store exists here:
/// https://www.chromium.org/developers/design-documents/preferences.
///
/// This intentionally skips the Cache, Code Cache, and GPUCache directories.
/// While we're not sure exactly what is in them, this constitutes nearly 1 GB
/// of data for a fresh flutter run and adds significant overhead to all startups.
/// For workflows that may require this data, using the start-paused flag and
/// dart debug extension with a user controlled browser profile will lead to a
/// better experience.
void _cacheUserSessionInformation(Directory userDataDir, Directory cacheDir) {
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path, _chromeDefaultPath));
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath));
if (sourceChromeDefault.existsSync()) {
targetChromeDefault.createSync(recursive: true);
try {
copyDirectory(sourceChromeDefault, targetChromeDefault);
copyDirectory(
sourceChromeDefault,
targetChromeDefault,
shouldCopyDirectory: _isNotCacheDirectory
);
} on FileSystemException catch (err) {
// This is a best-effort update. Display the message in case the failure is relevant.
// one possible example is a file lock due to multiple running chrome instances.
@@ -352,13 +363,24 @@ class ChromiumLauncher {
try {
if (sourceChromeDefault.existsSync()) {
targetChromeDefault.createSync(recursive: true);
copyDirectory(sourceChromeDefault, targetChromeDefault);
copyDirectory(
sourceChromeDefault,
targetChromeDefault,
shouldCopyDirectory: _isNotCacheDirectory,
);
}
} on FileSystemException catch (err) {
_logger.printError('Failed to restore Chrome preferences: $err');
}
}
// Cache, Code Cache, and GPUCache are nearly 1GB of data
bool _isNotCacheDirectory(Directory directory) {
return !directory.path.endsWith('Cache') &&
!directory.path.endsWith('Code Cache') &&
!directory.path.endsWith('GPUCache');
}
Future<Chromium> _connect(Chromium chrome, bool skipCheck) async {
// The connection is lazy. Try a simple call to make sure the provided
// connection is valid.