Skip to content

C++ Add move constructor for Reflection's SetString#6477

Merged
acozzette merged 4 commits intoprotocolbuffers:masterfrom
reed-lau:master
Aug 9, 2019
Merged

C++ Add move constructor for Reflection's SetString#6477
acozzette merged 4 commits intoprotocolbuffers:masterfrom
reed-lau:master

Conversation

@reed-lau
Copy link
Contributor

@reed-lau reed-lau commented Aug 5, 2019

Add move constructor for Reflection's SetString
add feature #6399

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@reed-lau
Copy link
Contributor Author

reed-lau commented Aug 5, 2019

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes and removed cla: no labels Aug 5, 2019
@reed-lau reed-lau changed the title Add move constructor for Reflection's SetString C++ Add move constructor for Reflection's SetString Aug 5, 2019
Copy link

@t-rybarski t-rybarski left a comment

Choose a reason for hiding this comment

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

Rvalue reference cannot be const



void Reflection::SetString(Message* message, const FieldDescriptor* field,
const std::string&& value) const {

Choose a reason for hiding this comment

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

@reed-lau rvalue references cannot be const as they cannot be moved. The overloaded functions rvalue parameter should be SetString(..,..,std::string&& value) without the const!

USAGE_CHECK_ALL(SetString, SINGULAR, STRING);
if (field->is_extension()) {
return MutableExtensionSet(message)->SetString(field->number(),
field->type(), value, field);

Choose a reason for hiding this comment

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

Also here i believe the value parameter (i.e. the input string) should be moved, so:
SetString(...,..., std::move(value), field)
Additionaly, the string which you are creating in your code should also be moved into place:

bool foo(const char *buf, size_t size,...) { ... std::string temp(buf, size); // actually, we don't need to create a temporary std::string here, if we have // some method like SetStringByArray(..., buf, size); reflection->SetString(...., std::move(temp)); ... }

@reed-lau
Copy link
Contributor Author

reed-lau commented Aug 5, 2019

@t-rybarski have fixed it according to your comment.

@reed-lau
Copy link
Contributor Author

reed-lau commented Aug 6, 2019

@acozzette @protobuf-kokoro
what should I do to fix the Kokoro build failure for Linux Golang?
it seems that the docker pulling failed due to the networking or environment.

@acozzette
Copy link

That Go error looks unrelated, so I wouldn't worry about it for now.

void SetBool(int number, FieldType type, bool value, desc);
void SetEnum(int number, FieldType type, int value, desc);
void SetString(int number, FieldType type, const std::string& value, desc);
void SetString(int number, FieldType type, std::string&& value, desc);

Choose a reason for hiding this comment

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

I believe it's possible to enable this optimization without creating new overloads. Could you try taking the existing methods and changing them to take a std::string (by value) instead of const std::string&? I think that should be backward-compatible with existing callers and still allow us to optimize the implementation.

@reed-lau
Copy link
Contributor Author

reed-lau commented Aug 7, 2019

after change the interface to std::string(by value), the following line may using the std::move form, and it could benefit from it, but I didn't change these things at this PR except the python's wrapper SetString.

SetString(message2, field1, temp_string);

@acozzette
Copy link

This looks great but we are having some issues with Docker in our CI setup. Hopefully we will get the test issues sorted out soon and then I plan to merge this.

@acozzette acozzette merged commit c132a4a into protocolbuffers:master Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants