Skip to content

add APIs for creating errors from C++ strings#27310

Merged
markdroth merged 2 commits intogrpc:masterfrom
markdroth:error_std_string
Sep 10, 2021
Merged

add APIs for creating errors from C++ strings#27310
markdroth merged 2 commits intogrpc:masterfrom
markdroth:error_std_string

Conversation

@markdroth
Copy link
Copy Markdown
Member

In the vast majority of cases where we call GRPC_ERROR_CREATE_FROM_COPIED_STRING(), we are constructing a C++ string, and then calling its c_str() method to create the error. This API is not very ergonomic, because it requires calling c_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 uses grpc_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 use GRPC_ERROR_CREATE_FROM_VECTOR() but the string is a dynamically generated std::string instead of a static string.

@markdroth markdroth added the release notes: no Indicates if PR should not be in release notes label Sep 10, 2021
@markdroth markdroth requested a review from veblush September 10, 2021 18:40
Copy link
Copy Markdown
Contributor

@veblush veblush left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for a nice refactoring!

@drfloob
Copy link
Copy Markdown
Member

drfloob commented Sep 10, 2021

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.

[microbenchmarks] Performance differences noted:
Benchmark              cpu_time    real_time
---------------------  ----------  -----------
BM_ErrorGetMissingInt  -16%        -16%
BM_NoOpExecCtx         +4%         +4%

@markdroth
Copy link
Copy Markdown
Member Author

This PR should not have any effect on that particular microbenchmark, so I assume that result is just noise.

@markdroth markdroth merged commit 3763be8 into grpc:master Sep 10, 2021
@markdroth markdroth deleted the error_std_string branch September 10, 2021 23:15
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lang/c++ lang/core release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants