-Added IJsonBufferPool, performance stuff

This commit is contained in:
James Newton-King
2015-11-12 22:08:39 +13:00
parent d91e8f4425
commit ac1a46bd14
19 changed files with 425 additions and 70 deletions
@@ -44,6 +44,11 @@ using NUnit.Framework;
#endif
using Newtonsoft.Json;
using System.IO;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Xml;
using Newtonsoft.Json.Utilities;
@@ -1168,6 +1173,74 @@ third line", jsonTextReader.Value);
Assert.AreEqual("type", reader.Value);
}
public class FakeBufferPool : IJsonBufferPool<char>
{
public readonly IList<char[]> FreeBuffers = new List<char[]>();
public readonly IList<char[]> UsedBuffers = new List<char[]>();
public char[] RentBuffer(int minSize)
{
char[] buffer = FreeBuffers.FirstOrDefault(b => b.Length >= minSize);
if (buffer != null)
{
FreeBuffers.Remove(buffer);
UsedBuffers.Add(buffer);
return buffer;
}
buffer = new char[minSize];
UsedBuffers.Add(buffer);
return buffer;
}
public void ReturnBuffer(ref char[] buffer)
{
UsedBuffers.Remove(buffer);
if (!FreeBuffers.Contains(buffer))
{
FreeBuffers.Add(buffer);
}
buffer = null;
}
}
[Test]
public void BufferTest()
{
string json = @"{
""CPU"": ""Intel"",
""Description"": ""Amazing!\nBuy now!"",
""Drives"": [
""DVD read/writer"",
""500 gigabyte hard drive""
]
}";
FakeBufferPool bufferPool = new FakeBufferPool();
for (int i = 0; i < 1000; i++)
{
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
reader.BufferPool = bufferPool;
while (reader.Read())
{
}
}
if ((i + 1) % 100 == 0)
{
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
}
}
Assert.AreEqual(2, bufferPool.FreeBuffers.Count);
}
[Test]
public void ReadNullTerminatorStrings()
{