Skip to content

Commit

Permalink
Avoid adding empty splices in CodedBufferWriter
Browse files Browse the repository at this point in the history
By avoiding allocating empty `Uint8List`s this saves a few percent in
dart2js -O4 in the benchmark reported in google#885.

|                              | Before     | After      | Diff                |
|------------------------------|------------|------------|---------------------|
| AOT                          | 122,917 us | 122,741 us |                     |
| JIT                          |  93,376 us |  94,880 us |                     |
| dart2js -O4                  | 271,111 us | 258,250 us | -12,861 us, -4.7%   |
| dart2wasm --omit-type-checks | 196,454 us | 195,300 us |                     |

Number of splices in the benchmark before this change: 21,123
After: 20,857
  • Loading branch information
osa1 committed Oct 24, 2023
1 parent 050c162 commit 0822f4c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,22 @@ class CodedBufferWriter {
_writeVarint32(value ? 1 : 0);
break;
case PbFieldType._BYTES_BIT:
_writeBytesNoTag(
value is Uint8List ? value : Uint8List.fromList(value));
final List<int> bytes = value;
if (bytes is Uint8List) {
_writeBytesNoTag(bytes);
} else if (bytes.isEmpty) {
writeInt32NoTag(0);
} else {
_writeBytesNoTag(Uint8List.fromList(value));
}
break;
case PbFieldType._STRING_BIT:
_writeBytesNoTag(_utf8.encoder.convert(value));
final String string = value;
if (string.isEmpty) {
writeInt32NoTag(0);
} else {
_writeBytesNoTag(_utf8.encoder.convert(string));
}
break;
case PbFieldType._DOUBLE_BIT:
_writeDouble(value);
Expand Down

0 comments on commit 0822f4c

Please sign in to comment.