Skip to content

Commit

Permalink
JsonDataWriter flushes output less frequently, to improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Jul 3, 2024
1 parent dd9718a commit c14be86
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions source/Sylvan.Data/JsonDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ public static long WriteJson(this DbDataReader data, Stream stream)
{
count++;
WriteRecord(data, names, writer);
if (writer.BytesPending > 0x1000)
{
writer.Flush();
}
}

writer.WriteEndArray();
writer.Flush();
return count;
}

Expand All @@ -52,7 +57,10 @@ public static async Task<long> WriteJsonAsync(this DbDataReader data, Stream str
{
count++;
WriteRecord(data, names, writer);
await writer.FlushAsync().ConfigureAwait(false);
if (writer.BytesPending > 0x1000)
{
await writer.FlushAsync().ConfigureAwait(false);
}
}

writer.WriteEndArray();
Expand All @@ -76,8 +84,6 @@ static JsonEncodedText[] GetNames(DbDataReader data)
const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFF";
const string DateTimeOffsetFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";

// TODO: measure if this is worthwhile, since we could avoid initializing the scratch buffer
//[SkipLocalsInit]
static void WriteRecord(DbDataReader data, JsonEncodedText[] names, Utf8JsonWriter writer)
{
writer.WriteStartObject();
Expand Down Expand Up @@ -180,6 +186,13 @@ static void WriteRecord(DbDataReader data, JsonEncodedText[] names, Utf8JsonWrit
break;
}

if (t == typeof(char[]))
{
char[] value = (char[])data.GetValue(i);
writer.WriteStringValue(value);
break;
}

// handle everything else
var obj = data.GetValue(i);
if (obj == DBNull.Value || obj == null)
Expand Down

0 comments on commit c14be86

Please sign in to comment.