Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Core/Resolvers/QueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,17 @@ public async Task<JsonArray> GetJsonArrayAsync(
{
if (dbResultSetRow.Columns.Count > 0)
{
JsonElement result =
JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(dbResultSetRow.Columns));
// Use a memory stream to serialize the columns
using MemoryStream ms = new();
using (Utf8JsonWriter writer = new(ms))
{
JsonSerializer.Serialize(writer, dbResultSetRow.Columns);
}

ms.Position = 0;
using JsonDocument doc = JsonDocument.Parse(ms);
Comment on lines +593 to +600
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach may not resolve the original OOM issue and could introduce performance overhead. The MemoryStream still allocates memory internally, and you're now creating additional objects (MemoryStream, Utf8JsonWriter, JsonDocument). Consider using JsonSerializer.SerializeToUtf8Bytes() and JsonSerializer.Deserialize() with ReadOnlySpan for better memory efficiency.

Suggested change
using MemoryStream ms = new();
using (Utf8JsonWriter writer = new(ms))
{
JsonSerializer.Serialize(writer, dbResultSetRow.Columns);
}
ms.Position = 0;
using JsonDocument doc = JsonDocument.Parse(ms);
byte[] jsonBytes = JsonSerializer.SerializeToUtf8Bytes(dbResultSetRow.Columns);
using JsonDocument doc = JsonDocument.Parse(jsonBytes);

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gilemos - have you considered this suggestion from copilot?

JsonElement result = doc.RootElement.Clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that verifies the change works as expected :)


Comment on lines +592 to +602
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using JsonSerializer.SerializeToElement(dbResultSetRow.Columns) instead of the MemoryStream approach. This method directly serializes to a JsonElement without requiring intermediate MemoryStream and JsonDocument objects, which would be simpler and more efficient. This approach is already used elsewhere in the codebase (e.g., SqlResponseHelpers.cs:199).

Suggested change
// Use a memory stream to serialize the columns
using MemoryStream ms = new();
using (Utf8JsonWriter writer = new(ms))
{
JsonSerializer.Serialize(writer, dbResultSetRow.Columns);
}
ms.Position = 0;
using JsonDocument doc = JsonDocument.Parse(ms);
JsonElement result = doc.RootElement.Clone();
// Directly serialize columns to JsonElement for efficiency
JsonElement result = JsonSerializer.SerializeToElement(dbResultSetRow.Columns);

Copilot uses AI. Check for mistakes.
// FromJsonElement() added to address .NET8 regression where the .Add() throws:
// System.InvalidOperationException: "The element cannot be an object or array."
resultArray.Add(FromJsonElement(result));
Expand Down