Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a196d68

Browse files
Clement Skaucommit-bot@chromium.org
authored andcommitted
Revert "[vm, front-end server] Cache generated bytecode using separate file"
This reverts commit 86641d0. Reason for revert: Appears to break Flutter 3H: - https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket.appspot.com/8897037674557388176/+/steps/flutter_test_framework_coverage/0/stdout - https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket.appspot.com/8897037674557388176/+/steps/flutter_test_framework_tests/0/stdout - https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket.appspot.com/8897037674557388176/+/steps/flutter_test_hostonly_devicelab_tests/0/stdout Original change's description: > [vm, front-end server] Cache generated bytecode using separate file > > Previously, generated bytecode was cached using metadata in kernel AST. > When serializaing kernel AST (for subsequent compilations) incremental > serialization was not used, so AST serialization was considerably > slower in bytecode mode compared to non-bytecode mode. > > Apparently, metadata is not compatible with incremental serialization > (dart-lang/sdk#39302). > > This change reimplements caching of generated bytecode using a separate > 'foo.dill.cache' file near the generated 'foo.dill' and 'foo.dill.ast'. > AST is now written using incremental serialization both in bytecode > and non-bytecode modes. > > flutter_test_performance benchmark: > > AST mode: > > "without_change_elapsed_time_ms": 2615-2652, >   "implementation_change_elapsed_time_ms": 6292-6394, >   "interface_change_elapsed_time_ms": 6183-6484, >   "with_coverage_time_ms": 2723-2834 > > Bytecode mode, before this change: > > "without_change_elapsed_time_ms": 3246-3295, >   "implementation_change_elapsed_time_ms": 7998-8068, >   "interface_change_elapsed_time_ms": 7899-8029, >   "with_coverage_time_ms": 3316-3378 > > Bytecode mode, after this change: > > "without_change_elapsed_time_ms": 2689-2737, >   "implementation_change_elapsed_time_ms": 7630-7677, >   "interface_change_elapsed_time_ms": 7724-7917, >   "with_coverage_time_ms": 2841-2949 > > Change-Id: I603d33f49949146cf6620226d810f2f3cb9853a9 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124592 > Reviewed-by: Ryan Macnak <rmacnak@google.com> > Commit-Queue: Alexander Markov <alexmarkov@google.com> TBR=rmacnak@google.com,alexmarkov@google.com Change-Id: Ib433b68b7de3b08179c9c080a10f62703cd2546d No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124983 Reviewed-by: Clement Skau <cskau@google.com> Commit-Queue: Clement Skau <cskau@google.com>
1 parent b2fa458 commit a196d68

File tree

4 files changed

+65
-137
lines changed

4 files changed

+65
-137
lines changed

pkg/frontend_server/lib/frontend_server.dart

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import 'package:kernel/target/targets.dart' show targets, TargetFlags;
2828
import 'package:path/path.dart' as path;
2929
import 'package:usage/uuid/uuid.dart';
3030

31-
import 'package:vm/binary_cache.dart' show BinaryCache, Range;
31+
import 'package:vm/metadata/binary_cache.dart'
32+
show BinaryCacheMetadataRepository;
33+
3234
import 'package:vm/bytecode/gen_bytecode.dart'
3335
show generateBytecode, createFreshComponentWithBytecode;
3436
import 'package:vm/bytecode/options.dart' show BytecodeOptions;
@@ -281,8 +283,6 @@ class FrontendCompiler implements CompilerInterface {
281283
String _kernelBinaryFilenameIncremental;
282284
String _kernelBinaryFilenameFull;
283285
String _initializeFromDill;
284-
String _bytecodeDillPath;
285-
String _bytecodeCachePath;
286286

287287
Set<Uri> previouslyReportedDependencies = Set<Uri>();
288288

@@ -404,14 +404,12 @@ class FrontendCompiler implements CompilerInterface {
404404
];
405405
}
406406

407-
_bytecodeCachePath = _initializeFromDill + ".cache";
408-
if (compilerOptions.bytecode) {
407+
if (compilerOptions.bytecode && _initializeFromDill != null) {
409408
// If we are generating bytecode, put bytecode only (not AST) in
410409
// [_kernelBinaryFilename], which the user of this tool will eventually
411410
// feed to Flutter engine or flutter_tester. Use a separate file to cache
412411
// the AST result to initialize the incremental compiler for the next
413412
// invocation of this tool.
414-
_bytecodeDillPath = _initializeFromDill;
415413
_initializeFromDill += ".ast";
416414
}
417415

@@ -558,38 +556,25 @@ class FrontendCompiler implements CompilerInterface {
558556
{bool filterExternal: false,
559557
IncrementalSerializer incrementalSerializer}) async {
560558
final Component component = results.component;
559+
// Remove the cache that came either from this function or from
560+
// initializing from a kernel file.
561+
component.metadata.remove(BinaryCacheMetadataRepository.repositoryTag);
561562

562563
if (_compilerOptions.bytecode) {
563-
BinaryCache inputCache;
564-
if (_options['incremental']) {
565-
inputCache = new BinaryCache();
566-
await inputCache.read(_bytecodeCachePath, _bytecodeDillPath);
567-
}
568-
BinaryCache outputCache = new BinaryCache();
569-
570564
{
571565
// Generate bytecode as the output proper.
572566
final IOSink sink = File(filename).openWrite();
573-
int offset = 0;
574567
await runWithFrontEndCompilerContext(
575568
_mainSource, _compilerOptions, component, () async {
576569
if (_options['incremental']) {
577570
// When loading a single kernel buffer with multiple sub-components,
578571
// the VM expects 'main' to be the first sub-component.
579572
await forEachPackage(component,
580573
(String package, List<Library> libraries) async {
581-
offset = _writePackage(
582-
results,
583-
package,
584-
libraries,
585-
sink,
586-
offset,
587-
inputCache,
588-
outputCache,
589-
incrementalSerializer?.invalidatedUris);
574+
_writePackage(results, package, libraries, sink);
590575
}, mainFirst: true);
591576
} else {
592-
_writePackage(results, 'main', component.libraries, sink, 0);
577+
_writePackage(results, 'main', component.libraries, sink);
593578
}
594579
});
595580
await sink.close();
@@ -600,6 +585,15 @@ class FrontendCompiler implements CompilerInterface {
600585
// of [filename] so that a later invocation of frontend_server will the
601586
// same arguments will use this to initialize its incremental kernel
602587
// compiler.
588+
final repository = BinaryCacheMetadataRepository();
589+
component.addMetadataRepository(repository);
590+
for (var lib in component.libraries) {
591+
var bytes = BinaryCacheMetadataRepository.lookup(lib);
592+
if (bytes != null) {
593+
repository.mapping[lib] = bytes;
594+
}
595+
}
596+
603597
final file = new File(_initializeFromDill);
604598
await file.create(recursive: true);
605599
final IOSink sink = file.openWrite();
@@ -613,18 +607,9 @@ class FrontendCompiler implements CompilerInterface {
613607

614608
sortComponent(component);
615609

616-
if (incrementalSerializer != null) {
617-
incrementalSerializer.writePackagesToSinkAndTrimComponent(
618-
component, sink);
619-
} else if (unsafePackageSerialization == true) {
620-
writePackagesToSinkAndTrimComponent(component, sink);
621-
}
622-
623610
printer.writeComponentFile(component);
624611
await sink.close();
625612
}
626-
627-
await outputCache.write(_bytecodeCachePath);
628613
} else {
629614
// Generate AST as the output proper.
630615
final IOSink sink = File(filename).openWrite();
@@ -647,9 +632,6 @@ class FrontendCompiler implements CompilerInterface {
647632

648633
printer.writeComponentFile(component);
649634
await sink.close();
650-
651-
// Reset cache if not generating bytecode.
652-
await new File(_bytecodeCachePath).writeAsString('');
653635
}
654636

655637
if (_options['split-output-by-packages']) {
@@ -732,33 +714,18 @@ class FrontendCompiler implements CompilerInterface {
732714
}
733715
}
734716

735-
int _writePackage(KernelCompilationResults result, String package,
736-
List<Library> libraries, IOSink sink, int offset,
737-
[BinaryCache inputCache,
738-
BinaryCache outputCache,
739-
Set<Uri> invalidatedUris]) {
717+
void _writePackage(KernelCompilationResults result, String package,
718+
List<Library> libraries, IOSink sink) {
740719
final canCache = libraries.isNotEmpty &&
741720
_compilerOptions.bytecode &&
742721
errors.isEmpty &&
743722
package != "main";
744723

745-
if (canCache && invalidatedUris != null) {
746-
final range = inputCache?.get(package);
747-
if (range != null) {
748-
bool invalidated = false;
749-
for (var lib in libraries) {
750-
if (invalidatedUris.contains(lib.fileUri)) {
751-
invalidated = true;
752-
break;
753-
}
754-
}
755-
756-
if (!invalidated) {
757-
final cachedBytes = inputCache.getBytes(range);
758-
sink.add(cachedBytes);
759-
outputCache.add(package, Range(offset, cachedBytes.length));
760-
return offset + cachedBytes.length;
761-
}
724+
if (canCache) {
725+
var cachedBytes = BinaryCacheMetadataRepository.lookup(libraries.first);
726+
if (cachedBytes != null) {
727+
sink.add(cachedBytes);
728+
return;
762729
}
763730
}
764731

@@ -783,9 +750,8 @@ class FrontendCompiler implements CompilerInterface {
783750
final bytes = byteSink.builder.takeBytes();
784751
sink.add(bytes);
785752
if (canCache) {
786-
outputCache.add(package, Range(offset, bytes.length));
753+
BinaryCacheMetadataRepository.insert(libraries.first, bytes);
787754
}
788-
return offset + bytes.length;
789755
}
790756

791757
@override

pkg/vm/lib/binary_cache.dart

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
library vm.metadata.binary_cache;
6+
7+
import 'package:kernel/ast.dart'
8+
show BinarySink, BinarySource, MetadataRepository, Node, TreeNode;
9+
10+
class BinaryCacheMetadataRepository extends MetadataRepository<List<int>> {
11+
static const repositoryTag = 'vm.binary_cache';
12+
13+
@override
14+
String get tag => repositoryTag;
15+
16+
@override
17+
final Map<TreeNode, List<int>> mapping = <TreeNode, List<int>>{};
18+
19+
@override
20+
void writeToBinary(List<int> metadata, Node node, BinarySink sink) {
21+
sink.writeByteList(metadata);
22+
}
23+
24+
@override
25+
List<int> readFromBinary(Node node, BinarySource source) {
26+
List<int> result = source.readByteList();
27+
_weakMap[node] = result;
28+
return result;
29+
}
30+
31+
static List<int> lookup(Node node) => _weakMap[node];
32+
static void insert(Node node, List<int> metadata) {
33+
_weakMap[node] = metadata;
34+
}
35+
36+
static final _weakMap = new Expando<List<int>>();
37+
}

pkg/vm/lib/target/vm.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:kernel/transformations/continuation.dart' as transformAsync
1717
import 'package:kernel/vm/constants_native_effects.dart'
1818
show VmConstantsBackend;
1919

20+
import '../metadata/binary_cache.dart' show BinaryCacheMetadataRepository;
2021
import '../transformations/call_site_annotator.dart' as callSiteAnnotator;
2122
import '../transformations/list_factory_specializer.dart'
2223
as listFactorySpecializer;
@@ -369,6 +370,7 @@ class VmTarget extends Target {
369370
@override
370371
Component configureComponent(Component component) {
371372
callSiteAnnotator.addRepositoryTo(component);
373+
component.addMetadataRepository(new BinaryCacheMetadataRepository());
372374
return super.configureComponent(component);
373375
}
374376

0 commit comments

Comments
 (0)