Files
sdk/pkg/kernel/lib/kernel.dart
T
Jens Johansen e491bc5d86 Allow to specify what dill file to initialize from
This CL updates the frontend_server to allow specifying what dill
file to initialize from.

Normally, it tries to initialize from the same dill file as is the
output. This is fine in many cases (e.g. flutter run always says it
should output to build/app.dill): The first time it compiles
everything, subsequent times (across restarts) it only recompiles
what's needed.

When running tests, however, the output is in a temporary directory,
so it doesn't work across restarts. The startup time for tests is
always rather high, because it always has to recompile everything.

This CL updates the frontend_server to allow specifying what dill
file to initialize from. Flutter can thus save the compiled test
file in a centralized location (e.g. build/test.dill) and load from
that across restarts.

Plumbing this into flutter yields the following result, when running
a single test (the one automatically created when creating a new
flutter project):

Before: ~5.5 seconds
Now, first run: ~5.5 second (unchanged)
Now, subsequent runs: ~3.4 seconds

Approximate breakdown:

Startup cost (e.g. if there's no test directory):        ~ 800 ms
Starting up the actual frontend server: ~150 ms
Reading source from old dill, loading all relevant files
and invalidating source:                                 ~ 300 ms
Compiling everything (or, rather, nothing as it's all
from the dill at this point):                            ~ 650 ms
Serializing:                                             ~ 250 ms
Copying files:                                           ~  50 ms
Running the actual test:                                 ~1100 ms

Total:                                                   ~3300 ms

`time flutter test --local-engine=host_debug --preview-dart-2`
says ~3.4 seconds...

To compare, --no-preview-dart-2 takes ~2.2 seconds,
so dart2 still has a (significantly) higher startup cost.

Moves the needle on https://github.com/flutter/flutter/issues/15767.

Change-Id: I36a2d05bc76e0193d369df42eb3b9f08416dd78f
Reviewed-on: https://dart-review.googlesource.com/51820
Reviewed-by: Kevin Millikin <kmillikin@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2018-05-17 06:49:41 +00:00

91 lines
2.6 KiB
Dart

// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Conventions for paths:
///
/// - Use the [Uri] class for paths that may have the `file`, `dart` or
/// `package` scheme. Never use [Uri] for relative paths.
/// - Use [String]s for all filenames and paths that have no scheme prefix.
/// - Never translate a `dart:` or `package:` URI into a `file:` URI, instead
/// translate it to a [String] if the file system path is needed.
/// - Only use [File] from dart:io at the last moment when it is needed.
///
library kernel;
import 'ast.dart';
import 'binary/ast_to_binary.dart';
import 'binary/ast_from_binary.dart';
import 'dart:async';
import 'dart:io';
import 'text/ast_to_text.dart';
export 'ast.dart';
Component loadComponentFromBinary(String path, [Component component]) {
List<int> bytes = new File(path).readAsBytesSync();
return loadComponentFromBytes(bytes, component);
}
Component loadComponentFromBytes(List<int> bytes, [Component component]) {
component ??= new Component();
new BinaryBuilder(bytes).readComponent(component);
return component;
}
Component loadComponentSourceFromBytes(List<int> bytes, [Component component]) {
component ??= new Component();
new BinaryBuilder(bytes).readComponentSource(component);
return component;
}
Future writeComponentToBinary(Component component, String path) {
var sink;
if (path == 'null' || path == 'stdout') {
sink = stdout.nonBlocking;
} else {
sink = new File(path).openWrite();
}
var future;
try {
new BinaryPrinter(sink).writeComponentFile(component);
} finally {
if (sink == stdout.nonBlocking) {
future = sink.flush();
} else {
future = sink.close();
}
}
return future;
}
void writeLibraryToText(Library library, {String path}) {
StringBuffer buffer = new StringBuffer();
new Printer(buffer).writeLibraryFile(library);
if (path == null) {
print(buffer);
} else {
new File(path).writeAsStringSync('$buffer');
}
}
void writeComponentToText(Component component,
{String path,
bool showExternal: false,
bool showOffsets: false,
bool showMetadata: false}) {
StringBuffer buffer = new StringBuffer();
new Printer(buffer,
showExternal: showExternal,
showOffsets: showOffsets,
showMetadata: showMetadata)
.writeComponentFile(component);
if (path == null) {
print(buffer);
} else {
new File(path).writeAsStringSync('$buffer');
}
}