Skip to content

Commit

Permalink
Use setRange when copying output chunks to the final buffer in `Cod…
Browse files Browse the repository at this point in the history
…edBufferWriter` (#887)

Similar to #885, this optimizes some more buffer copying to `memcpy`.

Results from the same benchmark in #885:

|                              | Before     | After      | Diff                |
|------------------------------|------------|------------|---------------------|
| AOT                          | 114,713 us | 109,838 us | - 4,875 us, -4.2%   |
| JIT                          |  91,960 us |  92,887 us | +   927 us, +1.0%   |
| dart2js -O4                  | 259,125 us | 257,000 us | - 2,125 us, -0.8%   |
| dart2wasm --omit-type-checks | 196,909 us | 182,333 us | -14,576 us, -7.4%   |

AOT and JIT tested on x64.
  • Loading branch information
osa1 authored Oct 24, 2023
1 parent 050c162 commit 3528fad
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions protobuf/lib/src/protobuf/coded_buffer_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ 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++];
}
buffer.setRange(
outPos, outPos + bytesToCopyFromChunk, chunk, chunkPos);
chunkPos += bytesToCopyFromChunk;
outPos += bytesToCopyFromChunk;
bytesToCopy -= bytesToCopyFromChunk;

// Move to the next chunk if the current one is exhausted.
Expand Down

0 comments on commit 3528fad

Please sign in to comment.