[dart2wasm] Improve container type in dart:convert

Use `GrowableList` instead of `List` for the container type in
`_JsonListener`.

Eliminates virtual calls in `_populateUnsafe`.

Improves bytes/second in an internal JSON decoding benchmark from
206,257,553 to 222,982,950 (8%).

Change-Id: I02e467e5353cc348493b52be1fb9ef2e6599381b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380702
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
This commit is contained in:
Ömer Sinan Ağacan
2024-08-15 13:03:19 +00:00
committed by Commit Queue
parent 05ee4e463f
commit a09fbf8468
2 changed files with 12 additions and 9 deletions
+4 -2
View File
@@ -4,6 +4,7 @@
import "dart:_internal"
show IterableElementError, ClassID, TypeTest, unsafeCast;
import "dart:_list" show GrowableList;
import "dart:_wasm";
import "dart:collection";
@@ -443,7 +444,7 @@ mixin _LinkedHashMapMixin<K, V> on _HashBase, _EqualsAndHashCode {
/// This function is unsafe: it does not perform any type checking on
/// keys and values assuming that caller has ensured that types are
/// correct.
void _populateUnsafe(List<Object?> keyValuePairs) {
void _populateUnsafe(GrowableList<Object?> keyValuePairs) {
assert(keyValuePairs.length.isEven);
int size = _roundUpToPowerOfTwo(keyValuePairs.length);
if (size < _HashBase._INITIAL_INDEX_SIZE) {
@@ -1130,5 +1131,6 @@ base class CompactLinkedCustomHashSet<E> extends _HashFieldBase
}
@pragma('wasm:prefer-inline')
Map<K, V> createMapFromKeyValueListUnsafe<K, V>(List<Object?> keyValuePairs) =>
Map<K, V> createMapFromKeyValueListUnsafe<K, V>(
GrowableList<Object?> keyValuePairs) =>
DefaultMap<K, V>().._populateUnsafe(keyValuePairs);
@@ -6,10 +6,11 @@ import "dart:_compact_hash" show createMapFromKeyValueListUnsafe;
import "dart:_internal" show patch, POWERS_OF_TEN, unsafeCast;
import "dart:_js_string_convert";
import "dart:_js_types";
import "dart:_list" show GrowableList;
import "dart:_string";
import "dart:_typed_data";
import "dart:_wasm";
import "dart:typed_data" show Uint8List, Uint16List;
import "dart:typed_data" show Uint8List;
/// This patch library has no additional parts.
@@ -87,7 +88,7 @@ class _JsonListener {
*
* When building [Map] this will contain array of key-value pairs.
*/
List<dynamic>? currentContainer;
GrowableList<dynamic>? currentContainer;
/** The most recently read value. */
Object? value;
@@ -95,13 +96,13 @@ class _JsonListener {
/** Pushes the currently active container. */
void beginContainer() {
stack.add(currentContainer);
currentContainer = [];
currentContainer = GrowableList.empty();
}
/** Pops the top container from the [stack]. */
void popContainer() {
value = currentContainer;
currentContainer = unsafeCast<List?>(stack.removeLast());
currentContainer = unsafeCast<GrowableList?>(stack.removeLast());
}
void handleString(String value) {
@@ -125,12 +126,12 @@ class _JsonListener {
}
void propertyName() {
unsafeCast<List>(currentContainer).add(value);
unsafeCast<GrowableList>(currentContainer).add(value);
value = null;
}
void propertyValue() {
final keyValuePairs = unsafeCast<List>(currentContainer);
final keyValuePairs = unsafeCast<GrowableList>(currentContainer);
if (reviver case final reviver?) {
final key = keyValuePairs.last;
keyValuePairs.add(reviver(key, value));
@@ -143,7 +144,7 @@ class _JsonListener {
void endObject() {
popContainer();
value = createMapFromKeyValueListUnsafe<String, dynamic>(
unsafeCast<List>(value));
unsafeCast<GrowableList>(value));
}
void beginArray() {