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

add SetStringByArray(..., const char *, size_t) method in Reflection #6399

Closed
reed-lau opened this issue Jul 18, 2019 · 7 comments
Closed

add SetStringByArray(..., const char *, size_t) method in Reflection #6399

reed-lau opened this issue Jul 18, 2019 · 7 comments
Assignees

Comments

@reed-lau
Copy link
Contributor

reed-lau commented Jul 18, 2019

What language does this apply to?
cxx and other binding languages such as python's cpp_implenentation

Describe the problem you are trying to solve.
the Reflection class in message.h only has SetString(..., const std::string &value)interface. in some cases, we just have raw buffer (char * and size), to call the SetString method, we have to construct/memcpy/deconstruct a std::string. But in some time-sensitive case, this consumes much, especially when the the bytes is large enough (eg. 8MB), which will cause large amount of page-faults.

Describe the solution you'd like
Add SetStringByArray(..., const char *, size_t ) interface, and call it when necessorily.

Describe alternatives you've considered
even in protobuf, construct 8MB bytes(std::string) will cause large amount of page-fault, this will slow down much. is it possible to setup an allocator for the protobuf. I did not seen such method.

Additional context
Add any other context or screenshots about the feature request here.

@acozzette
Copy link
Member

Unfortunately I don't think there's any easy way to do this. Internally messages store string and bytes fields as std::string, so ultimately we have to construct a real std::string. However, we could potentially add an overload that takes a std::string&& and then we could at least move the string into place without creating another copy.

@reed-lau
Copy link
Contributor Author

reed-lau commented Jul 19, 2019

@acozzette
I'm sorry, I did not describe it clearly.
I uses the contruct/memcpy/deconstruct did not mean the protobuf's internal data storage but the extra std::string, which must be create to adapt to the interface.
like the following code, could we remove the temp std::string's construct/memcpy/deconstruct

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(...., temp);
  ...
}

the original case comes from the following code, and I measured it in a 8MB bytes case. the value_string's construct and memcpy take about 3ms( actually due to the page fault).

string value_string(value, value_len);

@acozzette
Copy link
Member

If the string is passed by value or by rvalue reference then we can eliminate one of the two copies. The user can create the string and then it can be moved directly into place.

@t-rybarski
Copy link

Adding an overloaded SetString(..., std::string&& value) which utilizes move semantics will avoid using the copy constructor and the std::string object will be moved, not copied which is less costly and will improve performance significantly

@reed-lau
Copy link
Contributor Author

copy that. when will this feature be added to the master?

@acozzette
Copy link
Member

I'm afraid I don't have time to work on it, but if you want to try implementing it yourself then feel free to send me a pull request.

@reed-lau
Copy link
Contributor Author

solution:

  1. change the Reflection's prototype from SetString(..., const std::string&) to SetString(..., std::string).
  2. if the caller do not use the created std::string s any more , using SetString(..., std::move(s))
  3. if the caller would like to keep the created std::string s, using SetString(..., s)
  4. in the SetString, becuase we get the moved or copied string, we just move it forward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants