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

[c#] Moved all message allocations to CreateTemplate(), making it easier to e.g. use pools to prevent GC load. #7722

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions csharp/src/Google.Protobuf/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal MessageParser(Func<IMessage> factory, bool discardUnknownFields, Extens
/// Creates a template instance ready for population.
/// </summary>
/// <returns>An empty message.</returns>
internal IMessage CreateTemplate()
public IMessage CreateTemplate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
return factory();
}
Expand Down Expand Up @@ -247,7 +247,7 @@ internal MessageParser(Func<T> factory, bool discardUnknownFields, ExtensionRegi
/// Creates a template instance ready for population.
/// </summary>
/// <returns>An empty message.</returns>
internal new T CreateTemplate()
public new T CreateTemplate()
{
return factory();
}
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/csharp/csharp_message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
variables_,
"if (other.$has_property_check$) {\n"
" if ($has_not_property_check$) {\n"
" $property_name$ = new $type_name$();\n"
" $property_name$ = $type_name$.Parser.CreateTemplate();\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after changing the codegen, you'd also need to regenerate the code in this repository.

Copy link
Author

@ManuelKugelmann ManuelKugelmann Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= do a build ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build and then run https://github.com/protocolbuffers/protobuf/blob/master/csharp/generate_protos.sh.

The updates to the generated files need to be commited (preferrably as a separate commit)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the overhead of using Parser.CreateTemplate?

" }\n"
" $property_name$.MergeFrom(other.$property_name$);\n"
"}\n");
Expand All @@ -114,7 +114,7 @@ void MessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
printer->Print(
variables_,
"if ($has_not_property_check$) {\n"
" $property_name$ = new $type_name$();\n"
" $property_name$ = $type_name$.Parser.CreateTemplate();\n"
"}\n");
if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
printer->Print(variables_, "input.ReadMessage($property_name$);\n");
Expand Down Expand Up @@ -255,7 +255,7 @@ void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
void MessageOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) {
printer->Print(variables_,
"if ($property_name$ == null) {\n"
" $property_name$ = new $type_name$();\n"
" $property_name$ = $type_name$.Parser.CreateTemplate();\n"
"}\n"
"$property_name$.MergeFrom(other.$property_name$);\n");
}
Expand All @@ -264,7 +264,7 @@ void MessageOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
// TODO(jonskeet): We may be able to do better than this
printer->Print(
variables_,
"$type_name$ subBuilder = new $type_name$();\n"
"$type_name$ subBuilder = $type_name$.Parser.CreateTemplate();\n"
"if ($has_property_check$) {\n"
" subBuilder.MergeFrom($property_name$);\n"
"}\n");
Expand Down