Skip to content

optimize header name handling in Grpc::map_response#1359

Merged
tottoto merged 3 commits intohyperium:masterfrom
srijs:optimize-response-into-http-header-handling
Sep 20, 2024
Merged

optimize header name handling in Grpc::map_response#1359
tottoto merged 3 commits intohyperium:masterfrom
srijs:optimize-response-into-http-header-handling

Conversation

@srijs
Copy link
Copy Markdown
Contributor

@srijs srijs commented Apr 16, 2023

Avoid parsing reserved header names on every call to MetadataMap::into_sanitized_headers as well as in Status::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 through Grpc::map_response and Response::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 function HeaderName::from_static. This eliminates any runtime overhead of parsing the reserved header names.

@srijs srijs changed the title optimize MetadataMap::into_sanitized_headers optimize header name handling in Response::into_http Apr 16, 2023
@srijs srijs changed the title optimize header name handling in Response::into_http optimize header name handling in Grpc::map_response Apr 16, 2023
@srijs srijs force-pushed the optimize-response-into-http-header-handling branch from 7bf69be to 2c83f31 Compare April 16, 2023 13:31
@srijs
Copy link
Copy Markdown
Contributor Author

srijs commented May 1, 2023

@LucioFranco this should be a relatively simple chance, any indication as to whether you'd be able to merge it or have any feedback would be appreciated, thanks!

@tottoto tottoto added this pull request to the merge queue Sep 20, 2024
Merged via the queue into hyperium:master with commit a09d453 Sep 20, 2024
@tottoto
Copy link
Copy Markdown
Collaborator

tottoto commented Sep 20, 2024

Thank you!

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

Successfully merging this pull request may close these issues.

2 participants