Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 38f360f

Browse files
authored
Increased WriteBuffers starting capacity to 64 bytes. (#101790)
1 parent 88724e5 commit 38f360f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

packages/flutter/lib/src/foundation/serialization.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import 'dart:typed_data';
1313
/// The byte order used is [Endian.host] throughout.
1414
class WriteBuffer {
1515
/// Creates an interface for incrementally building a [ByteData] instance.
16-
factory WriteBuffer() {
16+
/// [startCapacity] determines the start size of the [WriteBuffer] in bytes.
17+
/// The closer that value is to the real size used, the better the
18+
/// performance.
19+
factory WriteBuffer({int startCapacity = 8}) {
20+
assert(startCapacity > 0);
1721
final ByteData eightBytes = ByteData(8);
1822
final Uint8List eightBytesAsList = eightBytes.buffer.asUint8List();
19-
return WriteBuffer._(Uint8List(8), eightBytes, eightBytesAsList);
23+
return WriteBuffer._(Uint8List(startCapacity), eightBytes, eightBytesAsList);
2024
}
2125

2226
WriteBuffer._(this._buffer, this._eightBytes, this._eightBytesAsList);

packages/flutter/lib/src/services/message_codecs.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class StandardMessageCodec implements MessageCodec<Object?> {
310310
ByteData? encodeMessage(Object? message) {
311311
if (message == null)
312312
return null;
313-
final WriteBuffer buffer = WriteBuffer();
313+
final WriteBuffer buffer = WriteBuffer(startCapacity: 64);
314314
writeValue(buffer, message);
315315
return buffer.done();
316316
}

packages/flutter/test/foundation/serialization_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,11 @@ void main() {
124124
write.done();
125125
expect(() => write.done(), throwsStateError);
126126
});
127+
test('empty WriteBuffer', () {
128+
expect(() => WriteBuffer(startCapacity: 0), throwsAssertionError);
129+
});
130+
test('size 1', () {
131+
expect(() => WriteBuffer(startCapacity: 1), returnsNormally);
132+
});
127133
});
128134
}

0 commit comments

Comments
 (0)