From c14be863199715f89a0ac3a422123de1c91d45c8 Mon Sep 17 00:00:00 2001 From: MarkPflug Date: Wed, 3 Jul 2024 10:50:57 -0700 Subject: [PATCH] JsonDataWriter flushes output less frequently, to improve performance. --- source/Sylvan.Data/JsonDataWriter.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/source/Sylvan.Data/JsonDataWriter.cs b/source/Sylvan.Data/JsonDataWriter.cs index c456642b..95b77927 100644 --- a/source/Sylvan.Data/JsonDataWriter.cs +++ b/source/Sylvan.Data/JsonDataWriter.cs @@ -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; } @@ -52,7 +57,10 @@ public static async Task 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(); @@ -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(); @@ -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)