Skip to content

Overload resolution results in repeated argument deserialization #278

Open

Description

When JsonRpc is resolving which overload of a target method to invoke based on arguments sent by the client, it deserializes the arguments repeatedly: once for every overload that is considered. This is apparent from the trace below which shows allocation costs incurred during overload resolution:

Name Inc % Inc
streamjsonrpc.ni!JsonRpc.HandleRpcAsync 11.1 207,171,552
+ streamjsonrpc.ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[StreamJsonRpc.JsonRpc+d__99](d__99 ByRef) 11.1 207,171,712
+ streamjsonrpc.ni!StreamJsonRpc.JsonRpc+d__99.MoveNext() 11.1 207,171,712
+ streamjsonrpc.ni!JsonRpc.DispatchIncomingRequestAsync 9.1 169,076,928
|+ streamjsonrpc.ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[System.__Canon].Start[StreamJsonRpc.JsonRpc+d__94](d__94 ByRef) 9.1 169,076,928
| + streamjsonrpc.ni!StreamJsonRpc.JsonRpc+d__94.MoveNext() 9.1 169,076,928
|  + streamjsonrpc.ni!StreamJsonRpc.TargetMethod..ctor(StreamJsonRpc.Protocol.JsonRpcRequest, System.Collections.Generic.List`1) 8.5 158,753,856
|  |+ streamjsonrpc.ni!TargetMethod.TryGetArguments 8.5 158,538,528
|  ||+ streamjsonrpc.ni!StreamJsonRpc.JsonMessageFormatter+JsonRpcRequest.TryGetTypedArguments(System.ReadOnlySpan1, System.Span1) 8.2 152,791,088
|  |||+ streamjsonrpc.ni!JsonRpcRequest.TryGetTypedArguments 8.2 152,791,088
|  ||| + streamjsonrpc.ni!StreamJsonRpc.JsonMessageFormatter+JsonRpcRequest.TryGetArgumentByNameOrIndex(System.String, Int32, System.Type, System.Object ByRef) 8.2 152,791,088
|  |||  + newtonsoft.json.ni!JToken.ToObject 8.2 152,791,088

We should cache the results of deserializing an argument to a given type so that as each overload is considered, while an argument may be deserialized more than once, it will only be deserialized once for a given target type. For large arguments and multiple overloads, this could dramatically improve performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions