-
Notifications
You must be signed in to change notification settings - Fork 419
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
Improve performance of routing inbound RPCs #924
Conversation
There's some noise in the way, but actually the win looks more like 5%. The results below are on a single thread, with the only difference being the application of this patch. The headline figure is an improvement of 5.45% on RPS. Original:
With this patch:
|
There are two commits here: the first is the functional change, the second is the result of re-running codegen. |
Motivation: Right now the server burns quite a lot of CPU time initially working out which RPC handlers to use when it wants to dispatch an inbound RPC. One chunk of time here involves processing the inbound request URI to work out the appropriate method name. The costs here come in a few places, but the biggest thing blocking a performance improvement is `String.components(separatedBy:)`. This function is secretly an NSString function. The result is that the mere act of calling this function forces us to transform the UTF8 Swift String into a UTF16 NSString, split on the slash, and then allocate an NSArray into which we will re-encode each of the components as Swift Strings (transforming back from UTF16 to UTF8). We then throw away most of the results, and pass the string into the CallHandlerProvider. This is way too much work. At the very least we should swap `String.components(separatedBy:)` for `String.split`, which does not require Foundation. This avoids the extra `String` constructions and any other mess. What would be even better, though, is to not work in String space at all but instead in Substring space. That is, if we can avoid using Strings inside GRPCServerRequestRoutingHandler at all in favour of Substrings, we can avoid ever needing to create a String to dispatch an RPC. We'll instead look up the RPCs using a Substring, and then dispatch into the individual CallHandlerProvider with that substring. This avoids any need to allocate at all on the entire codepath except for the Array returned by `split`. For now we'll tolerate that, and if it really becomes too big of a performance problem we can try to investigate an alternative approach there instead (such as working in UTF-8 space directly). Modifications: - Replace String.components(separatedBy:) with String.split. - Update GRPCServerRequestRoutingHandler to compute on Substring, not String. - Update codegen to generate conforming code. Results: Improved performance on RPC dispatch, about a 4% win.
eb693c3
to
2d45ccf
Compare
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.
This is great; thanks @Lukasa!
@@ -13,7 +13,6 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
import Foundation |
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.
🚀
@@ -57,7 +56,7 @@ public struct CallHandlerContext { | |||
/// from the pipeline. | |||
public final class GRPCServerRequestRoutingHandler { | |||
private let logger: Logger | |||
private let servicesByName: [String: CallHandlerProvider] | |||
private let servicesByName: [Substring: CallHandlerProvider] |
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.
Not directly related to these changes: but I wonder if just storing [ChannelHandlerProvider]
would be more efficient here. The provider stores enough information and I'd expect the number of providers to be low.
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.
Yeah, in principle we could investigate that as a further optimisation as well.
Hmm I guess this is major because we're breaking generated code :/ |
Motivation:
Right now the server burns quite a lot of CPU time initially working out
which RPC handlers to use when it wants to dispatch an inbound RPC. One
chunk of time here involves processing the inbound request URI to work
out the appropriate method name.
The costs here come in a few places, but the biggest thing blocking a
performance improvement is
String.components(separatedBy:)
. Thisfunction is secretly an NSString function. The result is that the mere
act of calling this function forces us to transform the UTF8 Swift
String into a UTF16 NSString, split on the slash, and then allocate an
NSArray into which we will re-encode each of the components as Swift
Strings (transforming back from UTF16 to UTF8). We then throw away most
of the results, and pass the string into the CallHandlerProvider.
This is way too much work. At the very least we should swap
String.components(separatedBy:)
forString.split
, which does notrequire Foundation. This avoids the extra
String
constructions and anyother mess.
What would be even better, though, is to not work in String space at all
but instead in Substring space. That is, if we can avoid using Strings
inside GRPCServerRequestRoutingHandler at all in favour of Substrings,
we can avoid ever needing to create a String to dispatch an RPC. We'll
instead look up the RPCs using a Substring, and then dispatch into the
individual CallHandlerProvider with that substring.
This avoids any need to allocate at all on the entire codepath except
for the Array returned by
split
. For now we'll tolerate that, and ifit really becomes too big of a performance problem we can try to
investigate an alternative approach there instead (such as working in
UTF-8 space directly).
Modifications:
String.
Results:
Improved performance on RPC dispatch, about a 4% win.