From 4c9b71205262699764c5db74e95bc9e702451f4e Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Wed, 2 May 2018 22:16:29 +0000 Subject: [PATCH] Revert "Don't encode strings up front; don't toString uris" This reverts commit 47e90395128ed2744213786ba9fa26281cc92d64 as it breaks flutter build aot. Bug: https://github.com/flutter/flutter/issues/17214 Change-Id: I5e727db976822a6a001e5fa7a0bb0ffc7460fea8 Reviewed-on: https://dart-review.googlesource.com/53500 Reviewed-by: Alexander Markov --- .../test/summary_generator_test.dart | 4 +- pkg/kernel/lib/binary/ast_from_binary.dart | 4 +- pkg/kernel/lib/binary/ast_to_binary.dart | 72 ++++++++----------- 3 files changed, 32 insertions(+), 48 deletions(-) diff --git a/pkg/front_end/test/summary_generator_test.dart b/pkg/front_end/test/summary_generator_test.dart index 435e5ecfd40..05d582ce053 100644 --- a/pkg/front_end/test/summary_generator_test.dart +++ b/pkg/front_end/test/summary_generator_test.dart @@ -14,9 +14,9 @@ main() { var summary = await summarize(['a.dart'], allSources); var component = loadComponentFromBytes(summary); - // Note: the kernel representation always has a null key in the map, + // Note: the kernel representation always has an empty '' key in the map, // but otherwise no other data is included here. - expect(component.uriToSource.keys.single, null); + expect(component.uriToSource.keys.single, Uri.parse("")); }); test('summary includes declarations, but no method bodies', () async { diff --git a/pkg/kernel/lib/binary/ast_from_binary.dart b/pkg/kernel/lib/binary/ast_from_binary.dart index 54d677aa849..5b0eda784ec 100644 --- a/pkg/kernel/lib/binary/ast_from_binary.dart +++ b/pkg/kernel/lib/binary/ast_from_binary.dart @@ -507,9 +507,7 @@ class BinaryBuilder { Map uriToSource = {}; for (int i = 0; i < length; ++i) { List uriBytes = readByteList(); - Uri uri = uriBytes.isEmpty - ? null - : Uri.parse(const Utf8Decoder().convert(uriBytes)); + Uri uri = Uri.parse(const Utf8Decoder().convert(uriBytes)); _sourceUriTable[i] = uri; List sourceCode = readByteList(); int lineCount = readUInt(); diff --git a/pkg/kernel/lib/binary/ast_to_binary.dart b/pkg/kernel/lib/binary/ast_to_binary.dart index 21fe4419081..48a431a1e8c 100644 --- a/pkg/kernel/lib/binary/ast_to_binary.dart +++ b/pkg/kernel/lib/binary/ast_to_binary.dart @@ -22,7 +22,7 @@ class BinaryPrinter implements Visitor, BinarySink { final TypeParameterIndexer _typeParameterIndexer = new TypeParameterIndexer(); final StringIndexer stringIndexer; ConstantIndexer _constantIndexer; - final UriIndexer _sourceUriIndexer = new UriIndexer(); + final StringIndexer _sourceUriIndexer = new StringIndexer(); final Set _knownSourceUri = new Set(); Map _libraryDependencyIndex = {}; @@ -103,22 +103,15 @@ class BinaryPrinter implements Visitor, BinarySink { _binaryOffsetForStringTable = getBufferOffset(); // Write the end offsets. - writeUInt30(indexer.index.length); + writeUInt30(indexer.numberOfStrings); int endOffset = 0; - List> data = new List.filled(indexer.index.length, null); - int i = 0; - Utf8Encoder utf8Encoder = const Utf8Encoder(); - for (String key in indexer.index.keys) { - List utf8Bytes = utf8Encoder.convert(key); - data[i] = utf8Bytes; - endOffset += utf8Bytes.length; + for (var entry in indexer.entries) { + endOffset += entry.utf8Bytes.length; writeUInt30(endOffset); - i++; } - // Write the UTF-8 encoded strings. - for (var entry in data) { - writeBytes(entry); + for (var entry in indexer.entries) { + writeBytes(entry.utf8Bytes); } } @@ -213,11 +206,11 @@ class BinaryPrinter implements Visitor, BinarySink { // Returns the new active file uri. Uri writeUriReference(Uri uri) { if (_knownSourceUri.contains(uri)) { - final int index = _sourceUriIndexer.put(uri); + final int index = _sourceUriIndexer.put(uri == null ? "" : "$uri"); writeUInt30(index); return uri; } else { - final int index = 0; // equivalent to index = _sourceUriIndexer[null]; + final int index = 0; // equivalent to index = _sourceUriIndexer[""]; writeUInt30(index); return null; } @@ -494,19 +487,19 @@ class BinaryPrinter implements Visitor, BinarySink { void writeUriToSource(Map uriToSource) { _binaryOffsetForSourceTable = getBufferOffset(); - int length = _sourceUriIndexer.index.length; + int length = _sourceUriIndexer.numberOfStrings; writeUInt32(length); - List index = new List(length); + List index = new List(_sourceUriIndexer.entries.length); // Write data. - int i = 0; - Utf8Encoder utf8Encoder = const Utf8Encoder(); - for (Uri uri in _sourceUriIndexer.index.keys) { + for (int i = 0; i < length; ++i) { index[i] = getBufferOffset(); - Source source = uriToSource[uri] ?? new Source([], const []); + StringTableEntry uri = _sourceUriIndexer.entries[i]; + Source source = uriToSource[Uri.parse(uri.value)] ?? + new Source([], const []); - writeByteList(utf8Encoder.convert(uri == null ? "" : "$uri")); + writeByteList(uri.utf8Bytes); writeByteList(source.source); List lineStarts = source.lineStarts; writeUInt30(lineStarts.length); @@ -515,7 +508,6 @@ class BinaryPrinter implements Visitor, BinarySink { writeUInt30(lineStart - previousLineStart); previousLineStart = lineStart; }); - i++; } // Write index for random access. @@ -2110,17 +2102,29 @@ class TypeParameterIndexer { int operator [](TypeParameter parameter) => index[parameter]; } +class StringTableEntry { + final String value; + final List utf8Bytes; + + StringTableEntry(String value) + : value = value, + utf8Bytes = const Utf8Encoder().convert(value); +} + class StringIndexer { - // Note the LinkedHashMap: The iteration order is important. - final Map index = new LinkedHashMap(); + final List entries = []; + final LinkedHashMap index = new LinkedHashMap(); StringIndexer() { put(''); } + int get numberOfStrings => index.length; + int put(String string) { var result = index[string]; if (result == null) { + entries.add(new StringTableEntry(string)); result = index.length; index[string] = result; } @@ -2130,24 +2134,6 @@ class StringIndexer { int operator [](String string) => index[string]; } -class UriIndexer { - // Note the LinkedHashMap: The iteration order is important. - final Map index = new LinkedHashMap(); - - UriIndexer() { - put(null); - } - - int put(Uri uri) { - var result = index[uri]; - if (result == null) { - result = index.length; - index[uri] = result; - } - return result; - } -} - /// Computes and stores the index of a library, class, or member within its /// parent list. class GlobalIndexer extends TreeVisitor {