From 19903f0f753eed3ff3be774d35bb97759deeb851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 26 Oct 2023 10:02:24 +0200 Subject: [PATCH] Make `CodedBufferWriter.writeRawByte` argument type more accurate (#891) `writeRawBytes` adds bytes to the output and it will only be fast when the argument is `Uint8List`. Since all the users already pass `Uint8List`, update the argument type. This allows simplifyping `_copyInto`. Because it has only one call site, we simplify it and inline it in the call site. cl/576427817 --- protobuf/CHANGELOG.md | 5 ++- protobuf/lib/protobuf.dart | 2 +- .../lib/src/protobuf/coded_buffer_writer.dart | 37 ++++++------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/protobuf/CHANGELOG.md b/protobuf/CHANGELOG.md index 7fa100fca..a69ed025d 100644 --- a/protobuf/CHANGELOG.md +++ b/protobuf/CHANGELOG.md @@ -1,6 +1,6 @@ ## 4.0.0-dev -* The following types and members are now removed: +* **Breaking:** The following types and members are now removed: - `PbEventMixin` - `PbFieldChange` @@ -12,6 +12,9 @@ surface small (to make it easier to change the library or migrate to another library) these types and members are removed. ([#738]) +* **Breaking:** `CodedBufferWriter.writeRawBytes` now takes a `Uint8List` + argument (instead of `TypedData`). + [#738]: https://github.com/google/protobuf.dart/issues/738 ## 3.1.0 diff --git a/protobuf/lib/protobuf.dart b/protobuf/lib/protobuf.dart index 200424484..e4481ae5e 100644 --- a/protobuf/lib/protobuf.dart +++ b/protobuf/lib/protobuf.dart @@ -17,7 +17,7 @@ import 'dart:convert' jsonDecode, jsonEncode; import 'dart:math' as math; -import 'dart:typed_data' show ByteData, Endian, TypedData, Uint8List; +import 'dart:typed_data' show ByteData, Endian, Uint8List; import 'package:fixnum/fixnum.dart' show Int64; import 'package:meta/meta.dart' show UseResult; diff --git a/protobuf/lib/src/protobuf/coded_buffer_writer.dart b/protobuf/lib/src/protobuf/coded_buffer_writer.dart index 4285a7c2d..db97f05a4 100644 --- a/protobuf/lib/src/protobuf/coded_buffer_writer.dart +++ b/protobuf/lib/src/protobuf/coded_buffer_writer.dart @@ -22,8 +22,8 @@ part of '../../protobuf.dart'; class CodedBufferWriter { /// Array of splices representing the data written into the writer. /// Each element might be one of: - /// * a TypedData object - represents a sequence of bytes that need to be - /// emitted into the result as-is; + /// * a [Uint8List] object - represents a sequence of bytes that need to be + /// emitted into the result as-is; /// * a positive integer - a number of bytes to copy from [_outputChunks] /// into resulting buffer; /// * a non-positive integer - a positive number that needs to be emitted @@ -166,9 +166,12 @@ class CodedBufferWriter { } } } else { - // action is a TypedData containing bytes to emit into the output + // action is a `Uint8List` containing bytes to emit into the output // buffer. - outPos = _copyInto(buffer, outPos, action); + final Uint8List value = action; + final end = outPos + value.length; + buffer.setRange(outPos, end, value); + outPos = end; } } @@ -210,7 +213,7 @@ class CodedBufferWriter { /// Record number of bytes written into output chunks since last splice. /// /// This is used before reserving space for an unknown varint splice or - /// adding a TypedData array splice. + /// adding a [Uint8List] array splice. void _commitSplice() { final pos = _bytesInChunk + _outputChunksBytes; final bytes = pos - _lastSplicePos; @@ -220,9 +223,9 @@ class CodedBufferWriter { _lastSplicePos = pos; } - /// Add TypedData splice - these bytes would be directly copied into the - /// output buffer by [writeTo]. - void writeRawBytes(TypedData value) { + /// Add a [Uint8List] splice, without copying. These bytes will be directly + /// copied into the output buffer by [writeTo]. + void writeRawBytes(Uint8List value) { _commitSplice(); _splices.add(value); _bytesTotal += value.lengthInBytes; @@ -419,24 +422,6 @@ class CodedBufferWriter { _writeVarint32(value & 0xFFFFFFFF); } - /// Copy bytes from the given typed data array into the output buffer. - /// - /// Has a specialization for [Uint8List] for performance. - int _copyInto(Uint8List buffer, int pos, TypedData value) { - if (value is Uint8List) { - final len = value.length; - final end = pos + len; - buffer.setRange(pos, end, value); - return end; - } else { - final len = value.lengthInBytes; - final end = pos + len; - final u8 = Uint8List.view(value.buffer, value.offsetInBytes, len); - buffer.setRange(pos, end, u8); - return end; - } - } - /// This function maps a power-of-2 value (2^0 .. 2^31) to a unique value /// in the 0..31 range. ///