-
Notifications
You must be signed in to change notification settings - Fork 440
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
Open
Scooletz
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
Scooletz:json-rpc-message-converter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−27
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
benchmarks/ModelContextProtocol.Benchmarks/JsonRpcMessageSerializationBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using BenchmarkDotNet.Attributes; | ||
using ModelContextProtocol.Protocol; | ||
|
||
namespace ModelContextProtocol.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class JsonRpcMessageSerializationBenchmarks | ||
{ | ||
private byte[] _requestJson = null!; | ||
private byte[] _notificationJson = null!; | ||
private byte[] _responseJson = null!; | ||
private byte[] _errorJson = null!; | ||
|
||
private JsonSerializerOptions _options = null!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_options = McpJsonUtilities.DefaultOptions; | ||
|
||
_requestJson = JsonSerializer.SerializeToUtf8Bytes( | ||
new JsonRpcRequest | ||
{ | ||
Id = new RequestId("1"), | ||
Method = "test", | ||
Params = JsonValue.Create(1) | ||
}, | ||
_options); | ||
|
||
_notificationJson = JsonSerializer.SerializeToUtf8Bytes( | ||
new JsonRpcNotification | ||
{ | ||
Method = "notify", | ||
Params = JsonValue.Create(2) | ||
}, | ||
_options); | ||
|
||
_responseJson = JsonSerializer.SerializeToUtf8Bytes( | ||
new JsonRpcResponse | ||
{ | ||
Id = new RequestId("1"), | ||
Result = JsonValue.Create(3) | ||
}, | ||
_options); | ||
|
||
_errorJson = JsonSerializer.SerializeToUtf8Bytes( | ||
new JsonRpcError | ||
{ | ||
Id = new RequestId("1"), | ||
Error = new JsonRpcErrorDetail { Code = 42, Message = "oops" } | ||
}, | ||
_options); | ||
} | ||
|
||
[Benchmark] | ||
public JsonRpcMessage DeserializeRequest() => | ||
JsonSerializer.Deserialize<JsonRpcMessage>(_requestJson, _options)!; | ||
|
||
[Benchmark] | ||
public JsonRpcMessage DeserializeNotification() => | ||
JsonSerializer.Deserialize<JsonRpcMessage>(_notificationJson, _options)!; | ||
|
||
[Benchmark] | ||
public JsonRpcMessage DeserializeResponse() => | ||
JsonSerializer.Deserialize<JsonRpcMessage>(_responseJson, _options)!; | ||
|
||
[Benchmark] | ||
public JsonRpcMessage DeserializeError() => | ||
JsonSerializer.Deserialize<JsonRpcMessage>(_errorJson, _options)!; | ||
} |
19 changes: 19 additions & 0 deletions
19
benchmarks/ModelContextProtocol.Benchmarks/ModelContextProtocol.Benchmarks.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 intoJsonSerializer
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 notvar rawText = root.GetRawText();
and this is one part of the coping. The other is creatingJsonDocument
that has a few fields and does some parsing. WithUnion
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.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)
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.
@eiriktsarpalis I added the benchmark for
JsonDocument.Deserialize
in the PR description. My machine seems to be sluggish a bit today, but still, the union approach seems to be a winner. As this is what is executed a lot, I'd still vote for the union approach.