Skip to content

Commit 1ec4677

Browse files
osa1Commit Queue
authored and
Commit Queue
committed
[dart2wasm] More JSON decoding improvements
- Use unchecked reads when reading from `U8List` chunks. - Use unchecked reads when reading from "transition table" in UTF16 decoder. - Fix a typo in a comment. Tested: performance refactoring, covered by existing tests. Change-Id: I1b90781f2b419188d6a560994159b48faad16075 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371301 Commit-Queue: Ömer Ağacan <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 9974252 commit 1ec4677

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

sdk/lib/_internal/vm/lib/convert_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ mixin _ChunkedJsonParser<T> on _JsonParserWithListener {
531531
* Get character/code unit of current chunk.
532532
*
533533
* The [index] must be non-negative and less than `chunkEnd`.
534-
* In practive, [index] will be no smaller than the `start` argument passed
534+
* In practice, [index] will be no smaller than the `start` argument passed
535535
* to [parse].
536536
*/
537537
int getChar(int index);

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ mixin _ChunkedJsonParser<T> on _JsonParserWithListener {
519519
* Get character/code unit of current chunk.
520520
*
521521
* The [index] must be non-negative and less than `chunkEnd`.
522-
* In practive, [index] will be no smaller than the `start` argument passed
522+
* In practice, [index] will be no smaller than the `start` argument passed
523523
* to [parse].
524524
*/
525525
int getChar(int index);
@@ -2072,7 +2072,10 @@ class _Utf8Decoder {
20722072

20732073
String decode16(Uint8List bytes, int start, int end, int size) {
20742074
assert(start < end);
2075-
final String transitionTable = _Utf8Decoder.transitionTable;
2075+
final OneByteString transitionTable =
2076+
unsafeCast<OneByteString>(_Utf8Decoder.transitionTable);
2077+
final OneByteString typeTable =
2078+
unsafeCast<OneByteString>(_Utf8Decoder.typeTable);
20762079
TwoByteString result = TwoByteString.withLength(size);
20772080
int i = start;
20782081
int j = 0;
@@ -2083,21 +2086,19 @@ class _Utf8Decoder {
20832086
assert(!isErrorState(state));
20842087
final int byte = bytes[i++];
20852088
final int type =
2086-
_Utf8Decoder.typeTable.oneByteStringCodeUnitAtUnchecked(byte) &
2087-
typeMask;
2089+
oneByteStringCodeUnitAtUnchecked(typeTable, byte) & typeMask;
20882090
if (state == accept) {
20892091
char = byte & (shiftedByteMask >> type);
2090-
state = transitionTable.codeUnitAt(type);
2092+
state = oneByteStringCodeUnitAtUnchecked(transitionTable, type);
20912093
} else {
20922094
char = (byte & 0x3F) | (_charOrIndex << 6);
2093-
state = transitionTable.codeUnitAt(state + type);
2095+
state = oneByteStringCodeUnitAtUnchecked(transitionTable, state + type);
20942096
}
20952097

20962098
while (i < end) {
20972099
final int byte = bytes[i++];
20982100
final int type =
2099-
_Utf8Decoder.typeTable.oneByteStringCodeUnitAtUnchecked(byte) &
2100-
typeMask;
2101+
oneByteStringCodeUnitAtUnchecked(typeTable, byte) & typeMask;
21012102
if (state == accept) {
21022103
if (char >= 0x10000) {
21032104
assert(char < 0x110000);
@@ -2107,14 +2108,14 @@ class _Utf8Decoder {
21072108
writeIntoTwoByteString(result, j++, char);
21082109
}
21092110
char = byte & (shiftedByteMask >> type);
2110-
state = transitionTable.codeUnitAt(type);
2111+
state = oneByteStringCodeUnitAtUnchecked(transitionTable, type);
21112112
} else if (isErrorState(state)) {
21122113
_state = state;
21132114
_charOrIndex = i - 2;
21142115
return "";
21152116
} else {
21162117
char = (byte & 0x3F) | (char << 6);
2117-
state = transitionTable.codeUnitAt(state + type);
2118+
state = oneByteStringCodeUnitAtUnchecked(transitionTable, state + type);
21182119
}
21192120
}
21202121

0 commit comments

Comments
 (0)