add APIs for creating errors from C++ strings#27310
Merged
markdroth merged 2 commits intogrpc:masterfrom Sep 10, 2021
Merged
Conversation
veblush
approved these changes
Sep 10, 2021
Contributor
veblush
left a comment
There was a problem hiding this comment.
LGTM. Thank you for a nice refactoring!
Member
|
There's a 16% time decrease in one microbenchmark test. I'm curious if that result can be reproduced ... it seems odd that this would only affect one benchmark test, and so significantly. |
Member
Author
|
This PR should not have any effect on that particular microbenchmark, so I assume that result is just noise. |
lidizheng
pushed a commit
to lidizheng/grpc
that referenced
this pull request
Sep 23, 2021
* add API for creating errors from C++ strings * add missing include
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the vast majority of cases where we call
GRPC_ERROR_CREATE_FROM_COPIED_STRING(), we are constructing a C++ string, and then calling itsc_str()method to create the error. This API is not very ergonomic, because it requires callingc_str()all over the place. It may also be slightly less efficient, since it requires reallocating the string and copying it.To address this, I have added a
GRPC_ERROR_CREATE_FROM_CPP_STRING()method, which takes ownership of the C++ string that you pass to it. Under the hood, this usesgrpc_slice_from_cpp_string(), which I added back in #23157. (Note that this still makes a second allocation for the slice ref-count, so it winds up holding two different memory allocations for the lifetime of the slice, but there is no time during which the string is double-allocated, and it does not need to make a copy.)I have also added a
GRPC_ERROR_CREATE_FROM_VECTOR_AND_CPP_STRING()function to address the less pervasive but still common use-case where we want to useGRPC_ERROR_CREATE_FROM_VECTOR()but the string is a dynamically generatedstd::stringinstead of a static string.