Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Unified Diff: pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart

Issue 2896493002: Add LimitedBinaryPrinter, tests and switch incremental generator to it. (Closed)
Patch Set: Make 'stringIndexer' public and make it named constructor parameter. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart
diff --git a/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart b/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart
new file mode 100644
index 0000000000000000000000000000000000000000..aaf5be00e483df6c63c285223c4d650ec664f4b6
--- /dev/null
+++ b/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart
@@ -0,0 +1,75 @@
+// Copyright (c) 2017, 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.
+
+import 'package:kernel/ast.dart';
+import 'package:kernel/binary/ast_to_binary.dart';
+
+/// Writes libraries that satisfy the [predicate].
+///
+/// Only the referenced subset of canonical names is indexed and written,
+/// so we don't waste time indexing all libraries of a program, when only
+/// a tiny subset is used.
+class LimitedBinaryPrinter extends BinaryPrinter {
+ final LibraryFilter predicate;
+
+ LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate)
+ : super(sink, stringIndexer: new ReferencesStringIndexer());
+
+ @override
+ void addCanonicalNamesForLinkTable(List<CanonicalName> list) {
+ var stringIndexer = this.stringIndexer as ReferencesStringIndexer;
Siggi Cherem (dart-lang) 2017/05/23 20:54:51 nit: I've seen this style in fasta so far to use a
scheglov 2017/05/23 21:54:43 Done.
+ stringIndexer.referencedNames.forEach((name) {
+ if (name.index != -1) return;
+ name.index = list.length;
+ list.add(name);
+ });
+ }
+
+ @override
+ void buildStringIndex(Program program) {
+ program.libraries.where(predicate).forEach((library) {
+ stringIndexer.scanLibrary(library);
+ });
+ stringIndexer.finish();
+ }
+
+ @override
+ bool shouldWriteLibraryCanonicalNames(Library library) {
+ return predicate(library);
+ }
+
+ @override
+ void writeLibraries(Program program) {
+ var librariesToWrite = program.libraries.where(predicate).toList();
+ writeList(librariesToWrite, writeNode);
+ }
+
+ @override
+ void writeNode(Node node) {
+ if (node is Library && !predicate(node)) return;
+ node.accept(this);
+ }
+}
+
+/// Extension of [StringIndexer] that also indexes canonical names of
+/// referenced classes and members.
+class ReferencesStringIndexer extends StringIndexer {
+ final List<CanonicalName> referencedNames = <CanonicalName>[];
+
+ defaultMemberReference(Member node) {
+ _handleReferencedName(node.canonicalName);
+ }
+
+ visitClassReference(Class node) {
+ _handleReferencedName(node.canonicalName);
+ }
+
+ void _handleReferencedName(CanonicalName name) {
+ if (name == null || name.parent == null) return;
+ _handleReferencedName(name.parent);
+ referencedNames.add(name);
+ name.index = -1;
+ put(name.name);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698