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

Commit 1ac3682

Browse files
authored
Increased StandardMethodCodec's WriteBuffer start capacity (#101860)
* Added standard method codec benchmark * increased writebuffer start size * added const
1 parent fce3cee commit 1ac3682

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
7+
import '../common.dart';
8+
9+
const int _kNumIterations = 100000;
10+
11+
void main() {
12+
assert(false,
13+
"Don't run benchmarks in checked mode! Use 'flutter run --release'.");
14+
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
15+
16+
const StandardMethodCodec codec = StandardMethodCodec();
17+
final Stopwatch watch = Stopwatch();
18+
const String methodName = 'something';
19+
watch.start();
20+
for (int i = 0; i < _kNumIterations; i += 1) {
21+
codec.encodeMethodCall(const MethodCall(methodName));
22+
}
23+
watch.stop();
24+
25+
printer.addResult(
26+
description: 'StandardMethodCodec null',
27+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
28+
unit: 'us per iteration',
29+
name: 'StandardMethodCodec_null',
30+
);
31+
32+
watch.reset();
33+
watch.start();
34+
for (int i = 0; i < _kNumIterations; i += 1) {
35+
codec.encodeMethodCall(const MethodCall(methodName, 12345));
36+
}
37+
watch.stop();
38+
39+
printer.addResult(
40+
description: 'StandardMethodCodec int',
41+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
42+
unit: 'us per iteration',
43+
name: 'StandardMethodCodec_int',
44+
);
45+
46+
watch.reset();
47+
48+
watch.start();
49+
for (int i = 0; i < _kNumIterations; i += 1) {
50+
codec.encodeMethodCall(
51+
const MethodCall(methodName, 'This is a performance test.'));
52+
}
53+
watch.stop();
54+
55+
printer.addResult(
56+
description: 'StandardMethodCodec string',
57+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
58+
unit: 'us per iteration',
59+
name: 'StandardMethodCodec_string',
60+
);
61+
62+
watch.reset();
63+
watch.start();
64+
for (int i = 0; i < _kNumIterations; i += 1) {
65+
codec.encodeMethodCall(const MethodCall(
66+
methodName, <Object>[1234, 'This is a performance test.', 1.25, true]));
67+
}
68+
watch.stop();
69+
70+
printer.addResult(
71+
description: 'StandardMethodCodec heterogenous list',
72+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
73+
unit: 'us per iteration',
74+
name: 'StandardMethodCodec_heterogenous_list',
75+
);
76+
77+
watch.reset();
78+
watch.start();
79+
for (int i = 0; i < _kNumIterations; i += 1) {
80+
codec.encodeMethodCall(const MethodCall(methodName, <String, Object>{
81+
'integer': 1234,
82+
'string': 'This is a performance test.',
83+
'float': 1.25,
84+
'boolean': true
85+
}));
86+
}
87+
watch.stop();
88+
89+
printer.addResult(
90+
description: 'StandardMethodCodec heterogenous map',
91+
value: watch.elapsedMicroseconds.toDouble() / _kNumIterations,
92+
unit: 'us per iteration',
93+
name: 'StandardMethodCodec_heterogenous_map',
94+
);
95+
96+
watch.reset();
97+
98+
printer.printToStdout();
99+
}

dev/devicelab/lib/tasks/microbenchmarks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ TaskFunction createMicrobenchmarkTask() {
6161
...await _runMicrobench('lib/language/sync_star_semantics_bench.dart'),
6262
...await _runMicrobench('lib/foundation/all_elements_bench.dart'),
6363
...await _runMicrobench('lib/foundation/change_notifier_bench.dart'),
64+
...await _runMicrobench('lib/foundation/standard_method_codec_bench.dart'),
6465
...await _runMicrobench('lib/foundation/standard_message_codec_bench.dart'),
6566
...await _runMicrobench('lib/foundation/timeline_bench.dart'),
6667
};

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;
99

1010
import 'message_codec.dart';
1111

12+
const int _writeBufferStartCapacity = 64;
13+
1214
/// [MessageCodec] with unencoded binary messages represented using [ByteData].
1315
///
1416
/// On Android, messages will be represented using `java.nio.ByteBuffer`.
@@ -310,7 +312,7 @@ class StandardMessageCodec implements MessageCodec<Object?> {
310312
ByteData? encodeMessage(Object? message) {
311313
if (message == null)
312314
return null;
313-
final WriteBuffer buffer = WriteBuffer(startCapacity: 64);
315+
final WriteBuffer buffer = WriteBuffer(startCapacity: _writeBufferStartCapacity);
314316
writeValue(buffer, message);
315317
return buffer.done();
316318
}
@@ -575,7 +577,7 @@ class StandardMethodCodec implements MethodCodec {
575577

576578
@override
577579
ByteData encodeMethodCall(MethodCall methodCall) {
578-
final WriteBuffer buffer = WriteBuffer();
580+
final WriteBuffer buffer = WriteBuffer(startCapacity: _writeBufferStartCapacity);
579581
messageCodec.writeValue(buffer, methodCall.method);
580582
messageCodec.writeValue(buffer, methodCall.arguments);
581583
return buffer.done();
@@ -594,15 +596,15 @@ class StandardMethodCodec implements MethodCodec {
594596

595597
@override
596598
ByteData encodeSuccessEnvelope(Object? result) {
597-
final WriteBuffer buffer = WriteBuffer();
599+
final WriteBuffer buffer = WriteBuffer(startCapacity: _writeBufferStartCapacity);
598600
buffer.putUint8(0);
599601
messageCodec.writeValue(buffer, result);
600602
return buffer.done();
601603
}
602604

603605
@override
604606
ByteData encodeErrorEnvelope({ required String code, String? message, Object? details}) {
605-
final WriteBuffer buffer = WriteBuffer();
607+
final WriteBuffer buffer = WriteBuffer(startCapacity: _writeBufferStartCapacity);
606608
buffer.putUint8(1);
607609
messageCodec.writeValue(buffer, code);
608610
messageCodec.writeValue(buffer, message);

0 commit comments

Comments
 (0)