optimize header name handling in Grpc::map_response
#1359
Merged
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.
Avoid parsing reserved header names on every call to
MetadataMap::into_sanitized_headers
as well as inStatus::to_http
. More explanation below. The change should be relatively straight forward and low-complexity, but let me know if you'd like to see an isolated benchmark for this regardless!Motivation
The
MetadataMap::into_sanitized_headers
function is called for every response transitively throughGrpc::map_response
andResponse::into_http
.The way that it currently sanitizes headers by removing the reserved GRPC headers involves calling
HeaderMap::remove
with a&str
key, which results in the string key being parsed into a header name at runtime for every one of the reserved headers (see the call here).This makes it show up on my profiles in the single-digit percentage range, which seemed worthwhile improving to me, especially considering the low complexity of the change.
A similar situation exists in
Status::to_http
where the GRPC status header names are being parsed at runtime when inserted into the header map.Solution
The solution implemented is to parse the reserved headers at compile time using the
const
functionHeaderName::from_static
. This eliminates any runtime overhead of parsing the reserved header names.