microsoft / microsoft/vs-streamjsonrpc

Overload resolution results in repeated argument deserialization

Open
#278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
937
Forks
178
Avg merge
3d 1h
Merged PRs (30d)
28

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at TargetMethod.TryGetArguments and follow JsonRpcRequest.TryGetTypedArguments through TryGetArgumentByNameOrIndex, the entry points named in the allocation trace. Implement caching so an argument is deserialized only once for each target type while overloads are considered. Done means repeated overload resolution avoids duplicate deserialization and the reported allocation cost is reduced.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.