Skip to content

Commit 9820c8d

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Avoid array allocation when ending json object
When the json parser sees the end of a json object (i.e. the '}') it currently creates a growable array in `popContainer()` but at the call site it doesn't need the growable array, so it just gets the wasm array and the length out. We can avoid this array allocation by just directly accessing the current container wasm array & length. Change-Id: Ib66a3ddf98934c3c083347ff41c93ed4de2eeaf5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/409960 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Ömer Ağacan <[email protected]>
1 parent 75cda04 commit 9820c8d

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

sdk/lib/_internal/wasm/lib/convert_patch.dart

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,6 @@ class _JsonListener {
161161

162162
/** Pops the top container from the [stack]. */
163163
void popContainer() {
164-
final currentContainerLocal = currentContainer;
165-
if (currentContainerLocal == null) {
166-
value = null;
167-
} else {
168-
value = GrowableList.withDataAndLength(
169-
currentContainerLocal,
170-
currentContainerLength,
171-
);
172-
}
173-
174164
final GrowableList<dynamic>? currentContainerList = stackPop();
175165
if (currentContainerList == null) {
176166
currentContainer = null;
@@ -220,12 +210,11 @@ class _JsonListener {
220210
}
221211

222212
void endObject() {
223-
popContainer();
224-
final list = unsafeCast<GrowableList>(value);
225213
value = createMapFromKeyValueListUnsafe<String, dynamic>(
226-
list.data,
227-
list.length,
214+
unsafeCast<WasmArray<Object?>>(currentContainer),
215+
currentContainerLength,
228216
);
217+
popContainer();
229218
}
230219

231220
void beginArray() {
@@ -242,6 +231,10 @@ class _JsonListener {
242231
}
243232

244233
void endArray() {
234+
value = GrowableList.withDataAndLength(
235+
unsafeCast<WasmArray<Object?>>(currentContainer),
236+
currentContainerLength,
237+
);
245238
popContainer();
246239
}
247240

0 commit comments

Comments
 (0)