Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit bc39ac6

Browse files
committed
Fix test
Method | Mean | StdDev | Gen 0 | Allocated | ------------------------------------------------------- |--------------- |----------- |------- |---------- | SerializeJsonStringToStream | 473.1403 ns | 2.3259 ns | 0.0343 | 183 B | SerializeJsonString256ToStream | 1,594.0896 ns | 62.2681 ns | 0.0402 | 414 B | SerializeJsonString512ToStream | 2,520.7552 ns | 7.5393 ns | 0.8331 | 2.93 kB | SerializeJsonString4096ToStream | 14,798.8854 ns | 54.2097 ns | 1.0742 | 5.48 kB |
1 parent 98bf63b commit bc39ac6

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

benchmarks/ServiceStack.Text.Benchmarks/JsonSerializationBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class JsonSerializationBenchmarks
6767
{
6868
static ModelWithAllTypes allTypesModel = ModelWithAllTypes.Create(3);
6969
static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes.Create(3);
70-
static MemoryStream stream = new MemoryStream(16384);
70+
static MemoryStream stream = new MemoryStream(32768);
7171
const string serializedString = "this is the test string";
7272
readonly string serializedString256 = new string('t', 256);
7373
readonly string serializedString512 = new string('t', 512);

src/ServiceStack.Text/DirectStreamWriter.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class DirectStreamWriter : TextWriter
1111

1212
private Stream stream;
1313
private StreamWriter writer = null;
14-
private char[] curChar = new char[1];
14+
private byte[] curChar = new byte[1];
15+
private bool needFlush = false;
1516

1617
private Encoding encoding;
1718
public override Encoding Encoding => encoding;
@@ -26,25 +27,44 @@ public override void Write(string s)
2627
{
2728
if (s.Length <= optimizedBufferLength)
2829
{
30+
if (needFlush)
31+
{
32+
writer.Flush();
33+
needFlush = false;
34+
}
35+
2936
byte[] buffer = Encoding.GetBytes(s);
3037
stream.Write(buffer, 0, buffer.Length);
31-
}
32-
else
38+
} else
3339
{
3440
if (writer == null)
3541
writer = new StreamWriter(stream, Encoding, s.Length < maxBufferLength ? s.Length : maxBufferLength);
3642

3743
writer.Write(s);
38-
writer.Flush();
44+
needFlush = true;
3945
}
4046
}
4147

4248
public override void Write(char c)
4349
{
44-
curChar[0] = c;
50+
if ((int)c < 128)
51+
{
52+
if (needFlush)
53+
{
54+
writer.Flush();
55+
needFlush = false;
56+
}
4557

46-
byte[] buffer = Encoding.GetBytes(curChar);
47-
stream.Write(buffer, 0, buffer.Length);
58+
curChar[0] = (byte)c;
59+
stream.Write(curChar, 0, 1);
60+
} else
61+
{
62+
if (writer == null)
63+
writer = new StreamWriter(stream, Encoding, optimizedBufferLength);
64+
65+
writer.Write(c);
66+
needFlush = true;
67+
}
4868
}
4969

5070
public override void Flush()

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ public void Does_encode_unicode()
6666
}
6767
}
6868

69+
[Test]
70+
public void Does_encode_large_strings()
71+
{
72+
char[] testChars = new char[32769];
73+
for (int i = 0; i < testChars.Length; i++)
74+
testChars[i] = (char)i;
75+
76+
string test = new string(testChars);
77+
78+
var obj = new { test };
79+
using (var mem = new System.IO.MemoryStream())
80+
{
81+
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), mem);
82+
83+
var encoded = System.Text.Encoding.UTF8.GetString(mem.ToArray());
84+
85+
var copy1 = JsonObject.Parse(encoded);
86+
87+
Assert.That(test, Is.EqualTo(copy1["test"]));
88+
89+
System.Diagnostics.Debug.WriteLine(copy1["test"]);
90+
}
91+
}
92+
6993
[Test]
7094
public void Can_parse_Twitter_response()
7195
{

0 commit comments

Comments
 (0)