Let frontend_server bootstrap the incremental compiler from a previou… (flutter/engine#4642)

* Let frontend_server bootstrap the incremental compiler from a previously build dill file

* Wrap 'invalidateIfBootstrapping' in try and don't bootstrap if getting an exception
This commit is contained in:
jensjoha
2018-02-07 23:03:05 +01:00
committed by Alexander Aprelev
parent b3d3a1781b
commit b7a3eb583d

View File

@@ -2,21 +2,21 @@ library frontend_server;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:io' hide FileSystemEntity;
import 'package:args/args.dart';
// front_end/src imports below that require lint `ignore_for_file`
// are a temporary state of things until frontend team builds better api
// that would replace api used below. This api was made private in
// an effort to discourage further use.
// ignore_for_file: implementation_imports
import 'package:front_end/src/api_prototype/compiler_options.dart';
import 'package:front_end/src/api_prototype/file_system.dart' show FileSystemEntity;
import 'package:front_end/src/api_prototype/incremental_kernel_generator.dart';
import 'package:kernel/ast.dart';
import 'package:kernel/binary/ast_to_binary.dart';
import 'package:kernel/binary/limited_ast_to_binary.dart';
import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes;
import 'package:kernel/target/flutter.dart';
import 'package:kernel/target/targets.dart';
import 'package:usage/uuid/uuid.dart';
@@ -133,6 +133,8 @@ class _FrontendCompiler implements CompilerInterface {
IncrementalKernelGenerator _generator;
String _kernelBinaryFilename;
String _kernelBinaryFilenameIncremental;
String _kernelBinaryFilenameFull;
@override
Future<Null> compile(
@@ -141,7 +143,9 @@ class _FrontendCompiler implements CompilerInterface {
IncrementalKernelGenerator generator,
}) async {
final Uri filenameUri = Uri.base.resolveUri(new Uri.file(filename));
_kernelBinaryFilename = '$filename.dill';
_kernelBinaryFilenameFull = '$filename.dill';
_kernelBinaryFilenameIncremental = '$filename.incremental.dill';
_kernelBinaryFilename = _kernelBinaryFilenameFull;
final String boundaryKey = new Uuid().generateV4();
_outputStream.writeln('result $boundaryKey');
final Uri sdkRoot = _ensureFolderPath(options['sdk-root']);
@@ -156,9 +160,9 @@ class _FrontendCompiler implements CompilerInterface {
if (options['incremental']) {
_entryPoint = filenameUri;
_compilerOptions = compilerOptions;
_generator = generator ?? _createGenerator();
program =
await _runWithPrintRedirection(() => _generator.computeDelta());
_generator = generator ?? _createGenerator(Uri.base.resolve(_kernelBinaryFilenameFull));
await invalidateIfBootstrapping();
program = await _runWithPrintRedirection(() => _generator.computeDelta());
} else {
if (options['link-platform']) {
// TODO(aam): Remove linkedDependencies once platform is directly embedded
@@ -178,21 +182,64 @@ class _FrontendCompiler implements CompilerInterface {
printer.writeProgramFile(program);
await sink.close();
_outputStream.writeln('$boundaryKey $_kernelBinaryFilename');
_kernelBinaryFilename = _kernelBinaryFilenameIncremental;
} else
_outputStream.writeln(boundaryKey);
return null;
}
Future<Null> invalidateIfBootstrapping() async {
if (_kernelBinaryFilename != _kernelBinaryFilenameFull)
return null;
try {
final File f = new File(_kernelBinaryFilenameFull);
if (!f.existsSync())
return null;
final Program program = loadProgramFromBytes(f.readAsBytesSync());
for (Uri uri in program.uriToSource.keys) {
if ('$uri' == '')
continue;
final List<int> oldBytes = program.uriToSource[uri].source;
final FileSystemEntity entity = _compilerOptions.fileSystem.entityForUri(uri);
if (!await entity.exists()) {
_generator.invalidate(uri);
continue;
}
final List<int> newBytes = await entity.readAsBytes();
if (oldBytes.length != newBytes.length) {
_generator.invalidate(uri);
continue;
}
for (int i = 0; i < oldBytes.length; ++i) {
if (oldBytes[i] != newBytes[i]) {
_generator.invalidate(uri);
continue;
}
}
}
} catch(e) {
// If there's a failure in the above block we might not have invalidated
// correctly. Create a new generator that doesn't bootstrap to avoid missing
// any changes.
_generator = _createGenerator(null);
}
}
@override
Future<Null> recompileDelta() async {
final String boundaryKey = new Uuid().generateV4();
_outputStream.writeln('result $boundaryKey');
await invalidateIfBootstrapping();
final Program deltaProgram = await _generator.computeDelta();
final IOSink sink = new File(_kernelBinaryFilename).openWrite();
final BinaryPrinter printer = printerFactory.newBinaryPrinter(sink);
printer.writeProgramFile(deltaProgram);
await sink.close();
_outputStream.writeln('$boundaryKey $_kernelBinaryFilename');
_kernelBinaryFilename = _kernelBinaryFilenameIncremental;
return null;
}
@@ -213,11 +260,13 @@ class _FrontendCompiler implements CompilerInterface {
@override
void resetIncrementalCompiler() {
_generator = _createGenerator();
_generator = _createGenerator(Uri.base.resolve(_kernelBinaryFilenameFull));
_kernelBinaryFilename = _kernelBinaryFilenameFull;
}
IncrementalKernelGenerator _createGenerator() =>
new IncrementalKernelGenerator(_compilerOptions, _entryPoint);
IncrementalKernelGenerator _createGenerator(Uri bootstrapDill) {
return new IncrementalKernelGenerator(_compilerOptions, _entryPoint, bootstrapDill);
}
Uri _ensureFolderPath(String path) {
String uriPath = new Uri.file(path).toString();