Improve performance of routing inbound RPCs (#924)
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.