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
Reduce allocations in the GRPCClientStateMachine #653
Conversation
Motivation: There were some unnecessary allocations in the client state machine. Modifications: - Use `HPACKHeader`s new `first(name:)` method to avoid allocating an array of headers before taking the first. - Turn LengthPrefixedMessageWriter into a struct. - Turn LengthPrefixedMessageReader into a struct. Result: - Reduce allocations, a small perf gain.
Before:
After:
|
I also had more targeted benchmark for just the state machine (which involved making a bunch of things public) which saw a decrease from ~60ms to ~50ms. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool stuff! Suggested a few more block replacements.
@@ -544,21 +544,20 @@ extension GRPCClientStateMachine.State { | |||
// responses as well as a variety of non-GRPC content-types and to omit Status & Status-Message. | |||
// Implementations must synthesize a Status & Status-Message to propagate to the application | |||
// layer when this occurs." | |||
let statusHeader = headers[":status"].first | |||
let responseStatus = statusHeader.flatMap(Int.init).map { code in | |||
let responseStatus = headers.first(name: ":status").flatMap(Int.init).map { code in | |||
HTTPResponseStatus(statusCode: code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.map(HTTPResponseStatus.init)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the init has a default second arg: init(statusCode: Int, reasonPhrase: String = "")
so this isn't possible.
We could make those public if we declare them as not part of the public API. |
True, I think I'll leave that for another PR though! |
Motivation:
There were some unnecessary allocations in the client state machine.
Modifications:
HPACKHeader
s newfirst(name:)
method to avoid allocating anarray of headers before taking the first.
Result: