Skip to content

Commit 00d63a6

Browse files
Use out parameter for ParseUnion to avoid struct copy
Changed ParseUnion to return the Union struct via an out parameter instead of by value, avoiding unnecessary copying of the large struct. Co-authored-by: Scooletz <scooletz@users.noreply.github.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent 635157c commit 00d63a6

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

src/ModelContextProtocol.Core/Protocol/JsonRpcMessage.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public sealed class Converter : JsonConverter<JsonRpcMessage>
7777
/// <inheritdoc/>
7878
public override JsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
7979
{
80-
var union = ParseUnion(ref reader, options);
80+
ParseUnion(ref reader, options, out Union union);
8181

8282
// All JSON-RPC messages must have a jsonrpc property with value "2.0"
8383
if (union.JsonRpc != JsonRpcVersion)
@@ -164,9 +164,9 @@ public override void Write(Utf8JsonWriter writer, JsonRpcMessage value, JsonSeri
164164
/// <summary>
165165
/// Manually parses a JSON-RPC message from the reader into the Union struct.
166166
/// </summary>
167-
private static Union ParseUnion(ref Utf8JsonReader reader, JsonSerializerOptions options)
167+
private static void ParseUnion(ref Utf8JsonReader reader, JsonSerializerOptions options, out Union union)
168168
{
169-
var union = new Union
169+
union = new Union
170170
{
171171
JsonRpc = string.Empty // Initialize to avoid null reference warnings
172172
};
@@ -228,8 +228,6 @@ private static Union ParseUnion(ref Utf8JsonReader reader, JsonSerializerOptions
228228
break;
229229
}
230230
}
231-
232-
return union;
233231
}
234232

235233
/// <summary>

0 commit comments

Comments
 (0)