-
Notifications
You must be signed in to change notification settings - Fork 297
Use Memory Stream for Serialization #2886
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?
Changes from all commits
ddc0e0a
8cb5e58
e126bde
db73c98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||||||||||||
| JsonElement result = doc.RootElement.Clone(); | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||
| // 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); |
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.
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.
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.
@gilemos - have you considered this suggestion from copilot?