Skip to content

Commit

Permalink
Make CodedBufferWriter.writeRawByte argument type more accurate (#891)
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
osa1 authored Oct 26, 2023
1 parent 2ce3e14 commit 19903f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
5 changes: 4 additions & 1 deletion protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/protobuf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 11 additions & 26 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
///
Expand Down

0 comments on commit 19903f0

Please sign in to comment.