Skip to content

Commit

Permalink
[dart2wasm] More JSON decoding improvements
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
osa1 authored and Commit Queue committed Jun 13, 2024
1 parent 9974252 commit 1ec4677
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sdk/lib/_internal/vm/lib/convert_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ mixin _ChunkedJsonParser<T> on _JsonParserWithListener {
* Get character/code unit of current chunk.
*
* The [index] must be non-negative and less than `chunkEnd`.
* In practive, [index] will be no smaller than the `start` argument passed
* In practice, [index] will be no smaller than the `start` argument passed
* to [parse].
*/
int getChar(int index);
Expand Down
21 changes: 11 additions & 10 deletions sdk/lib/_internal/wasm/lib/convert_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ mixin _ChunkedJsonParser<T> on _JsonParserWithListener {
* Get character/code unit of current chunk.
*
* The [index] must be non-negative and less than `chunkEnd`.
* In practive, [index] will be no smaller than the `start` argument passed
* In practice, [index] will be no smaller than the `start` argument passed
* to [parse].
*/
int getChar(int index);
Expand Down Expand Up @@ -2072,7 +2072,10 @@ class _Utf8Decoder {

String decode16(Uint8List bytes, int start, int end, int size) {
assert(start < end);
final String transitionTable = _Utf8Decoder.transitionTable;
final OneByteString transitionTable =
unsafeCast<OneByteString>(_Utf8Decoder.transitionTable);
final OneByteString typeTable =
unsafeCast<OneByteString>(_Utf8Decoder.typeTable);
TwoByteString result = TwoByteString.withLength(size);
int i = start;
int j = 0;
Expand All @@ -2083,21 +2086,19 @@ class _Utf8Decoder {
assert(!isErrorState(state));
final int byte = bytes[i++];
final int type =
_Utf8Decoder.typeTable.oneByteStringCodeUnitAtUnchecked(byte) &
typeMask;
oneByteStringCodeUnitAtUnchecked(typeTable, byte) & typeMask;
if (state == accept) {
char = byte & (shiftedByteMask >> type);
state = transitionTable.codeUnitAt(type);
state = oneByteStringCodeUnitAtUnchecked(transitionTable, type);
} else {
char = (byte & 0x3F) | (_charOrIndex << 6);
state = transitionTable.codeUnitAt(state + type);
state = oneByteStringCodeUnitAtUnchecked(transitionTable, state + type);
}

while (i < end) {
final int byte = bytes[i++];
final int type =
_Utf8Decoder.typeTable.oneByteStringCodeUnitAtUnchecked(byte) &
typeMask;
oneByteStringCodeUnitAtUnchecked(typeTable, byte) & typeMask;
if (state == accept) {
if (char >= 0x10000) {
assert(char < 0x110000);
Expand All @@ -2107,14 +2108,14 @@ class _Utf8Decoder {
writeIntoTwoByteString(result, j++, char);
}
char = byte & (shiftedByteMask >> type);
state = transitionTable.codeUnitAt(type);
state = oneByteStringCodeUnitAtUnchecked(transitionTable, type);
} else if (isErrorState(state)) {
_state = state;
_charOrIndex = i - 2;
return "";
} else {
char = (byte & 0x3F) | (char << 6);
state = transitionTable.codeUnitAt(state + type);
state = oneByteStringCodeUnitAtUnchecked(transitionTable, state + type);
}
}

Expand Down

0 comments on commit 1ec4677

Please sign in to comment.