Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use setRange when copying output chunks to the final buffer in CodedBufferWriter #887

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,32 @@ class CodedBufferWriter {
}
buffer[outPos++] = v;
} else {
// action is an amount of bytes to copy from _outputChunks into the
// buffer.
// `action` is an amount of bytes to copy from `_outputChunks` into
// the buffer.
var bytesToCopy = action;
while (bytesToCopy > 0) {
final Uint8List chunk = _outputChunks[chunkIndex];
final int bytesInChunk = _outputChunks[chunkIndex + 1];

// Copy at most bytesToCopy bytes from the current chunk.
// Copy at most `bytesToCopy` bytes from the current chunk.
final leftInChunk = bytesInChunk - chunkPos;
final bytesToCopyFromChunk =
leftInChunk > bytesToCopy ? bytesToCopy : leftInChunk;
final endPos = chunkPos + bytesToCopyFromChunk;
while (chunkPos < endPos) {
buffer[outPos++] = chunk[chunkPos++];

if (bytesToCopyFromChunk <= 20) {
osa1 marked this conversation as resolved.
Show resolved Hide resolved
while (chunkPos < endPos) {
buffer[outPos++] = chunk[chunkPos++];
}
bytesToCopy -= bytesToCopyFromChunk;
} else {
final chunkSlice = Uint8List.sublistView(chunk, chunkPos, endPos);
buffer.setRange(
outPos, outPos + bytesToCopyFromChunk, chunkSlice);
chunkPos += bytesToCopyFromChunk;
outPos += bytesToCopyFromChunk;
bytesToCopy -= bytesToCopyFromChunk;
}
bytesToCopy -= bytesToCopyFromChunk;

// Move to the next chunk if the current one is exhausted.
if (chunkPos == bytesInChunk) {
Expand Down
Loading