-
Notifications
You must be signed in to change notification settings - Fork 490
JsonRpcMessage.Converter.Read optimization #639
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
base: main
Are you sure you want to change the base?
JsonRpcMessage.Converter.Read optimization #639
Conversation
@eiriktsarpalis Could you comment on the perf findings? It looks like it's worth it? |
@Scooletz I think we can keep your changes as-is, on the understanding that this only touches the core JSON-RPC infrastructure and is therefore unlikely to require evolution in the future. |
/// <summary> | ||
/// The union to deserialize. | ||
/// </summary> | ||
internal struct Union |
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.
@Scooletz I would recommend marking the Union
struct as private and hand-craft a static Parse(ref Utf8JsonReader reader)
that populates an instance of the type. This should further improve performance while also minimizing the amount of source generated code (aka there wouldn't exist any serialization code for the type).
This PR replaces the copying behavior of
JsonRpcMessage.Converter.Read
with a single pass deserialization of it. It uses aUnion
struct that deserializes the raw content ofJsonRpcMessage
to a bag of properties. Then it uses it to check the conditions and properly construct underlying objects. This makes deserialization ~50% faster and also reduced the allocations.Motivation and Context
I noticed the double deserialization in
JsonRpcMessage.Converter.Read
and wanted to make it faster.How Has This Been Tested?
Benchmarks. Allocations are ~halfed.
Before
After
Using JsonDocument.Deserialize
The attempt in using
doc.Deserialize(options.GetTypeInfo<TMessageType>())
for deserializingBreaking Changes
Nope
Types of changes
Checklist