mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
made ascii string encoding faster (#101777)
This commit is contained in:
parent
30a501801a
commit
fd73f2730c
@ -92,5 +92,20 @@ void main() {
|
|||||||
|
|
||||||
watch.reset();
|
watch.reset();
|
||||||
|
|
||||||
|
watch.start();
|
||||||
|
for (int i = 0; i < _kNumIterations; i += 1) {
|
||||||
|
codec.encodeMessage('special chars >\u263A\u{1F602}<');
|
||||||
|
}
|
||||||
|
watch.stop();
|
||||||
|
|
||||||
|
printer.addResult(
|
||||||
|
description: 'StandardMessageCodec unicode',
|
||||||
|
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
|
||||||
|
unit: 'us per iteration',
|
||||||
|
name: 'StandardMessageCodec_unicode',
|
||||||
|
);
|
||||||
|
|
||||||
|
watch.reset();
|
||||||
|
|
||||||
printer.printToStdout();
|
printer.printToStdout();
|
||||||
}
|
}
|
||||||
|
@ -386,9 +386,28 @@ class StandardMessageCodec implements MessageCodec<Object?> {
|
|||||||
}
|
}
|
||||||
} else if (value is String) {
|
} else if (value is String) {
|
||||||
buffer.putUint8(_valueString);
|
buffer.putUint8(_valueString);
|
||||||
final Uint8List bytes = utf8.encoder.convert(value);
|
final Uint8List asciiBytes = Uint8List(value.length);
|
||||||
writeSize(buffer, bytes.length);
|
Uint8List? utf8Bytes;
|
||||||
buffer.putUint8List(bytes);
|
int utf8Offset = 0;
|
||||||
|
// Only do utf8 encoding if we encounter non-ascii characters.
|
||||||
|
for (int i = 0; i < value.length; i += 1) {
|
||||||
|
final int char = value.codeUnitAt(i);
|
||||||
|
if (char <= 0x7f) {
|
||||||
|
asciiBytes[i] = char;
|
||||||
|
} else {
|
||||||
|
utf8Bytes = utf8.encoder.convert(value.substring(i));
|
||||||
|
utf8Offset = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (utf8Bytes != null) {
|
||||||
|
writeSize(buffer, utf8Offset + utf8Bytes.length);
|
||||||
|
buffer.putUint8List(Uint8List.sublistView(asciiBytes, 0, utf8Offset));
|
||||||
|
buffer.putUint8List(utf8Bytes);
|
||||||
|
} else {
|
||||||
|
writeSize(buffer, asciiBytes.length);
|
||||||
|
buffer.putUint8List(asciiBytes);
|
||||||
|
}
|
||||||
} else if (value is Uint8List) {
|
} else if (value is Uint8List) {
|
||||||
buffer.putUint8(_valueUint8List);
|
buffer.putUint8(_valueUint8List);
|
||||||
writeSize(buffer, value.length);
|
writeSize(buffer, value.length);
|
||||||
|
Loading…
Reference in New Issue
Block a user