Skip to content

Commit

Permalink
Switch to accept/reject-friendly incremental compiler (flutter#4601)
Browse files Browse the repository at this point in the history
* Switch to accept/reject-friendly incremental compiler

* Merge
  • Loading branch information
aam authored Feb 13, 2018
1 parent 981ebb5 commit 5529f89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
34 changes: 11 additions & 23 deletions frontend_server/lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import 'package:args/args.dart';
// 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';
import 'package:vm/incremental_compiler.dart' show IncrementalCompiler;
import 'package:vm/kernel_front_end.dart' show compileToKernel;

ArgParser _argParser = new ArgParser(allowTrailingOptions: true)
Expand Down Expand Up @@ -64,7 +64,6 @@ Instructions:
...
<boundary-key>
- accept
- reject
- quit
Output:
Expand All @@ -87,7 +86,7 @@ abstract class CompilerInterface {
Future<Null> compile(
String filename,
ArgResults options, {
IncrementalKernelGenerator generator,
IncrementalCompiler generator,
});

/// Assuming some Dart program was previously compiled, recompile it again
Expand All @@ -98,10 +97,6 @@ abstract class CompilerInterface {
/// won't recompile sources that were previously reported as changed.
void acceptLastDelta();

/// Reject results of previous compilation. Next recompilation cycle will
/// recompile sources indicated as changed.
void rejectLastDelta();

/// This let's compiler know that source file identifed by `uri` was changed.
void invalidate(Uri uri);

Expand Down Expand Up @@ -131,7 +126,7 @@ class _FrontendCompiler implements CompilerInterface {
CompilerOptions _compilerOptions;
Uri _entryPoint;

IncrementalKernelGenerator _generator;
IncrementalCompiler _generator;
String _kernelBinaryFilename;
String _kernelBinaryFilenameIncremental;
String _kernelBinaryFilenameFull;
Expand All @@ -140,7 +135,7 @@ class _FrontendCompiler implements CompilerInterface {
Future<Null> compile(
String filename,
ArgResults options, {
IncrementalKernelGenerator generator,
IncrementalCompiler generator,
}) async {
final Uri filenameUri = Uri.base.resolveUri(new Uri.file(filename));
_kernelBinaryFilenameFull = '$filename.dill';
Expand All @@ -162,7 +157,7 @@ class _FrontendCompiler implements CompilerInterface {
_compilerOptions = compilerOptions;
_generator = generator ?? _createGenerator(new Uri.file(_kernelBinaryFilenameFull));
await invalidateIfBootstrapping();
program = await _runWithPrintRedirection(() => _generator.computeDelta());
program = await _runWithPrintRedirection(() => _generator.compile());
} else {
if (options['link-platform']) {
// TODO(aam): Remove linkedDependencies once platform is directly embedded
Expand Down Expand Up @@ -233,7 +228,7 @@ class _FrontendCompiler implements CompilerInterface {
final String boundaryKey = new Uuid().generateV4();
_outputStream.writeln('result $boundaryKey');
await invalidateIfBootstrapping();
final Program deltaProgram = await _generator.computeDelta();
final Program deltaProgram = await _generator.compile();
final IOSink sink = new File(_kernelBinaryFilename).openWrite();
final BinaryPrinter printer = printerFactory.newBinaryPrinter(sink);
printer.writeProgramFile(deltaProgram);
Expand All @@ -245,12 +240,7 @@ class _FrontendCompiler implements CompilerInterface {

@override
void acceptLastDelta() {
// TODO(aam): implement this considering new incremental compiler API.
}

@override
void rejectLastDelta() {
// TODO(aam): implement this considering new incremental compiler API.
_generator.accept();
}

@override
Expand All @@ -264,8 +254,9 @@ class _FrontendCompiler implements CompilerInterface {
_kernelBinaryFilename = _kernelBinaryFilenameFull;
}

IncrementalKernelGenerator _createGenerator(Uri bootstrapDill) {
return new IncrementalKernelGenerator(_compilerOptions, _entryPoint, bootstrapDill);
IncrementalCompiler _createGenerator(Uri bootstrapDill) {
return new IncrementalCompiler(_compilerOptions, _entryPoint,
bootstrapDill: bootstrapDill);
}

Uri _ensureFolderPath(String path) {
Expand Down Expand Up @@ -295,7 +286,7 @@ Future<int> starter(
CompilerInterface compiler,
Stream<List<int>> input,
StringSink output,
IncrementalKernelGenerator generator,
IncrementalCompiler generator,
BinaryPrinterFactory binaryPrinterFactory,
}) async {
ArgResults options;
Expand Down Expand Up @@ -358,9 +349,6 @@ Future<int> starter(
state = _State.RECOMPILE_LIST;
} else if (string == 'accept') {
compiler.acceptLastDelta();
} else if (string == 'reject') {
// TODO(aam) implement reject so it won't reset compiler.
compiler.resetIncrementalCompiler();
} else if (string == 'reset') {
compiler.resetIncrementalCompiler();
} else if (string == 'quit') {
Expand Down
28 changes: 6 additions & 22 deletions frontend_server/test/server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import 'package:frontend_server/server.dart';
// 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/incremental_kernel_generator.dart';
import 'package:kernel/binary/ast_to_binary.dart';
import 'package:kernel/ast.dart' show Program;
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'package:vm/incremental_compiler.dart';

class _MockedCompiler extends Mock implements CompilerInterface {}

class _MockedIncrementalKernelGenerator extends Mock
implements IncrementalKernelGenerator {}
class _MockedIncrementalCompiler extends Mock
implements IncrementalCompiler {}

class _MockedBinaryPrinterFactory extends Mock implements BinaryPrinterFactory {}

Expand Down Expand Up @@ -265,22 +265,6 @@ Future<int> main() async {
inputStreamController.close();
});

test('reject', () async {
final StreamController<List<int>> inputStreamController =
new StreamController<List<int>>();
final ReceivePort resetCalled = new ReceivePort();
when(compiler.resetIncrementalCompiler()).thenAnswer((Invocation invocation) {
resetCalled.sendPort.send(true);
});
final int exitcode = await starter(args, compiler: compiler,
input: inputStreamController.stream,
);
expect(exitcode, equals(0));
inputStreamController.add('reject\n'.codeUnits);
await resetCalled.first;
inputStreamController.close();
});

test('reset', () async {
final StreamController<List<int>> inputStreamController =
new StreamController<List<int>>();
Expand Down Expand Up @@ -358,9 +342,9 @@ Future<int> main() async {
}
});

final _MockedIncrementalKernelGenerator generator =
new _MockedIncrementalKernelGenerator();
when(generator.computeDelta())
final _MockedIncrementalCompiler generator =
new _MockedIncrementalCompiler();
when(generator.compile())
.thenAnswer((_) => new Future<Program>.value(new Program()));
final _MockedBinaryPrinterFactory printerFactory =
new _MockedBinaryPrinterFactory();
Expand Down

0 comments on commit 5529f89

Please sign in to comment.