-
Notifications
You must be signed in to change notification settings - Fork 439
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
bool hasMethod = root.TryGetProperty("method", out _); | ||
bool hasError = root.TryGetProperty("error", out _); | ||
|
||
var rawText = root.GetRawText(); |
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.
@stephentoub has pointed out that this call is wholly unnecessary. I'm curious what percentage of the performance improvement can be attributed to simply removing this and passing the root
directly into JsonSerializer
further down.
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.
The question that I'd ask, and I might be missing something here, is whether you want to create the root
at all? Clearly we'd benefit from not var rawText = root.GetRawText();
and this is one part of the coping. The other is creating JsonDocument
that has a few fields and does some parsing. With Union
we don't allocate at all beside for the properties that will be used in any case.
What do you want me to do then? Just remove this and rerun benchmarks and provide them in a comment?
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.
The Union
approach is clearly the fastest, but it is also the most elaborate. Before we commit to that, I'd like to see how much we can gain by fixing the obvious inefficiencies.
What do you want me to do then? Just remove this and rerun benchmarks and provide them in a comment?
No need to undo any changes yet, but if you could share benchmarks comparing all three versions would be great.
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.
👍 Will do tomorrow (CEST)
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
Breaking Changes
Nope
Types of changes
Checklist