Skip to content

Commit 5ff00b2

Browse files
authored
[pigeon] Remove heap allocation in generated C++ code (#6196)
Please let me know if I goofed anything, this is my first time contributing to Pigeon! Addresses flutter/flutter#144042
1 parent 679bdd7 commit 5ff00b2

File tree

9 files changed

+529
-548
lines changed

9 files changed

+529
-548
lines changed

packages/pigeon/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 17.1.1
2+
3+
* Removes heap allocation in generated C++ code.
4+
15
## 17.1.0
26

37
* [kotlin] Adds `includeErrorClass` to `KotlinOptions`.

packages/pigeon/example/app/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ application, rather than in a plugin.
55

66
To update the generated code, run:
77
```sh
8-
dart run pigeon --input pigeons/messages.dart
8+
cd ../..
9+
dart tool/generate.dart
910
```

packages/pigeon/example/app/windows/runner/messages.g.cpp

+14-16
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ const flutter::StandardMessageCodec& ExampleHostApi::GetCodec() {
141141
void ExampleHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
142142
ExampleHostApi* api) {
143143
{
144-
auto channel = std::make_unique<BasicMessageChannel<>>(
145-
binary_messenger,
146-
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi."
147-
"getHostLanguage",
148-
&GetCodec());
144+
BasicMessageChannel<> channel(binary_messenger,
145+
"dev.flutter.pigeon.pigeon_example_package."
146+
"ExampleHostApi.getHostLanguage",
147+
&GetCodec());
149148
if (api != nullptr) {
150-
channel->SetMessageHandler(
149+
channel.SetMessageHandler(
151150
[api](const EncodableValue& message,
152151
const flutter::MessageReply<EncodableValue>& reply) {
153152
try {
@@ -164,16 +163,16 @@ void ExampleHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
164163
}
165164
});
166165
} else {
167-
channel->SetMessageHandler(nullptr);
166+
channel.SetMessageHandler(nullptr);
168167
}
169168
}
170169
{
171-
auto channel = std::make_unique<BasicMessageChannel<>>(
170+
BasicMessageChannel<> channel(
172171
binary_messenger,
173172
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add",
174173
&GetCodec());
175174
if (api != nullptr) {
176-
channel->SetMessageHandler(
175+
channel.SetMessageHandler(
177176
[api](const EncodableValue& message,
178177
const flutter::MessageReply<EncodableValue>& reply) {
179178
try {
@@ -203,16 +202,16 @@ void ExampleHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
203202
}
204203
});
205204
} else {
206-
channel->SetMessageHandler(nullptr);
205+
channel.SetMessageHandler(nullptr);
207206
}
208207
}
209208
{
210-
auto channel = std::make_unique<BasicMessageChannel<>>(
209+
BasicMessageChannel<> channel(
211210
binary_messenger,
212211
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage",
213212
&GetCodec());
214213
if (api != nullptr) {
215-
channel->SetMessageHandler(
214+
channel.SetMessageHandler(
216215
[api](const EncodableValue& message,
217216
const flutter::MessageReply<EncodableValue>& reply) {
218217
try {
@@ -239,7 +238,7 @@ void ExampleHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
239238
}
240239
});
241240
} else {
242-
channel->SetMessageHandler(nullptr);
241+
channel.SetMessageHandler(nullptr);
243242
}
244243
}
245244
}
@@ -273,12 +272,11 @@ void MessageFlutterApi::FlutterMethod(
273272
const std::string channel_name =
274273
"dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi."
275274
"flutterMethod";
276-
auto channel = std::make_unique<BasicMessageChannel<>>(
277-
binary_messenger_, channel_name, &GetCodec());
275+
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
278276
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
279277
a_string_arg ? EncodableValue(*a_string_arg) : EncodableValue(),
280278
});
281-
channel->Send(
279+
channel.Send(
282280
encoded_api_arguments, [channel_name, on_success = std::move(on_success),
283281
on_error = std::move(on_error)](
284282
const uint8_t* reply, size_t reply_size) {

packages/pigeon/lib/cpp_generator.dart

+5-8
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,9 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
871871
scope: api.name,
872872
returnType: _voidType,
873873
parameters: parameters, body: () {
874-
const String channel = 'channel';
875874
indent.writeln(
876875
'const std::string channel_name = "${makeChannelName(api, func, dartPackageName)}";');
877-
indent.writeln(
878-
'auto channel = std::make_unique<BasicMessageChannel<>>(binary_messenger_, '
876+
indent.writeln('BasicMessageChannel<> channel(binary_messenger_, '
879877
'channel_name, &GetCodec());');
880878

881879
// Convert arguments to EncodableValue versions.
@@ -894,7 +892,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
894892
});
895893
}
896894

897-
indent.write('$channel->Send($argumentListVariableName, '
895+
indent.write('channel.Send($argumentListVariableName, '
898896
// ignore: missing_whitespace_between_adjacent_strings
899897
'[channel_name, on_success = std::move(on_success), on_error = std::move(on_error)]'
900898
'(const uint8_t* reply, size_t reply_size) ');
@@ -972,12 +970,11 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
972970
final String channelName =
973971
makeChannelName(api, method, dartPackageName);
974972
indent.writeScoped('{', '}', () {
975-
indent.writeln(
976-
'auto channel = std::make_unique<BasicMessageChannel<>>(binary_messenger, '
973+
indent.writeln('BasicMessageChannel<> channel(binary_messenger, '
977974
'"$channelName", &GetCodec());');
978975
indent.writeScoped('if (api != nullptr) {', '} else {', () {
979976
indent.write(
980-
'channel->SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) ');
977+
'channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) ');
981978
indent.addScoped('{', '});', () {
982979
indent.writeScoped('try {', '}', () {
983980
final List<String> methodArgument = <String>[];
@@ -1054,7 +1051,7 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
10541051
});
10551052
});
10561053
indent.addScoped(null, '}', () {
1057-
indent.writeln('channel->SetMessageHandler(nullptr);');
1054+
indent.writeln('channel.SetMessageHandler(nullptr);');
10581055
});
10591056
});
10601057
}

packages/pigeon/lib/generator_tools.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '17.1.0';
16+
const String pigeonVersion = '17.1.1';
1717

1818
/// Read all the content from [stdin] to a String.
1919
String readStdin() {

packages/pigeon/platform_tests/test_plugin/example/windows/flutter/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ include(${EPHEMERAL_DIR}/generated_config.cmake)
1010
# https://github.com/flutter/flutter/issues/57146.
1111
set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper")
1212

13+
# Set fallback configurations for older versions of the flutter tool.
14+
if (NOT DEFINED FLUTTER_TARGET_PLATFORM)
15+
set(FLUTTER_TARGET_PLATFORM "windows-x64")
16+
endif()
17+
1318
# === Flutter Library ===
1419
set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll")
1520

@@ -92,7 +97,7 @@ add_custom_command(
9297
COMMAND ${CMAKE_COMMAND} -E env
9398
${FLUTTER_TOOL_ENVIRONMENT}
9499
"${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat"
95-
windows-x64 $<CONFIG>
100+
${FLUTTER_TARGET_PLATFORM} $<CONFIG>
96101
VERBATIM
97102
)
98103
add_custom_target(flutter_assemble DEPENDS

0 commit comments

Comments
 (0)